简体   繁体   中英

Binary search tree implementation in C - Inserting incorrectly

I'm trying to implement a binary search tree in C. My insert methods are not working properly. The in-order print of this is:

1 2 4 3 5 7 6 10

The pre-order print is:

5 3 2 1 4 7 10 6

The post-order print is:

1 4 2 3 6 10 7 5

I've tried fiddling with the less-than and greater-thans and playing with the "left" and "right" char arrays I'm using to flag each inputs next move but so far I have gotten nothing. I would appreciate some help. Thank you very much!

    int main(int argc, const char * argv[]) {

        struct node* root1 = (struct node*)malloc(sizeof(struct node));
        root1->value = 5;
        root1->left = NULL;
        root1->right = NULL;
        root1->parent = NULL;
        insert(3, root1);
        insert(2, root1);
        insert(1, root1);
        insert(7, root1);
        insert(10, root1);
        insert(6, root1);
        insert(4, root1);
    }

    //Does an initial comparison to set up the helper function 'H' to actually insert the value.
    void insert(int val, struct node* rootNode) {
        if (val < (rootNode)->value) {
            insertH(val, rootNode, "left");
        } else if (val > (rootNode)->value) {
            insertH(val, rootNode, "right");
        }
    }

    //Inserts val into the BST in its proper location
    void insertH(int val, struct node* rootNode, char* helper) {
    if (!strcmp(helper, "left")) {
        if (rootNode->left == NULL) {
            rootNode->left = (struct node*)malloc(sizeof(struct node));
            rootNode->left->value = val;
            (rootNode->left)->parent = rootNode;
            rootNode->left->left = NULL;
            rootNode->left->right = NULL;
        } else {
            if (val < (rootNode)->value) {
                insertH(val, rootNode->left, "left");
            } else if (val > (rootNode)->value) {
                insertH(val, rootNode->left, "right");
            }
        }
    } else if (!strcmp(helper, "right")) {
        if (rootNode->right == NULL) {
            rootNode->right = (struct node*)malloc(sizeof(struct node));
            rootNode->right->value = val;
            (rootNode->right)->parent = rootNode;
            rootNode->right->left = NULL;
            rootNode->right->right = NULL;

        } else {
            if (val < (rootNode)->value) {
                insertH(val, rootNode->right, "left");
            } else if (val > (rootNode)->value) {
                insertH(val, rootNode->right, "right");
            }
        }
    }
}

In BST, a new key is always inserted at leaf. We start searching a key from root till we hit a leaf node. Once a leaf node is found, the new node is added as a child of the leaf node.
This is code with comments for you to understand how this works, I hope you will find this helpful

/* A utility function to insert a new node with given key in BST */
struct node* insert(struct node* node, int key)
{
    /* If the tree is empty, return a new node */
    if (node == NULL) return newNode(key);

    /* Otherwise, recur down the tree */
    if (key < node->key)
        node->left  = insert(node->left, key);
    else if (key > node->key)
        node->right = insert(node->right, key);   

    /* return the (unchanged) node pointer */
    return node;
}

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