【问题标题】:Is binary tree search lying to me?二叉树搜索是在骗我吗?
【发布时间】:2014-06-30 15:35:45
【问题描述】:

嘿,我正在尝试编写一个接收字符串列表的程序(这些是按顺序排列的):

polymorphism
object
templates
structure
class
pointer
reference
traversal
inheritance
exceptions
recursive
overloading

然后将这些字符串存储在二叉树中,最后进行中序遍历。 但是,我遇到了一个我无法弄清楚的问题。我添加节点的功能一直告诉我我已经添加了节点,但是实际上从未添加过?我的输出是这样的:

ADDED NODE: polymorphism
ERROR: Same Data: object, object
ERROR: Same Data: templates, templates
ERROR: Same Data: structure, structure
ERROR: Same Data: class, class
ERROR: Same Data: pointer, pointer
(etc...)
ERROR: overloading, overloading
ERROR: overloading, overloading
FINISHED BUILDING

overloading

最后,这里是源代码:

#include <stdlib.h>
#include <stdio.h>

struct tree {
    char* data;
    struct tree *left;
    struct tree *right;
};

void buildTree(struct tree**);
void printAlpha(struct tree*);
void insert(struct tree **root, char *n);

int main(int argc, char* argv[]) {
    struct tree* myTree = NULL;

    buildTree(&myTree);
    printf("FINISHED BUILDING\n\n");
    printAlpha(myTree);

    system("PAUSE");
    return 0;
}

/*Builds tree from text file*/
void buildTree(struct tree **root) {
    FILE* fIn = fopen("./in.txt", "r");
    char* input = (char*) malloc(sizeof(char));

    if(!fIn) {
        printf("ERROR: Cannot find file\n");
        return;
    }

    while(!feof(fIn) && fscanf(fIn, "%s", input)) {
        // printf("INP:%s\n", input);
        insert(root, input);
    }
}

void insert(struct tree **root, char *n) {
    if (*root == NULL) {
        // found the spot, create and insert the node
        struct tree *newNode = NULL;
        newNode = (struct tree*) malloc(sizeof(struct tree) );
        newNode->data = n;
        newNode->left = NULL;
        newNode->right = NULL;
        *root = newNode;

        printf("ADDED NODE: %s\n", newNode->data);
    }

    else if(strcmp(n, (*root)->data) < 0)
    insert(&((*root)->left), n);
    else if(strcmp(n, (*root)->data) > 0)
    insert(&((*root)->right), n);
    else
    printf("ERROR: Same data: %s, %s\n", (*root)->data, n);
}

/*In order traversal*/
void printAlpha(struct tree *root) {
    struct tree *curNode = root;

    /*If empty something went wrong*/
    if(!curNode) {
        printf("Error: Binary Tree Is Empty!\n");
        //  return;
    }

    if(curNode->left != NULL) {
        printAlpha(root->left);
    }

    printf("%s\n", curNode->data);

    if(curNode->right != NULL) {
        printAlpha(curNode->right);
    }
}

【问题讨论】:

  • 算法不会撒谎,如果它们给你的结果你不喜欢,那你就教错了。
  • @Isantipov 我不知道您是否曾经在 C 语言中使用过调试器,但我可以肯定地说,它们对 **** 来说很痛苦。如果你没有什么建设性的话,你为什么还要评论呢?
  • 你应该标记什么语言。我猜是 C?
  • @maremp 调试在任何语言中都是一个重要的工具,通常可以用来查明问题,因为您可以看到发生了什么。

标签: c binary-search-tree inorder


【解决方案1】:

您正在创建单个字符串 (char* input = (char*) malloc(sizeof(char));) 并每次都覆盖其内容。您将这个字符串插入到树中,然后下次将其与自身进行比较。

解决方案:将malloc 移动到循环内。

【讨论】:

  • 还应该给字符串一个真实的大小,否则它们也会有缓冲区溢出(也是代码存在的问题)。
  • 感谢“Daniel Darabos”和“crashmstr”。您的两个 cmets 现在为我提供了一个功能齐全的程序。再次感谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-08
  • 1970-01-01
  • 2013-04-14
  • 2017-09-28
  • 2021-10-01
  • 1970-01-01
相关资源
最近更新 更多