简体   繁体   English

无法理解在二进制搜索树中插入的逻辑

[英]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 // head是根节点,num是关键元素

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. 当树的节点的值小于num时 ,您可以向右导航树。 When the value of one node is highter than your num, you navigate the tree to the left, not to the right. 当一个节点的值大于num时,将树导航到左侧,而不是右侧。 This is the loop until you reach a NULL node, that will be the place of your new node with value num . 这是循环,直到您到达NULL节点为止,该节点将成为新节点的num值。

In this code block. 在此代码块中。

prev pointer will point to the leaf node after traversing whole tree based upon the value of num . prev指针将根据num的值遍历整棵树后指向叶节点。

temp is NULL, so space has been allocated using malloc so that it can hold a node with value as num . temp为NULL,因此已使用malloc分配了空间,以便它可以容纳值为num的节点。

Now, if value of num is greater than value of its parent ie prev->a, temp will become right child of prev. 现在,如果num的值大于其父级的值,即prev-> a,则temp将成为prev的正确子级。

If value of num is greater than value of its parent ie prev-a, temp will become left child of prev. 如果num的值大于其父级(即prev-a)的值,则temp将成为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. num小于在节点temp处存储的值时,探索将在左分支继续,否则它将在右分支继续。 During this process, it keeps track of prev which is the parent node of temp . 在此过程中,它将跟踪prev ,它是temp的父节点。

The search ends, when temp is null. temp为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. 这是必须插入num地方。

It then creates a new node called temp , stores the num in it. 然后,它创建一个名为temp的新节点,将num存储在其中。 Note that there is a small error here. 请注意,这里有一个小错误。 One shouldn't cast the return value of malloc . 一个不应该转换malloc的返回值。 This malloc works, but it is considered bad practice. 此malloc有效,但被认为是不良做法。

Then it retest if the node must be attached as a left or right branch of the parent node prev and attach it accordingly. 然后,它重新测试,如果该节点必须作为父节点的左或右分支被连接prev并相应地将其固定。 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM