简体   繁体   English

插入二叉搜索树

[英]Insertion in a Binary Search Tree

My teacher posted the following code on the class website. 我的老师在班级网站上发布了以下代码。 I don't understand the working of it. 我不了解它的工作原理。 If someone can elaborate, that'd be great. 如果有人可以详细说明,那就太好了。 One more thing, why is she using pair as a return value, won't using only bool suffice? 还有一件事,为什么她用对作为返回值,而不是只用布尔值就够了?

Here's the code: 这是代码:

template <class T>

std::pair<BST<T>::iterator, bool> BST<T>::insert(const T& val) {
    node<T>* t = root, *parent = null;

    while (t) {
        if (t->val == val)
            //return std::pair<BST<T>::iterator, bool>(iterator(t, this), false);    
            return std::make_pair(iterator(t, this), false); // stl convenience function

        parent = t;

        if (t->val > val) t = t->left;
        else if (t->val < val) t = t->right;
    }

    node<T>* newNode = new node<T>(val);  // (1) allocate memory
    newNode->parent = parent;             // (2) link child to parent

    //(3) link parent to child
    if (!parent) root = newNode;
    else if (parent->val > val) parent->left = newNode;
    else parent->right = newNode;

    return std::make_pair(iterator(newNode, this), true);
}

First, you need to know what does BST(binary search tree) means. 首先,您需要了解BST(二进制搜索树)的含义。

BST is a kind of data struct that used for searching. BST是一种用于搜索的数据结构。 The value of a node won't be bigger than the value of its left child node, and won't be smaller than the value of its right child node. 一个节点的值不会大于其左子节点的值,并且不会小于其右子节点的值。

Then, let's talk about your teachers code. 然后,让我们谈谈您的老师代码。

There are two big steps to do the job. 要完成这项工作有两个重要步骤。

  1. find the position of the node where it will be inserted. 找到要插入节点的位置。 Your teacher's code use a while loops to do the job. 您老师的代码使用while循环来完成这项工作。

    node* t = root, *parent = null; 节点* t =根,*父=空;

As initialization, the probe is assigned to root. 作为初始化,将探针分配给root。 Parents means the parent node of the probe. 父级是指探针的父级节点。 If it is null, that means the probe is the root node. 如果为空,则表示探针是根节点。

while (t) {
    if (t->val == val)   
        return std::make_pair(iterator(t, this), false);        
    parent = t;
    if (t->val > val) t = t->left;
    else if (t->val < val) t = t->right;
    }

Compare the value you need to insert and the value of the probe. 比较您需要插入的值和探针的值。 If the insert value is smaller than the value of the probe, assign the probe to its left child. 如果插入值小于探测器的值,则将探测器分配给它的左子级。 If it is bigger assign the probe to its right child. 如果更大,则将探针分配给其正确的子代。 If it is equal to the value of probe, insertion fail. 如果等于探测器的值,则插入失败。 Do the job until insertion fail or the probe is null which means it reaches leaf node. 进行作业,直到插入失败或探针为空(这意味着它到达叶节点)为止。

  1. insert the node. 插入节点。

    node* newNode = new node(val); node * newNode =新的节点(val); newNode->parent = parent; newNode-> parent =父级;
    if (!parent) root = newNode; 如果(!parent)root = newNode; else if (parent->val > val) parent->left = newNode; 否则(parent-> val> val)parent-> left = newNode; else parent->right = newNode; 否则parent-> right = newNode;

Create a new node and assign the parent to its parent node. 创建一个新节点,并将父节点分配给它的父节点。 If parents of the probe is null, which means the tree is empty before you insert the node. 如果探针的父级为空,则表示在插入节点之前树为空。 So set the new node as root. 因此,将新节点设置为root。 If the value of the parent is bigger than its value, assign its parent's left child to it. 如果父级的值大于其值,则为其分配父级的左子级。 If smaller, assign the right child to it. 如果较小,则为其分配合适的孩子。

return std::make_pair(iterator(newNode, this), true);

return the result of the insertion. 返回插入的结果。

Your last question. 您的最后一个问题。 I think your teacher wish to return both the result of insertion and the node where the node is. 我认为您的老师希望同时返回插入结果和该节点所在的节点。 If the result is false, it means that there is a node with the same value as the value you wish to insert. 如果结果为假,则表示存在与您要插入的值相同的值的节点。 So it return the node where it is. 因此,它将返回该节点所在的位置。 If the result is true, it means that you successfully insert a node and the node it return is the node you insert. 如果结果为true,则表示您已成功插入节点,并且返回的节点是您插入的节点。

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

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