简体   繁体   中英

Insert new node in a binary search tree at C

I already have a binary search tree with 5 levels, I am trying to create a function which inserts a new node but I can't do it working right. How can I edit my code to do the insertion?

TREE *insertion(TREE *root, int num){
    TREE *node, *new_node;
    node = root;
    new_node=(TREE *)malloc(sizeof(TREE));
    new_node->rec = num;
    new_node->lp = NULL;
    new_node->rp = NULL;

    while(node != NULL ){
//      if(node->rec == num){
//          printf("This value exists\n");
//          return NULL;            
//      }
        if(num > node->rec){
            node = node->rp;    
        }
        else
            node = node->lp;
    }

    if(num>node->rec)
        node = new_node;
    else
        node = new_node;

    return root;}

If this num > node->rec is true you need to check whether node->rp is NULL or not, which you didn't check.

Also pass the address of head to insertion() so that modification in passed variable head affects in calling function and you no need to return anything.

Assume you are calling insertion() like below

int main(void) {
        TREE *head = NULL;
        insertion(&head,10);/* pass the  address of head so that modification will affect here also otherwise just passing head means it will treat as call by value */
        insertion(&head,5);
        insertion(&head,15);
        return 0;
}

And insertion() function looks like

int  insertion(TREE **root, int num) {
        TREE *node, *new_node;
        node = *root;
        new_node = malloc(sizeof(TREE)); /* no need of casting malloc() results */
        new_node->rec = num;
        new_node->lp = NULL;
        new_node->rp = NULL;

        while(node != NULL ){
                if(node->rec == num){
                        printf("This value exists\n");
                        return NULL;`
                }

                if(num > node->rec){ /* right child of root node */
                        if(node->rp != NULL) { /* check node->rp is empty or not
                                                  if not, update node.. check here itself */
                                node = node->rp; /* update node */
                        }
                        else {
                                node->rp = new_node; /* insert new node */
                                return; /* new node added at correct position , so no need of any further processing, jut return */
                        }
                }
                else {
                        if(node->lp != NULL) {
                                node = node->lp;
                        }
                        else {
                                node->lp = new_node;
                                return;
                        }
                }
        }
}

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