







Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!








If a BST is balanced, one can do insertion, search and deletion in O(log n) time. But if it is skewed, then these operations could take O(n) time. Thus when n is large, it is always of interest to keep the tree balanced as far as possible. One of the first tree balancing algorithms was suggested by two Russian scientists Adel’son-Vel’skii and Landis. Such balanced trees are known by their initials as AVL trees.
An AVL tree is an almost balanced tree. The heights of the left sub tree and the right subtree determine whether the tree is balanced or not.
The height of a node is the longest path length, including itself.
In the following tree the heights of the nodes are indicated below:
Nodes 40, 75, 90 : Height 1 Nodes 60, 80 Height 2 Node 70 : Height 3.
A new term is introduced now, called the Balance factor.
The Balance Factor (B.F.) of any node of a Binary search tree, is the difference between the height of right child node (HR) and the height of the left child node (HL).
B.F. = HR – HL
A Height balanced tree (an AVL tree), is one which is either empty, or each node of the tree has a balance factor of 0, -1 or 1.
Balance factor BF for 12 and 25 is -1, but for 56 the BF is -2. So the tree is unbalanced. The balance has been disturbed by insertion of a node in the left subtree of the left child
A right rotation of the tree around the node 56, causes node 25 to move up and 56 to move down the tree. To maintain the Binary search tree property, the right subtree of 25 needs to be attached to node 56. The tree is now balanced and is shown below. Node 56 also takes care of node 38.
56
25
72
12
38
8
Now consider the general LL case, where P and Q are nodes, and T 1 , T2 , T 3 are sub trees. Let us say insertion of a node in T 1 causes the tree to be unbalanced.
Note that the BF of the nodes in the unbalanced tree are either monotonically increasing or decreasing (NO change in sign). The tree can be balanced by (right rotation) moving P down, and moving Q up. The subtrees are taken care in the following way:
P
Q T 3
T 1
T 2
The tree can be balanced by a left rotation which causes node 60 to move down and node 80 to move up, as shown
General case: Consider a general case where the tree becomes unbalanced by inserting a node in T 3.
80
60
90
40
70 98
60
40
80
70 90
98
Such a tree is balanced by moving up Q and moving down P (Left rotation). The subtrees are taken care of as follows:
Case RL:
This is a case where the tree gets unbalanced after a node is added to the right subtree of the left child. Let us check the balance factor in the following tree after node 45 is attached to right subtree of the left node25,. Here node 12 has zero BF, node 25 has BF
P
T 1
Q
T 2 T 3
Q
P T 3
T 1
T 2
The tree is still unbalanced, but the nodes along the path to the root have the Balance Factors as -1, -1, and -2 (the BF have the same sign). This means a right rotation would be able to balance the tree, as it is similar to the LL case. The right rotation results in the following balanced tree:
56
38
72
25
45
12
Let us now consider the general case, of a tree with nodes P, Q and R with 4 subtrees. The tree gets unbalanced after insertion of a node in T 2 or T 3.
The tree can be balanced by a double rotation. The final effect of the double rotation is shown below. Note that in this case the node R moves up, with nodes P and Q as its children and the subtrees are taken care of in the following way. Also note that the left to right ordering of subtrees T 1 T 2 T 3 T 4 is not disturbed by the balancing process.
P
Q
T 4
R
T 1 T 3 T 2
The tree is balanced by moving 70 up .Node 60 retains its left subtree, and node 80 retains its right subtree. The left and right subtrees of the root node 70 are taken care of by nodes 60 and 80.