AVL tree of rotation (balanced binary tree)

concept

Binary search tree concept: https://blog.csdn.net/weixin_43796685/article/details/104345502
AVL tree Code:
https://github.com/DHaoYu/C-Cplusplus/tree/master/C%2B%2B /% E4% BA% 8C%
E5% 8F% 89% E6% A0% 91 /% E4% BA% 8C% E5% 8F% 89% E6% A0% 91 since the binary search tree degenerate into single-branched tree (single after the list), find elements of time complexity becomes O (N), in practical application, will obviously affect the efficiency, therefore, in order to solve this problem, the introduction of a balancing factor in the concept of a binary search tree in binary search tree unbalanced when it is adjusted to balance the state.

So what nature has AVL tree
  • The first is a binary search tree
  • His left and right subtrees are AVL tree
  • Left and right subtrees height difference (balance factor) does not exceed an absolute value of (<= 1)
  • It can be guaranteed: The time complexity of the search is O (logN)

Balance factor

Within each node of the binary tree maintains a balanced integer element bf, called balance factor. And generally defined as: balance factor is negative left high, high right balance factor is positive.
Below, the above value of the node is the balance factor.
Here Insert Picture Description

After insertion node, to adjust the balance factor three cases

First, after the insertion node, the node need to first insert the parent node is adjusted,

  • The new node in the left and subtract operation, parent of the bf parent.
  • The new node's parent jerk operate on the right side, parent of bf.
    After the parent balance factor adjustment completion, parent balance factor divided into three cases.
    I. : parent balance factor is 0, indicating a positive and negative prior to insertion, after insertion, becomes 0, can ensure that the whole tree is balanced, to be directly out of the loop.
    Here Insert Picture Description
    Case 2 : parent balance factor as a positive and negative, indicating that, after the insertion node, the balance being the father node changes, in order to make the entire AVL tree of the tree is to be adjusted upwards. (Regarded as the father node node is inserted, the grandfather node regarded as the father nodes, until adjust to the root)
    Here Insert Picture Description
    I. and Case 2 can also be understood, the following look at the case of a three
Case three: the need for left-handed, right-handed, ambidextrous left, right-left ambidextrous

The third situation is the most complex case, after the insertion node, the value of the father node to the balance factor of two positive and negative values.
Here Insert Picture Description
As shown in the figure is one kind of the third case. You need to be rotated to operate.

L

The first step: rotation
Here Insert Picture Description
Step two: Change the value of the equilibrium factor
Here Insert Picture Description

Right-handed

The same right-handed and left-handed mode, rotating the first step, the second step to change the balance factor
Here Insert Picture Description
The above two methods, the new nodes are inserted on the same side of the higher subtree (left subtree higher, the insertion position of the left, right subtree higher, the insertion position of the right side)

About two-spin

This rotation is mainly directed to high insertion subtree opposite side (left subtree higher, the higher the left into the right subtree)
The main idea is ambidextrous, the nodes on the opposite side of the first adjusted to the same side, so that you can be a single screw.
Here Insert Picture Description

Right and left-handed twin

The main idea is still adjusted to the scene of a single spin, spin using a single scene, to be updated.
Here Insert Picture Description

Published 52 original articles · won praise 26 · views 3395

Guess you like

Origin blog.csdn.net/weixin_43796685/article/details/104621192