preface
- There are two ways to realize data structure, one is sequential storage, the other is chain storage
Sequential storage: the use of a continuous piece of logical address to store data
Chained storage: use a pointer to the address of the next element instead of a continuous address
- Common data structures are: linear table, stack, queue, tree, graph, etc. binary tree belongs to this tree.
Introduction to BST (binary search tree)
BSTIt’s a kind of tree. Its characteristics are
1.LeftThe values of all nodes on the subtree areLess than or equal toThe value of its root node.
2.rightThe values of all nodes on the subtree areGreater than or equal toThe value of its root node.
3. The left and right subtrees are also BST
Advantages of BST:
- BST is usually implemented with chain structure storage, which is either (full binary tree) or close (full binary tree) using sequential storage
- The biggest disadvantage of sequential storage is that the storage space of array must be given in advance. It is troublesome to insert and delete. The advantage is that it can save space and search quickly
The disadvantages of BST are as follows:
- Apart from the sequential storage and chain storage, the disadvantage of BST data structure itself is that at a specific time, the degree of the tree will always increase, resulting in low efficiency of search
At this time, we have a new tree to solve the problem of AVL tree
AVL Tree
AVL tree, in essence, is a BST with balance function. It has more than oneEquilibrium factorconcept
That is (the absolute value (balance factor) of the height difference between the left and right subtrees of each node is at most 1), if it is not satisfied, it will rotate to meet this condition.
The above figure 1 shows that after the BST is rotated in AVL, the specific rotation can be simulated in this website
https://www.cs.usfca.edu/~gal…
So what are the disadvantages of AVL?
The biggest drawback is the pursuit of perfect balance, which results in a large amount of balanced calculation for insertion and deletion. In this case, the cost is large. At this time, we wonder whether there is a tree to solve the shortcomings of BST, and at the same time, do not need a lot of calculation balance. So RB tree was invented
Difference between Rb tree and AVL
- The definition of Rb tree will not be introduced. We will just remember the difference
In the worst case, AVL needs to maintain the balance of all nodes in the path from the deleted node to the root, while the RB tree only needs three rotations at most.
The figure below shows the solution to the imbalance of Rb tree in Figure 1