Data structure and algorithm] Easy-to-understand explanation of binary search tree search

In the article Binary Tree Traversal (please poke me), the traversal of the binary search tree is mainly introduced. This article will continue to introduce the search of the binary search tree.

The search of the second search fork tree mainly involves searching for the specified element node, maximum and minimum search, and searching for the predecessor or successor node of the specified node. Introduced separately below.

Find the specified element

The process of searching for a given value key in a binary search fork tree is very similar to binary search. The process is: first, the keyword key is compared with the key of the root of the tree. If the key is greater than the key of the root, then the key is Search in the right subtree, otherwise search in the left subtree of the root. Repeat this process until an empty node is found or encountered. The following figure shows the process of finding a key of 2 nodes.
Data structure and algorithm] Easy-to-understand explanation of binary search tree search

According to the search process, the recursive and non-recursive code implementations are as follows:

//查找值为key的节点,递归版本
Node* bstree_search(BSTree root, Type key)
{
    if (root==NULL || root->key==key)
        return root;

    if (key < root->key)
        return bstree_search(root->left, key);
    else
        return bstree_search(root->right, key);
}
//查找值为key的节点,非递归版本
Node* iterative_bstree_search(BSTree root, Type key)
{
    while ((root!=NULL) && (root->key!=key))
    {
        if (key < root->key)
            root = root->left;
        else
            root = root->right;
    }

    return root;
}

Find the maximum and minimum values

According to the nature of the binary search tree, it is easy to think: a non-empty binary search tree finds its maximum value. The process is simple: only need to recursively traverse from the root node to the right subtree node. When the right child of the traversed node is NULL, this node is the maximum value of the tree, as shown in the following figure.

In the same way, the process of finding its minimum value is similar: traverse recursively from the root node to the left subtree node. When the left child of the traversed node is NULL, then this node is the minimum value of the tree.
Data structure and algorithm] Easy-to-understand explanation of binary search tree search

The code implementation for finding the maximum value is given below. The minimum value is similar. You can try to find the minimum value yourself.

//查找最大值
Node* bstree_maximum(BSTree root)
{
    if (root == NULL)
        return NULL;

    while(root->right != NULL)
        root = root->right;
    return root;
}

Find predecessor and successor nodes

The predecessor and successor nodes of a node mentioned here refer to the predecessor and successor of a certain node in the in-order traversal sequence; in more detail: for a binary search tree, the predecessor of a certain node x is less than the key The largest node among all keywords in [x], the successor is the smallest node among all keywords in key[x].

Find the precursor steps:

(1) First judge whether node x has a left subtree. If there is a left subtree, the largest node of its left subtree is the predecessor of x;
(2) If there is no left subtree, but the node is the right of its parent node Child, then the parent node is the predecessor node of the node;
(3) If there is no left child tree, but the node is the left child of its parent node, then you need to look for the top of the tree along its parent node until you find one Node P, P node is the right child of its parent node Q, then Q is the predecessor node of this node.

Data structure and algorithm] Easy-to-understand explanation of binary search tree search
Data structure and algorithm] Easy-to-understand explanation of binary search tree search
Steps to find successor nodes:
(1) First judge whether node x has a right subtree, if there is a right subtree, the smallest node of its right subtree is the predecessor of x;
(2) If there is no right subtree, but this node Is the left child of its parent node, then the parent node is the successor node of the node;
(3) If there is no right child tree, but the node is the right child of its parent node, then it needs to follow its parent node all the way to the tree Search at the top until it finds a node P, which is the left child of its parent node Q, then Q is the successor of this node.

For the third case, the text may be a bit abstract. Let's use a picture to demonstrate it. For example, the following figure finds the process of node 13's successor node 15:
Data structure and algorithm] Easy-to-understand explanation of binary search tree search

The code implementation of the predecessor node search is given below. The successor node is similar. You can try to implement the successor node search code yourself.

//查找节点x的前驱节点Node* bstree_predecessor(Node *x)
{
    /* 如果x存在左孩子,则"x的前驱结点"为     
    "以其左孩子为根的子树的最大结点"。*/
    if (x->left != NULL)
        return bstree_maximum(x->left);

    // 如果x没有左孩子。则x有以下两种可能:
    // (01) x是"一个右孩子",则"x的前驱结点"为 "它的父结点"。
    /* (02) x是"一个左孩子",则查找"x的最低的父结点,            
            并且该父结点要具有右孩子",找到的这个"最低的父结点"            
            就是"x的前驱结点"。*/
    Node* y = x->parent;
    while ((y!=NULL) && (x==y->left))
    {
        x = y;
        y = y->parent;
    }

    return y;
}

Recommended reading:

[Welfare] Video sharing of online boutique courses collected by myself ( Part 1 )
[System design] LRU cache
[Data structure and algorithm] Easy to understand binary search tree
[Data structure and algorithm] Easy to understand linked list
[Data structure and algorithm ] Easy-to-understand explanation of bit sorting
[C++ Notes] C++11 concurrent programming (1) Start the thread journey
[C++ Notes] Common pitfalls in using C/C++ pointers

Focus on server background technology stack knowledge summary sharing

Welcome to pay attention to communication and common progress

Data structure and algorithm] Easy-to-understand explanation of binary search tree search

Coding

The code farmer has the right way to provide you with easy-to-understand technical articles to make technology easier!

Guess you like

Origin blog.51cto.com/15006953/2552023