简体   繁体   English

在二叉搜索树中查找节点

[英]Find a node in a binary search tree

My findNode is called in my insert function with the address of tree.我的 findNode 在我的插入 function 中被调用,地址为树。 Since im inserting the first word tree should be NULL but when I debug it skips over this check in my FindWords function.由于我插入的第一个单词树应该是 NULL 但是当我调试它时它会跳过我的 FindWords function 中的这个检查。

Im not sure exactly what is the value of tree here if it is not NULL, please advise.如果不是 NULL,我不确定树的确切值是多少,请告知。

struct TREENODE {
    struct TREENODE *parent; // point to parent node of this node
    struct TREENODE *left; // point to left child of this node
    struct TREENODE *right; // point to right child of this node

    char *word; // store a unique word in the text file
    int count; // num of times this word appears in the file
};

typedef struct TREENODE NODE;
#define MAXWORDLEN  1000

when I insert the first word, tree should be NULL here because no words exist.当我插入第一个单词时,树应该是 NULL 因为没有单词存在。 But instead of this function returning NULL when no words exists its skips to the if statement (strcmp(word, tree->word == 0)) and causes a segmentation fault.但是,当不存在任何单词时,它不会返回 NULL 而不是这个 function 而是跳到 if 语句 (strcmp(word, tree->word == 0)) 并导致分段错误。

NODE* findNode(char *word, NODE* tree) {
  // return node storing word if exists
    if (tree == NULL){return NULL;}

    if (strcmp(word, tree->word)==0)
      return tree;
    if (strcmp(word, tree->word) <0)
          return findNode(word, tree->left);
    return findNode(word, tree->right);
 }


 void insertNode(char *word, NODE** address_of_tree ) {


    NODE *tree = *address_of_tree;
    NODE *node;
     node = findNode(word, tree); 
    if (node == NULL) {

    NODE *new_node = malloc(sizeof(NODE));
    new_node->word = word;
    new_node->parent = *address_of_tree;
    new_node->left = NULL;
    new_node->right = NULL;

    if (tree == NULL) {
        *address_of_tree = new_node;
        return;
    }
    if (strcmp(word, tree->word) < 0) {
        // word must be added to left subtree
        if (tree->left !=NULL) insertNode(word, &tree->left);
        else {
            new_node->parent = tree;
            tree->left = new_node;
        }
    }
    else {
        if (tree->right != NULL) insertNode(word, &(tree->right));
        else {
            new_node->parent = tree;
            tree->right = new_node;
        }
      }
    }
     else {
        // update the count for the word
        node->count += 1;
    }

 }

void printTree(NODE* tree) {
    // print each word and its count in alphabetical order
    if (tree == NULL) return;
     printTree(tree->left);
     printf("word: %s\ncount: %d", tree->word, tree->count);
     printTree(tree->right);
}


int main() {
      // read text from stdin
      // each time we read a word
     // insert this word into the tree

     NODE *tree; // the tree we are using to store the words

     char word[MAXWORDLEN];
      while(scanf("%s", word) != EOF) {
        // insert this word into the tree
        insertNode(word, &tree);
     }

    printTree(tree);

    return 0;
 }

You are taking input in buffer word and passing it to insertNode() .您正在输入缓冲区word并将其传递给insertNode() In the insertNode() , you are doinginsertNode()中,您正在做

new_node->word = word;

which will make all the new node new_node->word pointer point to same buffer word .这将使所有新节点new_node->word指针指向同一个缓冲区word Any changes in the content of word buffer will reflect in all the nodes nodes->word value. word缓冲区内容的任何变化都将反映在所有节点节点nodes->word值中。 Instead, you should do相反,你应该这样做

new_node->word = strdup(word);

strdup() duplicates the given string. strdup()复制给定的字符串。 It uses malloc to obtain memory for the new string.它使用malloc为新字符串获取 memory。 Make sure to free it once you are done with it.完成后确保释放它。

You should also initialise the tree with NULL before creating tree您还应该在创建树之前使用NULL初始化tree

NODE *tree = NULL;

because you are passing tree pointer to insertNode() and in the insertNode() you are checking for tree pointer is NULL or not.因为您将tree指针传递给insertNode()并且在insertNode()中检查tree指针是否为NULL So, you should explicitly initialise the tree pointer with NULL .因此,您应该使用NULL显式初始化tree指针。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM