Download presentation
Presentation is loading. Please wait.
Published bySilas Ray Modified over 6 years ago
1
Lecture 15 AVL Trees Slides modified from © 2010 Goodrich, Tamassia & by Prof. Naveen Garg’s Lectures
2
Linked List Priority Queue Stack Heap Queue BST Hash Table Ordered Trees
3
Most map operations on a BST take O(h)!
4
How to fix an imbalanced BST?
1 3 4 5 2 7 6
5
Rotation Keys(T1) < key (v) < keys(T2)<key(u) < keys (T3)
6
Rotation Keys(T1) < key (v) < keys(T2)<key(u) < keys (T3)
7
Apply Rotations Iteratively
rotate right (6,7), (5,6), and (4,5) rotate right (2,3) rotate left (6,5)
8
Complete BST 1 3 2 4 5 7 6
9
Adelson-Velsky and Landis
10
Rotate while Inserting Itself (7, 6, 5 4, 3, 2, 1)
11
-if left and right subtrees can differ in height by at most 1
height still remains O(log n) restructuring is easy and O(log n) insert and delete
12
AVL Trees
13
Height-Balanced Binary Search Trees
14
Height Balance Property!
For every internal node v of T, the heights of the children of v can differ by at most 1!
15
Examples 1 3 2 4 5 7 6
16
Examples 14 10 16 8 11 15 17 5
17
Examples 14 10 16 8 11 15 17 5 13
18
Examples 14 10 16 8 11 20 5 17
19
Examples 44 17 78 32 50 88 48 62 54
20
Examples 44 17 62 32 50 78 48 54 88
21
Let the height start from 1, and n(h) be minimum nodes in a tree of height h
n(1) = 1 and n(2) = 2 n(h) = 1 + n(h-1) + n(h-2) Since n(h-1) >= n(h-2) n(h) > 2n(h-2) > 4n(h-4) > 8n(h-6) ... > 2in(h-2i) Using n(2) = 2, we get i = h/2-1 n(h) > 2h/2-1*n(2) = 2h/2 Taking logarithms: h < 2log n(h)
22
What is minimum number of nodes with height h? n(h)>2h/2!
23
The height of an AVL tree is thus O( log2 n)!
24
Draw an AVL Tree of height 3 with Minimum number of nodes!
25
If the closest leaf is at level k, what is the maximum height of the AVL tree?
26
-given closest leaf at level k -find maximum height of BST?
What about a BST? -given closest leaf at level k -find maximum height of BST?
27
If the closest leaf is k, what can we say about nodes at levels k-2?
28
Example
29
If the closest leaf is at level k, all nodes at levels 0. 2
If the closest leaf is at level k, all nodes at levels 0.2..k-2 have 2 children! k
30
What about level k-1? k
31
Levels 0 through k-1 are full!
32
Number of nodes in an AVL tree with closest leaf at level k
2k ≤ n ≤2 2k+1 -1
33
Question: If the height of an AVL tree is h, where will the closest (to the root) leaf be?
34
Number of nodes in an AVL tree with closest leaf at level k
2h/2 ≤ n ≤2 h+1 -1
35
Structural Facts the closest leaf it at level h/2
upto level h/2-1, its complete after that, the density starts decreasing
36
AVL Tree Operations search insert delete
37
Insert 15 44 32 78 17 50 88 48 62
38
Is it balanced? 44 32 78 17 50 88 15 48 62
39
Re-balanced 44 17 78 15 32 50 88 48 62
40
Insert 54 44 17 78 32 50 88 48 62
41
Is it balanced? 44 17 78 32 50 88 48 62 54
42
1- go up and find the first unbalanced node (Z) 2 – name the child of Z we encountered while going up Y 3 – name the child of Y we encountered while going up X
43
Imbalanced Tree 44 17 78 Z 32 Y 50 88 48 62 X 54
44
Imbalanced Tree 44 32 78 Z 17 50 88 Y 15 48 62 X
45
What are different possibilities of X, Y, and Z?
46
Case 1 h+3h+4 z h+2h+3 y T4 h+1h+2 x T3 h+1 T1 T2 h+1 hh+1 h
47
After Right Rotation at Z
y h+2 h+2 z x T4 T3 T1 T2 h+1 h+1 h h+1
48
Case 2 h+3h+4 z h+2h+3 y T4 h+1 T1 x h+1h+2 h+1 T2 T3 hh+1 h
49
Right Rotation at Z Fails
y h+3 z h+2 T1 x h+1 T2 T3 T4 h+1 h+1 h Fails
50
Case 2 h+3h+4 z h+2h+3 y T4 h+1 T1 x h+1h+2 h+1 T2 T3 hh+1 h
51
Rotate Left at y h+4 z h+3 x T4 h+1 h+2 T3 y T2 T1 h h+1 h+1
52
Now Rotate Right at Z h+3 x h+2 h+2 z y T4 T3 T1 T2 h+1 h+1 h h+1
53
insert summary insert the element
check the height balance property on the way up to root rebalance the subtree rooted at the first unbalanced node STOP
54
Time complexity insert in BTS – O(log n)
Checking balance property – O(log n) Maximum rotations – 2 Hence O(log n)
55
Claim: In a BST, we always delete a leaf or a node with one child!
56
Delete 99 44 17 78 12 32 50 88 10 13 40 48 62 55 90 49 65 99 7 47 55 45
57
We can assume that deletion only takes place at leaf in an AVL tree!
58
Delete 99 44 17 78 12 32 50 88 10 13 40 48 62 55 90 49 65 99 7 47 55 45
59
1- let z be the first unbalanced node going up 2- let y be the child of z with larger height 3- let x be the child of y with larger height - if both child are of the same height, choose the one in the same direction as parent
60
Delete 99 44 Z 17 78 Y 12 32 50 88 X 10 13 40 48 62 55 90 49 65 7 47 55 45
61
Case 1 h+2 z h+1 y T4 h x T3 hh-1 T1 T2 h or h-1 h-1 or h-2
62
After right rotation at z
What do we do if it is h+1? h+1 or h+2 y h h or h+1 x z T1 T2 T3 T4 h-1 or h-2 h-1 or h-2 h or h-1 h-1
63
Case 2 h+2 z h+1 y T4 h h-1 T1 x h h-1 T2 T3 h-1 or h-2
64
Rotate Left at y h+2 z h+1 x h-1 T4 h y T3 h-1 or h-2 T2 T1 h-1 or h-2
65
Tree height reduced, so we need to go up and check!
Now rotate right at z h+1 x h h y z T1 T2 T3 T4 h-1 or h-2 h-1 or h-2 h-1 or h-2 h-1 Tree height reduced, so we need to go up and check!
66
delete summary delete the element until root is reached
go to the parent and check height balance property if unbalanced, balance the subtree rooted at that node if height nor reduced, stop
67
Time Complexity of delete
Delete in BST takes O(log n). Rebalancing also requires O(log n) time. O(log n)
68
Time Complexity of insert
We perform rotation only once. We need O(log n) to find unbalanced node. Insert in BST takes O(log n) O(log n)
69
Time complexity insert – O(n) remove – O(n) search – O(log n)
70
Comparison of Ordered Map Implementations
Sorted Array BST/List AVL Trees insert O(n) O(log n) delete search
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.