简体   繁体   中英

Adding nodes to Binary Search Tree

Disclaimer: This is for an assignment. I am not asking for explicit code answers, only help understanding why my code isn't working.

I am trying to implement a basic Binary Search Tree, but I am having problems with my _addNode(...) function.

Here's the problem. When I walk through my code with the debugger, I notice that leaf nodes are created infinitely on both sides (left and right) so aside from the creation of the root, there is never any point when a leaf node is NULL . The problem is that I am asking my program to create a new node whenever it finds a NULL value where a leaf would be. Therefore, if there are never any NULL values, there will never be any new leaves created, right?

The other issue I'm running into is with my compare(...) function. Stepping through it in the debugger shows it to iterate through the function several times, never actually returning a value. When it returns to the calling function, it drops back into the compare(...) function and loops infinitely. Again, I don't know why this is happening considering I have valid return statements in each if statement.

Here is all the code you'll probably need. If I left something out, let me know and I'll post it.

struct Node {
    TYPE         val;
    struct Node *left;
    struct Node *right;
};

struct BSTree {
    struct Node *root;
    int          cnt;
};

struct data {
    int number;
    char *name;
};

int compare(TYPE left, TYPE right)
{
    assert(left != 0);
    assert(right != 0);

    struct data *leftData = (struct data *) left;
    struct data *rightData = (struct data *) right;

    if (leftData->number < rightData->number) {
        return -1;
    }
    if (leftData->number > rightData->number) {
        return 1;
    } else return 0;
}

void addBSTree(struct BSTree *tree, TYPE val)
{
    tree->root = _addNode(tree->root, val);
    tree->cnt++;
}

struct Node *_addNode(struct Node *cur, TYPE val)
{
    assert(val != 0);

    if(cur == NULL) {
        struct Node * newNode = malloc(sizeof(struct Node));
        newNode->val = val;
        return newNode;
    }
    if (compare(val, cur->val) == -1) {
        //(val < cur->val)
        cur->left = _addNode(cur->left, val);
    } else cur->right = _addNode(cur->right, val);

    return cur;
}

Edit: Adding the below function(s)

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

    /*Create value of the type of data that you want to store*/
    struct data myData1;
    struct data myData2;
    struct data myData3;
    struct data myData4;

    myData1.number = 5;
    myData1.name = "rooty";
    myData2.number = 1;
    myData2.name = "lefty";
    myData3.number = 10;
    myData3.name = "righty";
    myData4.number = 3;
    myData4.name = "righty";

    /*add the values to BST*/
    addBSTree(tree, &myData1);
    addBSTree(tree, &myData2);
    addBSTree(tree, &myData3);
    addBSTree(tree, &myData4);

    /*Print the entire tree*/
    printTree(tree);
    /*(( 1 ( 3 ) ) 5 ( 10 ))*/
    return 1;
}

Maybe you could try setting right and left to NULL right after malloc :

struct Node * newNode = malloc(sizeof(struct Node));
newNode->left = NULL;
newNode->right = NULL;

Check this line here (or the corresponding for left):

cur->right = _addNode(cur->right, val);

If cur->right == 0, it's fine. But if cur->right != 0, the node that was sitting there will be replaced by the return value of _addNode, which ultimately is not a whole branch, but just one node.

I like to explicitly 0-out values in a struct after a malloc using memset(newNode, 0, sizeof(struct Node)). Others might disagree.

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