Balanced Binary Tree AVL Tree

Definition of balanced binary tree

Balanced binary tree is also known as: Self-Balancing Binary Search Tree or
Height-Balanced Binary Search Tree: a highly balanced binary sort tree.
Balanced binary tree : It is a kind of binary sorting tree. The height difference between the left subtree and the right subtree of each node is less than or equal to one.
Two Russian mathematicians, GM Adelson-Velskii and EM Landis , jointly invented an algorithm to solve a balanced binary tree in 1962. Such a balanced binary tree is also called an AVL tree.

  1. Simply analyze whether the following binary tree is a balanced binary tree (AVL tree)
    Insert picture description here
    . Figure 1 meets the requirements of sorting (intermediate traversal: 35 47 58 62 88 93 99); but the absolute value of the height difference between the left and right subtrees of the second layer 58 88 nodes is greater than 1. . So Figure 1 is not an AVL tree.
    Figure 2 satisfies that the height difference between the left and right subtrees of each node is less than or equal to 1, but does not meet the sorting requirements, 59 is greater than 58 nodes; therefore, it is not an AVL tree either.
    Figure 3 satisfies the sorting requirements; but the height of the left subtree of node 58 is 2, and the height of the right subtree is 0; therefore, it is not a balanced binary tree.
    Figure 4 satisfies the sorting requirements and also satisfies the condition that the height difference between the left and right subtrees of each node is less than one. So it is a balanced binary tree or AVL tree.

Minimum unbalanced subtree

Minimum unbalanced subtree : The subtree that is rooted at the node closest to the inserted node and whose absolute value of the balance factor is greater than 1 , is the minimum unbalanced subtree. For example, when 37 is inserted, the 58 node is the closest node with a balance factor greater than 1; then, the subtree rooted at 58 is the smallest unbalanced subtree.

Insert picture description here

The realization principle of balanced binary tree

The realization principle of balanced binary tree: The idea of ​​balanced binary tree construction is that whenever a node is inserted, first check whether the balance of the tree is damaged by the insertion, and if so, find the minimum balanced tree. Under the condition of maintaining the characteristics of the binary sort tree, the link relationship between the nodes in the minimum unbalanced tree is adjusted and rotated to make it a new balanced subtree.

Demonstration : The operation realizes the binary balanced tree of array a[10]= [3,2,1,4,5,6,7,10,9,8]:
First, insert a node into the tree and record the data of each node Balance factor.
Balance factor = the depth of the left subtree-the depth of the right subtree. The depth is equal to the number of layers of the tree.
When the balance factor is greater than 1, adjust the rotation of the node of the least unbalanced subtree.
Insert picture description here
Insert node 1, the balance factor of node 3 is 2>1, adjust the entire subtree to right-handed; insert node 4;
Insert picture description here
insert node 5, the balance factor of the nearest 3 nodes is -2, the absolute value is greater than 1. The entire node 3 subtree Left-handed.
Insert picture description here
Insert node 6, the balance factor of node 2 is -2, the whole tree is the smallest unbalanced subtree, left-handed, 4 nodes become the root, the left subtree of 4 becomes the right subtree of node 2, and the balance is reached;
Insert picture description here
insert node 7. The balance factor of node 5 is -2, and the minimum unbalanced subtree is left-handed; insert node 10;
Insert picture description here
insert node 9; node 7 is unbalanced, and it should be left-handed at this time, but 9<10 cannot be the right node of 10;
through observation, Insert the node before, simply left-handed and right-handed is because the subtree root and the left and right children have the same sign of the balance factor. Therefore, the symbols of the balance factor need to be adjusted to be consistent here. Rotate the 10 and 9 nodes to the right.
Insert picture description here
Then rotate the minimum unbalanced tree to the left; insert node 8;
Insert picture description here
at this time, it is necessary to rotate the 6-node subtree to the left, but the balance factor of node 9 is inconsistent with the sign of node 6, and rotate the node 9 subtree to the right.
Insert picture description here
Then rotate the node 6 subtree to the left.
In this way, the construction of a balanced binary tree is completed.

Algorithm implementation

This is not omitted, waiting for the update! !

references

[1] "Dahua Data Structure" Cheng Jie[M].

Guess you like

Origin blog.csdn.net/beauthy/article/details/105578056