简体   繁体   中英

Find a node in a binary search tree

My findNode is called in my insert function with the address of tree. Since im inserting the first word tree should be NULL but when I debug it skips over this check in my FindWords function.

Im not sure exactly what is the value of tree here if it is not NULL, please advise.

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. 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.

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() . In the insertNode() , you are doing

new_node->word = word;

which will make all the new node new_node->word pointer point to same buffer word . Any changes in the content of word buffer will reflect in all the nodes nodes->word value. Instead, you should do

new_node->word = strdup(word);

strdup() duplicates the given string. It uses malloc to obtain memory for the new string. Make sure to free it once you are done with it.

You should also initialise the tree with NULL before creating 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. So, you should explicitly initialise the tree pointer with NULL .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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