Advanced Algorithms Analysis and Design
Advanced Algorithms Analysis and Design
Lecture 10
Rotations.
7
-1
1 3
-1 10 1
7
-2 0
1 1 2 3
-1
10 1 13 0
1 2
0
13
AVL Tree
An AVL tree is rebalanced after each insertion or deletion. The height-balance property ensures that the height of an AVL tree with n nodes is O(log n). Searching, insertion, and deletion are all O(log n).
Rebalancing
Suppose the node to be rebalanced is X. There are 4 cases that we might have to fix (two are the mirror images of the other two):
1. An insertion in the left subtree of the left child of X, 2. An insertion in the right subtree of the left child of X, 3. An insertion in the left subtree of the right child of X, or 4. An insertion in the right subtree of the right child of X.
What is a Rotation?
A rotation is a process of switching children and parents among two or three adjacent nodes to restore balance to a tree. An insertion or deletion may cause an imbalance in an AVL tree. The deepest node, which is an ancestor (parent) of a deleted or an inserted node, and whose balance factor has changed to -2 or +2 requires rotation to rebalance the tree. 50 -1
-1 45 0 40 0 35 78 0 -1 40 Deepest unbalanced node
Insert 35
-2 45
-2 50 78 0
50
rotation 78 0
-1 50 0 40 0
35 45 0 78 0
35
There are two kinds of single rotation: Right Rotation. Left Rotation.
A double right-left rotation is a right rotation followed by a left rotation. A double left-right rotation is a left rotation followed by a right rotation.
x y
y x T3
T1
T2 T3 T1 T2
Single Rotation
A single rotation switches the roles of the parent and child while maintaining the search order. Single rotation handles the outside cases (i.e. 1 and 4).i.e
1: An insertion in the left subtree of the left child of X, 4: An insertion in the right subtree of the right child of X.
The result is a binary search tree that satisfies the AVL property.
10
Double Rotation
Single rotation does not fix the inside cases (2 and 3).
2. An insertion in the right subtree of the left child of X, 3. An insertion in the left subtree of the right child of X,
11
Figure 19.25
12
Example : Start with an empty AVL tree and insert the items 3,2,1, and then 4 through 7 in sequential order.
Example
Start with an empty AVL tree and insert the items 3,2,1, and then 4 through 7 in sequential order. Answer:
14
x y z
T1
T2
T3
T4
T1
T2
T3
T4
T2
T3
w
T1
Note: First pivot is the left child of the deepest unbalanced node; second pivot is the deepest unbalanced
T1
T2
T3
T4
T1
T2
T2
T3
Similar
Insertion Analysis
logN
Insert the new key as a new leaf just as in ordinary binary search tree: O(logN) Then trace the path from the new leaf towards the root, for each node x encountered: O(logN) Check height difference: O(1) If satisfies AVL property, proceed to next node: O(1) If not, perform a rotation: O(1) The insertion stops when A rotation is performed Or, weve checked all nodes in the path Time complexity for insertion O(logN)
For each node x encountered, check if heights of left(x) and right(x) differ by at most 1.
If yes, proceed to parent(x)
If no, perform an appropriate rotation at x Continue
Deletion Example 1
20 35 25 40
30
38
45 50
Single Rotation
Contd
20 15 10 18 25 30 35 40 38 45 10 15 18 20 25 30 35 40 38 45 50
Single Rotation
For deletion, after rotation, we need to continue tracing upward to see if AVL-tree property is violated at other node.
Balance by yourself
Solution
NEXT WEEK