Find - balanced binary tree

First, the definition

The time complexity of binary sort tree is O (n), the time complexity balanced binary sort tree is O (logn)

AVL balanced binary tree is also called, it is a special binary sort tree, has a higher query efficiency. Or a balanced binary tree empty, or to meet the following properties of a non-null binary tree T:

  1. The absolute value of the difference between the depth of the left subtree of T and the right subtree of not more than 1;
  2. T left subtree and right subtree are balanced binary tree.

Balanced binary tree node T factor BF (Balance Factor) is a left subtree depth minus depth of right subtree, species


Second, the idea of ​​the algorithm

In the process of establishing a balanced binary tree, each insert a new keyword should be checked to see if the new keyword will be inserted so that the original balanced binary tree out of balance, i.e. the balance factor appears in the tree node is greater than the absolute value of 1. If you need to be out of balance balance adjustment.


Third, balance adjustment

  • When out of balance is adjusted to a minimal tree balanced sub-tree without adjusting the existing imbalance in all other sub-tree, the entire binary sort tree will become a balanced binary tree.

  • Minimum unbalanced subtree: a distance nearest the insertion node, and with an absolute value greater than the balance factor as a node subtree rooted.

(1) LL Balanced rotation method

The insertion point is left subtree subtree smallest imbalance Zhizuo sub-tree.

Since node F is inserted in the left subtree of left child of A, B, A the equilibrium factor increased by 1 and 2 out of balance. Therefore, the need for a clockwise rotation operation. A rotation instead of as root on the left child B of A is about right, A right to be at the root of rotation B of the right subtree. And the right subtree of the original B A becomes the left subtree.
Here Insert Picture Description

(2) RR Balanced rotation method

The insertion point is a right subtree of the right hand subtree of the subtrees minimum imbalance.

Since node F is inserted in the right subtree of the right child of A, C, so that the balance factor A is reduced by -1 -2 lose balance. Therefore, the need for a counterclockwise rotating operation. A rotation instead of coming on as a root left and right child C of A, A rotating left to become the root of the left subtree C. The left subtree of the original C becomes A's right subtree.

Here Insert Picture Description

(3) LR Balanced rotation method

The insertion point is the smallest sub-tree unbalanced left subtree right hand subtree.

Since the insertion node F on the right and left of the number of children of A, B, A the equilibrium factor increased by 1 and 2 out of balance. Therefore, the need for two rotating operation (first counter, after clockwise).

  1. D left on the root of the right subtree of the first B A left child node promoted to the rotational position of the node B
  2. The node D then put on to the right to position A rotary lift node. That is, first making LL type, press the LL type process.

Here Insert Picture Description
Wherein the adjustment of intermediate states as follows (corresponding to a step of: the right subtree of B left child of the root node A D raised to the rotational position of the left node B)
Here Insert Picture Description

(4) RL Balanced rotation method

The insertion point is a right subtree Zhizuo minimal subtree of the subtrees imbalance.

Since node F is inserted in the left subtree of the right child of A, C, so that the balance factor A is reduced by -1 -2 lose balance. Therefore, the need for two rotating operation (first clockwise, then counterclockwise).

  1. D left subtree root node A first right child node C to the right to lift the rotational position of the node C
  2. The node D and then to the left to lift the rotational position of the A node. Which is to make it a type RR, then type RR process.

Here Insert Picture Description

Wherein the adjustment of intermediate states as follows (corresponding to a step: a left subtree root D C of the right child node A to the right to lift the rotational position of the node C)

Here Insert Picture Description

Guess you like

Origin blog.csdn.net/starter_____/article/details/93875517