AVL TREES UNIT 3
AVL TREES UNIT 3
Syllabus
Structural properties
1. Binary tree property (same as BST)
2. Order property (same as for BST)
3. Balance condition:
balance of every node is between -1 and 1
Balance Condition:
balance of every node is between -1 and 1 6
where balance(node) =
height(node.left) – height(node.right)
4 8
1 7 11
10 12
Example #2: Is this an AVL Tree?
6
Balance Condition:
balance of every node is between -1 and 1
where balance(node) = 4 8
height(node.left) – height(node.right)
1 5 7 11
2
AVL Trees
Good News:
Because height of AVL tree is O(log(n)), then find
3
10
2 2
5 20
0 1 1 0
2 9 15 30
0 0
7 17
An AVL Tree
3 10 data
10 3 height
children
1 2
5 15
0 0 0 1
2 9 12 20
0 0
17 30
Not AVL Trees
3 0-2 = -2
10 2 (-1)-1 = -2
10
0 2
5 15 1
15
0 1
12 20 0
20
0 0
17 30
■ AVL insert:
AVL delete:
2. For each node on the path from the root to the new leaf,
the insertion may (or may not) have changed the node’s
height
■ There are four scenarios where a rotation might be required when the tree is unbalanced( ie when height
difference is more than 1 )at node α
■ Case A : Insertion into the left sub-tree of left node of α (Single right rotation)
■ Case B : Insertion into the right sub-tree of right node of α (Single left rotation)
■ Case C : Insertion into the right sub-tree of left node of α (Double rotation)
■ Case D : Insertion into the left sub-tree of right node of α (Double rotation)
Generalizing the examples…
12
5 23
2 9 18 30
1 4 7 10
Generalizing our examples…
12
5 23
2 9 18 30
1 4 7 10
Generalizing our examples…
12
A C
Case A : Single Right rotation
■ Two nodes k2 and k1 are shown in the figure, here k2 is the root
node (and also the a node, k1 is its left child and Z shown in the
triangle is its right child. The nodes X and Y are the left and right
sub-trees of the node K1)A new node is inserted at left subtree of
K1and because of this the tree becomes unbalance at K2 The k2
node has been rotated single time towards right to become the
right child of k1 and Y has become the left child of k2
The sub routine to do right rotation is
/**
* Rotate binary tree node with left child.
* For AVL trees, this is a single rotation for case 1.
* Update heights, then return new root.
*/
private static AvlNode rotateWithLeftChild( AvlNode k2 )
{
AvlNode k1 = k2.left;
k2.left = k1.right;
k1.right = k2;
k2.height = max( height( k2.left ), height( k2.right ) ) + 1;
k1.height = max( height( k1.left ), k2.height ) + 1;
return k1;
}
15
8 22
4 10 19 24
3 6 17 20
16
Case B: Single Left Rotation
■ The new node has been inserted as a child node of Z, One
rotation towards should make it balanced within limits of AVL
tree. The figure on the right is after rotation the node k1 one
time towards left. This time node Y has become the right child
node of the node k1
* Rotate binary tree node with right child.
* For AVL trees, this is a single rotation for case 4.
* Update heights, then return new root.
*/
private static AvlNode rotateWithRightChild( AvlNode k1 )
{
AvlNode k2 = k1.right;
k1.right = k2.left;
k2.left = k1;
k1.height = max( height( k1.left ), height( k1.right ) ) + 1;
k2.height = max( height( k2.right ), k1.height ) + 1;
return k2;
}
Example for right-right case:
insert(26)
15
15
8 24
8 22
24 4 10 22 25
4 10 19
3 6 3 6 19 23
23 25 26
26
Case C : LEFT Right Double rotation
■ The new node is inserted in the right sub-tree of left
child . ie the left child of node α is k1 and its right
sub-tree is node k2. Because of insertion the
balance is lost at k3 so there are two single rotations
performed