Index of binary search tree, balanced binary tree

Binary search tree (binary search)

It is not suitable for indexing, because when the data in the database changes (delete/new nodes), it may become a linear table, so the search efficiency will be very low.
Binary search tree
Delete and add nodes

Balanced binary tree

1: Each node can only have two children at most.
2: The value of the left subtree is less than the key value of the root node, and the key value of the right subtree is greater than the key value of the root node.
3: Left subtree and right subtree of any node 高度差的绝对值不超过1(characteristics of balanced binary tree)
Balanced binary tree
Unbalanced binary tree

Balanced binary tree for insert operations may cause loss of balance

LL (left and left insertion loses balance):
Left and left out of balance (high on the left, low on the right) We need to use the left child of the root node as the new root node to rotate. Get a new tree node.
Insert picture description here
LR (Insert the node left and right, causing the tree to lose balance): Inserting into the right child of the left child of the follow node causes the tree to lose balance.
LR (Insert nodes left and right, causing the tree to lose balance)
Two rotations are required: the first rotation is 4 6, and the 6 is used as the follow node. The
Insert picture description here
second rotation is 6 8 rotation , Use 6 as the follow node as a
Insert picture description here
balanced binary tree. Although it solves the shortcomings of a tree that is easy to become a linear table ( through rotation ) when adding or deleting nodes in the binary tree, it is still a binary tree. When the number of nodes is large When, the tree level will become very deep and deep, and recursive query is very time-consuming.

Guess you like

Origin blog.csdn.net/eluanshi12/article/details/87259377