简体   繁体   中英

Unable to understand logic in insertion in binary search tree

I was studying binary search tree and I came through this code which I am unable to understand

//head is the root node &num is the key element

void generate(struct node **head, int num)
{
    struct node *temp = *head, *prev = *head;
    if (*head == NULL)
    {
        *head = (struct node *)malloc(sizeof(struct node));
        (*head)->a = num;
        (*head)->left = (*head)->right = NULL;
    }
    else
    {
        while (temp != NULL)
        {
            if (num > temp->a)
            {
                prev = temp;
                temp = temp->right;
            }
            else
            {
                prev = temp;
                temp = temp->left;
            }
        }
        temp = (struct node *)malloc(sizeof(struct node));
        temp->a = num;

//I am not able to understand the following lines

        if (num >= prev->a)
        {
            prev->right = temp;
        }
        else
        {
            prev->left = temp;
        }
    }

}

In binary search tree the left child has a lower value that the parent, and the right child has a higher value that the parent. Then, if you want to insert a new node, you have to find his site. While the value of the nodes of the tree is less than your num , you navigate the tree to the right. When the value of one node is highter than your num, you navigate the tree to the left, not to the right. This is the loop until you reach a NULL node, that will be the place of your new node with value num .

In this code block.

prev pointer will point to the leaf node after traversing whole tree based upon the value of num .

temp is NULL, so space has been allocated using malloc so that it can hold a node with value as num .

Now, if value of num is greater than value of its parent ie prev->a, temp will become right child of prev.

If value of num is greater than value of its parent ie prev-a, temp will become left child of prev.

Just above the code you don't understand, the program is going down the tree going left or right. When num is smaller than the value stored at node temp , the exploration continues on the left branch, otherwise it continues on the right branch. During this process, it keeps track of prev which is the parent node of temp .

The search ends, when temp is null. This means that there is no node attached to the left or right branch where we wanted to go. This is where num has to be inserted.

It then creates a new node called temp , stores the num in it. Note that there is a small error here. One shouldn't cast the return value of malloc . This malloc works, but it is considered bad practice.

Then it retest if the node must be attached as a left or right branch of the parent node prev and attach it accordingly. This is what the code you don't understand does.

There is a bad bug in this code because the new node has undefined values for left and right branches.

temp = (struct node *)malloc(sizeof(struct node));
temp->a = num;
temp->right = temp->left = NULL;  // <-- missing instruction

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