简体   繁体   中英

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 is a kind of data struct that used for searching. 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.

    node* t = root, *parent = null;

As initialization, the probe is assigned to 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); newNode->parent = parent;
    if (!parent) root = newNode; else if (parent->val > val) parent->left = newNode; else 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. 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.

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