A balanced binary tree is called a balanced binary tree. Its strict definition is as follows:
An empty tree is a balanced binary tree. If T is a non-empty Binary Tree, its left and right subtree are TL and TR, make HL and HR respectively the depth of left and right subtree. When and only when
① TL and TR are balanced binary trees;
② | HL-HR | ≤ 1;
T is a balanced binary tree.
(Example) 8.4.
(A) balanced binary tree (B) non-balanced binary tree
Figure 8.3 balanced binary tree and non-balanced binary tree
Correspondingly, HL-HR is defined as the balance factor of the binary balancing tree ). Therefore, the equilibrium factors of all nodes on the balanced binary tree may be-1, 0, 1. In other words, if the absolute value of the balance factor at any node of a binary tree is not greater than 1, the tree is a balanced binary tree.
Dynamic Balancing Technology
1. Dynamic Balancing Technology
Adelson-velskii and Landis propose a method to dynamically maintain the balance of the binary sorting tree. The basic idea is:
During the construction of a binary sorting tree, whenever a knot is inserted, first check whether the balance of the tree is damaged due to insertion. If the balance of the tree is damaged due to the insertion of nodes, then findMinimum imbalance subtreeTo achieve a new balance. The balanced binary sorting tree obtained in this way is generally referred toAVL Tree.
2. Minimum imbalance subtree
Use the node closest to the inserted node and the absolute value of the balance factor greater than 1 as the Child tree of the root node. To simplify the discussion, assume that the root node of the sub-tree with the minimum imbalance of the binary sorting tree is a, then the rule of adjusting the sub-tree can be summarized into the following four situations:
(1) LL type:
The new node X is inserted in the left subtree of the left child of. The adjustment method is shown in Figure 8.5 (). In the figure, node A is centered on Node B and is transferred from the top right of Node B to the bottom right of Node B, making node a right child of Node B.
Figure 8.5 four basic types of balance adjustment (the number next to the node is the balance factor)
(2) rr type:
The new node X is inserted in the right subtree of the right child of. See Figure 8.5 (B ). In the figure, node A is centered on Node B and is transferred from the top left of Node B to the bottom left of Node B, making node A the left child of Node B.
(3) LR type:
The new node X is inserted in the right subtree of the left child of. The adjustment method is shown in Figure 8.5 (c ). There are two steps: the first step is to take X as the axis, and convert B from the top left of X to the bottom left of X, so that B becomes the left child of X, and X becomes the left child of. The second step is the same as the LL type (X should be used as the axis ).
(4) RL:
The new node X is inserted in the left subtree of the right child of. For the adjustment method, see Figure 8.5 (d ). There are two steps: the first step is to take X as the axis, and convert B from the top right of X to the bottom right of X, so that B becomes the right child of X, and X becomes the right child of. The second step is the same as the RR type (X should be used as the axis ).