adelbertc
adelbertc

Reputation: 7320

Binary tree implementation in C question as found in K&R

So I've been reading through the K&R C book and have a question.. in the 6th Chapter on structs on page 140-141, there is code that looks like this (I took out some of the more irrelevant parts)

/*
the program loops through a tree looking for some word
if it finds the word itll incremenet the count by 1 
if it doesnt itll add a new node
*/

 struct node {
    char *word;
    int count;
    struct node *left;
    struct node *right;
}

 main() {
    struct node *root;
    char word[1000];

    root = NULL;
    while(getword(word, MAXWORD) != EOF) /* getword just grabs 1 word at a time from a file of words */
        if(isalpha(word[0])) /* isalpha checks to see if it is a valid word */
            root = addNode(root, word);

    treeprint(root); /* prints the tree */
    return 0;
}

struct node *addNode(struct node *p, char *w) {
    int cond;

    if(p == NULL) {
        p = malloc(sizeof(struct node)); /* allocates memory for the new node */
        p -> word = strdup(w);
        p -> count = 1;
        p -> left = p -> right = NULL;
    }

    else if ((cond = strcmp(w, p -> word)) == 0)
        p -> count++;

    else if(cond < 0)
        p -> left = addNode(p -> left, w);

    else
        p -> right = addNode(p -> right, w);

    return p;
}

And my confusion is in the main() function at root = addNode(root, word)

If addNode returns a pointer to the newly added node (or to the node that word is at if its already int he tree), wouldn't that "lose" all the data above the tree? Shouldn't root stay as the root of the tree?

Thanks!

Upvotes: 6

Views: 2253

Answers (2)

taskinoor
taskinoor

Reputation: 46027

root is always staying as root of the tree. root is passed as the first parameter of addNode which will only malloc if that is NULL, i.e. when root is passed for the first time. In later calls it won't change root, will only modify count, left or right. Note that in recursive addNode calls p is not passed, rather it's left or right child is passed. Try to go through the tree with a paper and pencil/pen and you will realize how the nodes are getting added.

Upvotes: 5

Thomas
Thomas

Reputation: 181745

Your misunderstanding is in the behaviour of addNode. It does not return a pointer to the newly added node; rather, it returns a pointer to the node that was passed in, p (unless that was NULL).

Since the only time that root == NULL is when the very first word is added, root will have the same value from that point on, and get assigned this very same value over and over again. This is just an elegant way of dealing with empty trees, which are represented by the NULL pointer.

Remember that each recursive call of addNode has a different value for p, though. This is how local variables work; they are local to a particular invocation of the function, not to the function as a whole. Maybe this led to your misunderstanding of the function's behaviour.

Upvotes: 3

Related Questions