Ch15: AVL Tree
305233, 305234 Algorithm Analysis and Design
Jiraporn PooksookNaresuan University
Review: Binary Search Tree
• Rooted binary tree
• Each node has– Key
– Left pointer
41
20 65
Left pointer
– Right pointer
– Parent pointer
• Binary search tree property– Let x be a node in a binary search tree. If y is a node
in the left subtree of x, then key[y] ≤ key[x]. If y is a node in the right subtree of x, then key[x] ≤ key[y].
11 29 50
26
Review: Binary Search Tree
• Binary search tree supports
– Insert
– Delete
– Minimum– Minimum
– Maximum
– Successor
• In O(h) time where h is the height of a binary search tree
Height Balanced
15
6 20
3 9 25
Height = lg n
17
2 4 137 16 18 23 29
Height Unbalanced
15
20
25
Height = n
29
Height
• Height of a tree is the length of longest path of the root down to a leaf.
• Height of a node is the length of longest path of it node down to a leaf.of it node down to a leaf.
Example: Height
41
20 65
2 1
3
-1
11 29 50
26
0
10 0
Height of node = max {height of left child , height of right child}
-1 -1
-1
-1-1
-1
-1
-1
AVL Tree
• An AVL tree is a binary search tree that is height balanced.
• For each node x, the heights of the left and right subtrees of x differ by at most 1.right subtrees of x differ by at most 1.
• An AVL tree with n nodes has height O(lg n).
x
|hr - hr | ≤ 1hr hl
AVL Tree
• AVL trees are balanced.
• The worst case is when the right subtree has height 1 more than left for every node.
1 1
Analyze the Height of AVL Tree
• Let Nh to be a minimum number of nodes in an AVL tree of height h.
• Nh = 1 + Nh-1 + Nh-2
• Nh > Fh where Fh is a Fibonacci function• Nh > Fh where Fh is a Fibonacci function
• Nh > 1.618h /51/2
• Denote Nh = n
• 1.618h /51/2 < n
• h < 1.440 lg n h-1h-2
h
Height of AVL tree = lg n
AVL Insert
• Use a simple binary search tree insert
• Fix the AVL property using rotations
Example: AVL Insert(T,23)
41
20 65
3 1
4
-1
11 29
26
1
20
-1
-1
-1-1
23
0
The Tree is unbalanced !!!
50
0
-1
-1
-1
Rotations
x
y x
y
Left Rotate(T,x)
Right Rotate(T,y)
A
y
B C
x
A B
C
Left-Rotate(T,x)y = right[x]right[x] = left[y]if left[y] != nil[T]
then p[left[y]] = xp[y] = p[x]p[y] = p[x]if p[x]= nil[T]
then root[T] = yelse if x = left[p[x]]
then left[p[x]] = yelse right[p[x]] = yleft[y] =xp[x] = y
Example: Left-Rotate(T,29)
41
20 65
x
11 2950
32
36
y
x
Example: Left-Rotate(T,29)
41
20 65
x
11 29 50
32
36
y
x
Example: Left-Rotate(T,29)
41
20 65
x
11 2950
32
36
y
x
Example: Left-Rotate(T,29)
41
20 65
y
11
29
5032
36
y
x
Example: Left-Rotate(T,29)
41
20 65
y
11
29
5032
36
y
x
Example: Right-Rotate(T,29)
41
20 65
11 29 50
26
23
Example: Right-Rotate(T,29)
41
20 65
11 26 50
23 29
Example: AVL Insert(T,23)
41
20 65
3 1
4
-1
11 29
26
1
20
-1
-1
-1-1
23
0
Right Rotate(T,29)
50
0
-1
-1
-1
Example: AVL Insert(T,23)
41
20 65
2 1
3
-1
11
2923
0
10
-1
-1-1
26
-1 -1-1
0
50
0
-1
-1
-1
Example: AVL Insert(T,55)
41
20 65
2
1
2
3
11
29
50
23
0
101
-1
-1-1 -1
26
-1 -1-1
0
55
The Tree is unbalanced !!!
Example: AVL Insert(T,55)
41
20 65
2
1
2
3
x
y
11
29
50
23
0
101
-1
-1-1 -1
26
-1 -1-1
0
55
Right Rotate(T,65)
x
Example: AVL Insert(T,55)
41
20 50
2
1
2
3
11
29
65
23
0
101
-1
-1-1
26
-1 -1-1
0
55
-1
The Tree is unbalanced !!!
Example: AVL Insert(T,55)
41
20 65
2
1
2
3
x
11
29
50
23
0
101
-1
-1-1 -1
26
-1 -1-1
0
55
Left-Rotate(T,50)
x
y
Example: AVL Insert(T,55)
41
20 65
2
1
2
3
y
11
29
55
23
0
101
-1
-1-1
26
-1 -1-1
0
50
The Tree is unbalanced !!!Right-Rotate(T,65)
x
Example: AVL Insert(T,55)
41
20
2
0
1
3
55
0
6511
29
50
23
0
100
-1
-1-1
26
-1 -1-1
0
0
-1-1
AVL Insert
• Use a simple binary search tree insert
• Fix the AVL property from changed node up
– Suppose x is lowest node violating AVL property
AVL Insert
• Assume the right child of x is higher
• If the right child of x is right-heavy or balanced, then we do right-rotate(x)
k-1
x
A
y
BC
x
A
y
B
C
Right Rotate(T,x)
k
k-1
k-2k-3
k-3
k-3 k-3
k-2
k-1
k-2
AVL Insert
• Assume the right child of x is higher
• Else
xx
Right-Rotate(T,z)x
A
y
B C
A
y
B
C
z
Dz
D
AVL Insert
• Assume the right child of x is higher
• Else
y
Left-Rotate(T,x)x
x
A
y
B C
z
D
A
y
B
C
z
D
AVL Insert
• Assume the right child of x is higher
• Else
x y
Right-Rotate(T,z)Left-Rotate(T,x)x
A
y
B C
x
A
y
B C
Left-Rotate(T,x)
z
D
z
D
AVL Sort
• Insert n items
– Take O(n lg n) time
• In-order tree traversal
– Take O(n) time– Take O(n) time
Summary
• Abstract Data Types Data Structure
• Insert , Delete ●Heap or AVL
• Min ● Balanced BST
• Successor/ predecessor
Priority queue
• Successor/ predecessor
Practice: AVL Insert
4 7 5 10 23 65 73