[soft test] data structure - tree structure - binary search tree (Binary Search Tree, BST)

1. What is a BST binary search tree?

BST (Binary Search Tree) is a binary search tree
BST binary search tree is an ordered, tree-shaped data structure

2. The nature of BST binary search tree

BST binary search tree has the following properties:

  • Each node has 0 or 2 children (up to two children).
  • For each node,
    • The value of all nodes of its left subtree is less than the value of the node (the value of the left child node is less than the value of the parent node)
    • And the value of all nodes of the right subtree is greater than the value of the node (the value of the right child node is greater than the value of the parent node).

3. The time complexity of BST binary search tree

The structure/characteristic of the BST binary search tree makes it possible to find an element in the tree with good performance

  • In BST, the average time complexity is O(log n).
  • In BST, the time complexity of lookup, insertion and deletion is O(log n), where n is the number of nodes in the tree.

4. Application of BST binary search tree

BST binary search tree is used to efficiently find, insert and delete data.

BST binary search tree can be used to implement many data structures and algorithms, such as:

4.1 Ordered Sets

When elements are inserted into the BST, they are sorted according to their values, which
can implement common operations on ordered sets, such as search, insert, delete, and so on.

4.2 Binary search tree

BST is a binary search tree,
so it can be used to implement common operations of binary search trees, such as search, insert, delete, etc.

4.3 Hash table

Using each node of the BST as an element of the hash table
can achieve fast lookup and insertion operations.

4.4 Minimum heap

Comparing the value of each node of the BST with the value of its child nodes, and placing the minimum value on the root node, the
common operations of the minimum heap can be realized, such as search, insertion, deletion, etc.

5. The construction process of BST

The construction process of BST is as follows:

The root node is the first value to find.
If the current node's value is equal to the value being looked up, return that node.
If the value to be found is less than the value of the current node, continue searching in the left subtree.
If the value to be found is greater than the value of the current node, continue searching in the right subtree.
If the current node is a leaf node and the value you are looking for is not in the tree, return an empty node or a special flag indicating not found.

Six, BST insertion process

The insertion process of BST is as follows:

Traverse the BST from the root node to find the position to be inserted.
If the value of the current node is equal to the value to be inserted, return the node directly.
If the value to be inserted is less than the value of the current node, continue searching in the left subtree.
If the value to be inserted is greater than the value of the current node, continue searching in the right subtree.
Find an empty location, insert a new node into that location, and update the pointer of the parent node.

7. BST deletion process

The deletion process of BST is as follows:

Traverse the BST starting from the root node and find the node to be deleted.
If the node to be deleted has no children, just delete it from the tree.
If the node to be deleted has only one child node, connect its child node to its parent node.
If the node to be deleted has two child nodes, find the predecessor node (that is, the maximum value in the left subtree) or the successor node (that is, the minimum value in the right subtree) of the node, and use it to replace the position of the node to be deleted, And connect its child nodes to predecessor nodes or successor nodes.

Guess you like

Origin blog.csdn.net/wstever/article/details/129978040