What is a binary sort tree, the advantages of search analysis and detailed description of the generation, search, insert, and delete operations

Binary Sort Tree

1 Introduction

When table insertion and deletion operations are frequent, many records in the table need to be moved in order to maintain the orderliness of the table.

改用动态查找表————几种特殊的树

The table structure is dynamically generated during the lookup process

For a given value key, if it exists in the table, it will return successfully;

Otherwise, insert the record whose key is equal to key

二叉排序树 平衡二叉树 红黑树 B-树 B+树 键树

2. Definition

二叉排序树(Binary Sort Tree)Also known as binary search tree, binary search tree

A binary sort tree is either an empty tree, or a binary tree that satisfies the following properties:

  1. If its left subtree is not empty, the values ​​of all nodes on the left subtree are less than the value of the root node;
  2. If the right subtree is not empty, the values ​​of all nodes on the right subtree are greater than or equal to the value of the root node;
  3. The left and right subtrees are each a binary sort tree
  4. Features: The middle-order traversal sequence is an increasing sequence 3. The law of the middle-order traversal binary sorting tree (the nature of the binary sorting tree)

The sequence of data elements obtained by traversing the non-empty binary sort tree in middle order is an increasing ordered sequence arranged by key

4. The operation of the binary sort tree-search

  • If the searched keyword is equal to the root node, success
  • otherwise
    • If it is smaller than the root node, check its left subtree
    • If it is greater than the root node, check its right subtree
  • The operation on the left and right subtrees is similar

5. Storage structure of binary sort tree

typedef struct{
    KeyType key;  //关键字项
    InfoType otherinfo; //其他数据域
}ElemType;

typedef struct BSTNode{
    ElemType data;   //数据域
    struct BSTNode *lchild, *rchild ;  //左右孩子指针
}BSTNode,*BSTree;

BSTree T; //定义二叉排序树T

6. Recursive search of binary sort tree

算法思想

  1. If the binary sort tree is empty, the search fails and a null pointer is returned

  2. If the binary sort tree is not empty, combine the given value key with the key of the root node

    T–>data.key for comparison:

    1. If key is equal to T->data.key, the search is successful and the root node address is returned;
    2. If key is less than T->data.key, further search the left subtree;
    3. If key is greater than T->data.key, further search the right subtree;

算法描述

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);//在右子树中继续查找
    }
}//SearchBST

7. Search and analysis of binary sort tree

The process of finding a node whose key is equal to a given value on the binary sort tree is actually a path from following to the node.

The number of compared keywords = the number of layers where this node is located

The most number of comparisons = the depth of the tree

Average search length of binary sort tree

The higher the height of the tree, the longer the average search length (the height of the tree does not exceed lg2(n+1))

The average search length of a binary sort tree with n nodes is related to the shape of the tree

The best case: ASL=log2(n+1) -1;

​ The depth of the tree is: log2 n times (to integer) + 1

​ Same as the decision tree in this search

Worst case: the inserted n elements are in order from the beginning

​ -----In the form of a single tree!

​ At this time, the depth of the tree is n, ASL = (n+1)/2

​ The search efficiency is the same as the sequential search: O(n)

Question: How to improve the search efficiency of a binary sort tree with uneven morphology?

Solution: Do "balance" processing, that is, try to make the shape of the binary tree balanced!

8. Operation of Binary Sort Tree

8.1. Insert

method:

  • If the binary tree is empty, insert the node as the root node into the empty tree
  • Otherwise, continue to search on the left and right subtrees
    • Already in the tree, not inserted
    • Not in the tree
      • Search until the left or right subtree of a leaf node is empty, then the inserted node should be the left child or right child of the leaf node

插入的元素一定在叶节点上

8.2, Generate

Starting from the empty tree, after a series of search and insertion operations, a binary sort tree can be generated.

For example: Let the search keyword sequence be {45,24,53,45,12,24,90}

​ 45

​ 24 53

​ 12 90

总结:

  • ​ An unordered sequence can be transformed into an ordered sequence by constructing a binary sorting tree (in-order sorting). The process of constructing a tree is a process of sorting unnecessary sequences
  • The inserted nodes are all leaf nodes, so there is no need to move other nodes. It is equivalent to inserting record two on the ordered sequence without moving other records
  • The input order of the keywords is different, and a different binary sort tree will be established

8.3, delete

​ To delete a node from the binary sort tree, you cannot delete all the subtrees rooted at the node, you can only delete the node, and you should also ensure that the binary tree obtained after the deletion still satisfies the binary sort tree The nature of

​ Since the middle order traverses the binary sort tree, an ascending sequence can be obtained. Then deleting a node in the binary sort tree is equivalent to deleting a node in the ordered sequence

  • Reconnect the binary linked list that was broken by deleting the node
  • Prevent the height of the tree from increasing after reconnection (the height determines the search efficiency of the binary sort tree)
  1. The deleted node is a leaf node: delete the node directly, modify the value of the corresponding pointer field in its parent node to "empty"

  2. The deleted node has only the left subtree or only the right subtree, replace it with its left subtree or right subtree (node ​​replacement)

    The value of the corresponding pointer field of the parent node is changed to: "point to the left subtree or right subtree of the deleted node"

  3. The deleted node has both a left subtree and a right subtree

    ​ 50

    ​ 30 80

    ​ 20 40 90

    ​ 35 85

    ​ 32 88

    Deleted keywords = 50

    • Replace it with its predecessor value, (value replacement) and then delete the predecessor node.前驱结点是左子树中最大的结点
    • You can also replace it with its successor, and then delete the successor node,后继是左子树中最小的结点

9. Advantages of Binary Sort Tree

When inserting a new node in a binary sort tree, the new node is always added to the tree in the form of a leaf node.
The insertion of a new node has no effect on the position of the existing node and the original shape of the tree.
If the binary sort tree is regarded as an ordered sequence, it is equivalent to inserting new elements in an ordered sequence, and will not affect existing elements.
Contrast: In an ordered sequence where a binary search is performed, inserting a new element will cause all elements after the insertion position to move backward.

Textbook focus

1. The nature of binary sort tree

  • If the left subtree of the root node is not empty, then all the node keys on the left subtree are less than the root node key
  • If the right subtree of the root node is not empty, then all the node keys on the right subtree are greater than the root node key
  • The left and right subtrees of the root node are each a binary sort tree
  • The in-order sequence obtained by traversing the tree in the middle-order is an increasing ordered sequence

That is to say, the binary sort tree is based on the binary tree with the constraint of the node value added.

supplement:

Each data is compared from the root node downward

After the data is more playful, it is to insert the position accurately and thoughtfully, and then insert it as a leaf.

2. Key points

The smaller the height, the higher the search efficiency of the binary sort tree

Keyword comparisons do not exceed the height of the tree

Binary sort tree search

The average search length performed on the binary sort tree is related to the shape of the binary sort tree

  • Best case (n+1)/2
  • In the worst case, the logarithm of n is the base 2. The shape of the tree is relatively uniform, and the final result is a BST with a shape similar to the decision tree of the binary search.

Summary: As far as the average time performance is concerned, the search on the binary sort tree is similar to the binary search, but in terms of maintaining the orderliness of the table, the binary sort tree is more effective because there is no need to move elements, just modify the pointer. Insertion and deletion of nodes

Maximum node problem

The largest node in a binary sort tree is the lower-right node of the root node, and the smallest node is the lower-left node of the root node

Guess you like

Origin blog.csdn.net/weixin_46195957/article/details/111570080