Search - Search of tree table - Binary sort tree

Definition of binary sort tree

Binary Sort Tree, also known as binary search tree, is a special binary tree useful for both sorting and searching.
Definition:
A binary sorting tree is either an empty binary tree, or a binary tree with the following properties:

  • The keys of all nodes on the left subtree are smaller than the keys of the root node;
  • The keys of all nodes on the right subtree are greater than the keys of the root node;
  • The left subtree and the right subtree are each a binary tree.
//二叉排序树结点
typedef int KeyType;
typedef struct {
    
    
    KeyType key;
    char info[];
}ElemType;

typedef struct BSTNode{
    
    
    ElemType data;
    struct BSTNode * lchild;
    struct BSTNode * rchild;
}BSTNode,*BSTree;

Insert image description here

Left subtree node value < root node value < right subtree node value "
> \quad\qquad\qquad\qquad \Downarrow">
Perform in-order traversal to obtain an increasing ordered sequence.

Find operation

Because a binary sort tree can be regarded as an ordered list, searching on a binary sort tree is similar to a binary search, and is also a process of gradually narrowing the search scope.

Algorithm Description:

① If the binary sorting tree is empty, the search fails and a null pointer is returned.
② If the binary sorting tree is not empty, compare the given key with the key of the root node;

  • If they are equal, the search is successful and the root node pointer is returned;
  • If it is less than the root node, search recursively on the left subtree.
  • If it is greater than the root node, search recursively on the right subtree.
    Algorithm Description:
BSTree SearchBST(BSTree T,KeyType key){
    
    
    if((!T)||key==T->data.key)
        return T;
    else if(key<T->data.key)
        return SearchBST(T->lchild,key);
    else
        return SearchBST(T->rchild,key);
}

insert operation

The insertion operation of binary sorted tree is based on search. To insert a node * S with a key value of key into a binary sorted tree, you need to search downward from the root node, and insert it only when there is a node with a key equal to key in the middle of the tree. The newly inserted node must be a newly added leaf node, and be the left child or right child node of the last node visited on the search path when the search part is successful.

Algorithm Description

① If the binary sorting tree is empty, the node *S to be inserted is inserted into the empty tree as the root node.
② If the binary sorting tree is not empty, compare the given key with the key of the root node (T->data.key):

  • If key< T->data.key, insert *S into the left subtree;
  • If key>T->data.key, insert *S into the right subtree.
int InsertBST(BSTree &T, ElemType e) {
    
    
    if (!T) {
    
    
        T = (BSTree) malloc(sizeof(BSTNode));
        T->data = e;
        T->lchild = T->rchild = NULL;
        return 1;
    } else if (e.key == T->data.key)
        return 0;
    else if (e.key < T->data.key)
        return InsertBST(T->lchild, e);
    else if (e.key > T->data.key)
        return InsertBST(T->rchild, e);
}

Delete operation

The deleted node may be any node in the binary sorting tree. After the node is deleted, the pointers of its parent node and its related nodes must be modified according to its position to maintain the characteristics of the binary sorting tree.
Insert image description here

If the deleted node z is a leaf node, it will be deleted directly, which will not behave like a binary sort tree.
Insert image description here

If node z has only a left subtree, let z's subtree become the subtree of z's parent node, replacing z's position.
Insert image description here
If node z has only the right subtree, let z's subtree become the subtree of z's parent node, replacing z's position.
Insert image description here
If the left and right subtrees of node z are not empty, replace z with the direct predecessor of z, and then delete the direct predecessor from the binary sorting tree, thus converting to situation ① or ②.
Insert image description here
If the left and right subtrees of node z are not empty, replace z with the direct successor of z, and then delete this direct successor from the binary sorting tree, thus converting to situation ① or ②.

Algorithm Description:

  1. First, search for the node to be deleted with the keyword key:
    ① If the deleted node z is a leaf node, it will be deleted directly, which will not have the characteristics of a binary sort tree.
    ② If node z has only a left subtree or only a right subtree, let z's subtree become the subtree of z's parent node, replacing z's position.
    ③ If the left and right subtrees of node z are not empty, replace z with the direct successor (or direct predecessor) of z, and then delete the direct successor (or direct predecessor) from the binary sorting tree, thus converting to ① or ② situation.
void DeleteBST(BSTree &T, KeyType key) {
    
    
    BSTNode *z = T; //待删除的结点
    BSTNode *f = NULL; //待删除结点 *z 的双亲结点
    /**----------------------- while循环从根开始查找关键字等于key的结点*z -----------------------*/
    while (z) {
    
    
        if (z->data.key == key)
            break; //找到关键字等于key的结点 *z
        f = z; //*f 为 *p的双亲结点
        if (z->data.key > key)
            z = z->lchild; //在*z 的左子树中继续查找
        else
            z = z->rchild;    //在 *p的右子树中继续查找
    }

    if (!z)return; //找不到待删除的结点;
    /**------------ 考虑3中情况实现p所指子树内部的处理:*p左右子树不为空、无左子树、无右子树 ------------*/
    BSTNode *q = z;
    if (z->lchild && z->rchild) {
    
     //待删除结点*z 左右子树均不为空
        BSTNode *s = z->lchild;
        while (s->rchild) {
    
     //在*p的左子树中继续查找其前驱结点,即最右下结点
            q = s;
            s = s->rchild; //向右到尽头
        }
        z->data = s->data; //s指向待删除结点的前驱
        if (q != z)
            q->rchild = s->lchild; //重接*q的右子树
        else
            q->lchild = s->lchild; //重接*q的左子树
        delete s;
        return;
    } else if (!z->rchild) {
    
    //待删除结点*p无右子树结点,只需重接其左子树
        z = z->lchild;
    } else if (!z->lchild) {
    
    //待删除结点*z 无左子树结点,只需重接其右子树
        z = z->rchild;
    }
    /**------------------------- 将p所指的子树挂接到其双亲结点*f相应的位置 -------------------------*/
    if (!f)  //被删除结点为根结点
        T = z;
    else if (q == f->lchild)
        f->lchild = z; //挂接到*f的左子树位置
    else
        f->rchild = z; //挂接到*f的右子树位置
    delete q;

}

Create operation

The creation operation of the binary sorting tree starts from an empty binary sorting tree. No node is entered. After the search operation, the new node is inserted into the appropriate position of the current binary sorting tree.

Algorithm Description:

① Initialize the binary sorting tree T to an empty tree.
② Read in a node whose keyword is key.
③ If the read keyword key is not the input end mark, the following operations are performed in a loop:

  • Insert these points into the binary sorting tree T;
  • Read in a node whose keyword is key.
void CreatBST(BSTree &T) {
    
    
    T = NULL;
    ElemType e;
    cin >> e;
    while (e.key != '\0') {
    
    
        InsertBST(T, e);
        cin >> e;
    }
}

Search efficiency analysis

Guess you like

Origin blog.csdn.net/QQ657205470/article/details/127427327