Download presentation
Presentation is loading. Please wait.
Published byJane Georgina Bryant Modified over 9 years ago
1
1 Joe Meehean
2
BST efficiency relies on height lookup, insert, delete: O(height) a balanced tree has the smallest height We can balance an unbalanced tree DSW expensive How can we ensure that our BST stays balanced? after inserts and deletes 2
3
Adelson-Velskii and Landis Binary search tree Additional balance condition for every node height of subtrees differ by at most 1 must store height of each node empty tree has height of -1 3
4
Insert as normal (BST) Update heights Check balance condition height differences of nodes along path from new node to root Use rotation to fix imbalances 4
5
Fixing imbalance at node A 4 cases: Insertion into 1. left subtree of left child of A 2. right subtree of left child of A 3. left subtree of right child of A 4. right subtree of right child of A Cases 1 & 4 are outside inserts Cases 2 & 3 are inside inserts 5
6
Insert key less than A & B 6 A A B B X Y Z h: 2 h: 3h: 2 h: 4
7
Insert key less than A & B 7 A A B B X Y Z h: 2 h: 3h: 2 h: 4 A A B B X Y Z h: 2 h: 3 h: 4h: 2 h: 5
8
What series of rotations can we do to fix this? 8
9
Rotate A with “highest” subtree 9 A A B B X Y Z h: 2 h: 3 h: 4h: 2 h: 5
10
Rotate A with “highest” subtree 10 A A B B Y Z A A B B Y Z X X h: 2 h: 3 h: 4h: 2 h: 5 h: 2 h: 3 h: 4 h: 3 h: 2
11
Are we done? Worry about imbalances above B? 11 A A B B Y Z X h: 2 h: 3 h: 4 h: 3 h: 2
12
B tree same height as A tree before insert Property of outside insert rebalances Right-right imbalance similar 12 A A B B Y Z X h: 2 h: 3 h: 4 h: 3 h: 2
13
More specifically Balance factor = h(left) – h(right) If node N has a balance factor of 2, left subtree (L) is too high if L has a balance factor of 1 rotate N & L If node N has a balance factor of -2, right subtree (R) is too high if R has a balance factor of -1 rotate N & R 13
14
14
15
Fixing imbalance at node A 4 cases: Insertion into 1. left subtree of left child of A 2. right subtree of left child of A 3. left subtree of right child of A 4. right subtree of right child of A Cases 1 & 4 are outside inserts Cases 2 & 3 are inside inserts 15
16
Insert key less than A, greater than B 16 A A B B X Y Z h: 2 h: 3h: 2 h: 4
17
Insert key less than A, greater than B 17 A A B B X Y Z h: 2 h: 3h: 2 h: 4 A A B B X Y Z h: 3 h: 2 h: 4h: 2 h: 5
18
Let’s try to rotate A & B again 18 A A B B X Z h: 3 h: 2 h: 4h: 2 h: 5 Y
19
Let’s try to rotate A & B again 19 A A B B X Z h: 3 h: 2 h: 4h: 2 h: 5 Y A A B B X Z h: 3 h: 2 h: 4h: 2 h: 5 Y
20
Let’s expand Y 20 A A B B X Z h: 3 h: 2 h: 4h: 2 h: 5 Q R C C A A B B X Z h: 3 h: 2 h: 4h: 2 h: 5 Y h: 1 h: 2
21
What if we made C the root 21 A A B B X Z h: 3 h: 2 h: 4h: 2 h: 5 R C C h: 2 A A B B X Z h: 3 h: 1 h: 3 h: 2 h: 4 C C h: 2 Q R h: 1 Q
22
What series of rotations can we do to accomplish this? 22
23
Rotate left child (B) with its right child (C) 23 A A B B X Z h: 3 h: 1 h: 4h: 2 h: 5 C C h: 2 A A B B X Z h: 3 h: 2 h: 4h: 2 h: 5 C C h: 2 Q R h: 1 Q R
24
Rotate root (A) with its new left child (C) 24 A A B B X Z h: 3 h: 1 h: 3 h: 2 h: 4 C C h: 2 A A B B X Z h: 3 h: 2 h: 4h: 2 h: 5 Q R C C h: 2 Q R
25
Are we done? Worry about imbalances above ? 25 A A B B X Z h: 3 h: 1 h: 3 h: 2 h: 4 C C h: 2 Q R
26
C tree same height as A tree before insert Property of inside insert rebalances Right-left imbalance similar 26 A A B B X Z h: 3 h: 1 h: 3 h: 2 h: 4 C C h: 2 Q R
27
More generally If there is an inside imbalance at A Find A’s highest child, B Rotate B with its highest child, C Rotate A with C 27
28
More specifically Balance factor = h(left) – h(right) If node N has a balance factor of 2, left subtree (L) is too high if L has a balance factor of -1 rotate L with L’s right child rotate N with its new left child 28
29
More specifically Balance factor = h(left) – h(right) If node N has a balance factor of -2, right subtree (R) is too high if R has a balance factor of 1 rotate R with R’s left child rotate N with it’s new right child 29
30
30
31
Find the node N w/ key to be deleted Different actions depending on N’s # of kids Case 1: N has 0 kids (it’s a leaf) set parent’s N-pointer (left or right) to null Case 2: N has 1 kid set parent’s N-pointer to point to N’s only kid Case 3: N has 2 kids Replace N’s key with a key further down in the tree Delete that node 31
32
What node value can replace n’s value? new value of n must be: > all values in left subtree < all values in right subtree Largest value from the left subtree Smallest value from the right subtree let’s choose this one (arbitrarily) use findMin on root of right subtree 32
33
Deletes can also imbalance an AVL tree Can we fix these imbalances with rotations too? How many rotations do we need to do? 33
34
75 55 50 60 57 Smallest value in right subtree delete(50) 34 X Y Z h: 2 h: 3 h: 1 h: 0 h: 2 h: 4 65 h: 1 h: 5 62 h: 0
35
Copy smallest key in R subtree 35 75 55 60 57 delete(50) X Y Z h: 2 h: 3 h: 1 h: 0 h: 2 h: 4 65 h: 1 h: 5 62 h: 0
36
delete(55) from R subtree 36 75 57 55 60 delete(50) X Y Z h: 2 h: 3 h: 0 h: 2 h: 4 65 h: 1 h: 5 62 h: 0
37
75 57 55 60 Fix imbalance delete(50) 37 X Y Z h: 1 h: 2 h: 0 h: 2 h: 3 65 h: 1 h: 5 62 h: 0
38
New imbalance Need to rebalance all the way up to root 38 75 57 55 60 delete(50) X Y Z h: 1 h: 2 h: 0 h: 3 65 h: 5 62 h: 0
39
More generally Perform a BST delete Check for imbalances along path to root Rebalance using expanded insert rules if delete happened in “light” child “heavy” child may have a balance factor of 0 additional case from insertion rules 39
40
Balance factor = h(left) – h(right) Given node N and its two children L & R bf(N) == -2 & bf(R) == 0 single right child rotation bf(N) == -2 & bf(R)== -1 single right child rotation bf(N) == -2 & bf(R) == 1 double right child rotation (from inside insert) 40
41
Balance factor = h(left) – h(right) Given node N and its two children R & L bf(N) == 2 & bf(L) == 0 single left child rotation bf(N) == 2 & bf(L) == 1 single left child rotation bf(N) == 2 & bf(L) == -1 double left child rotation (from inside insert) 41
42
BST insert, delete, lookup O(H), H is height of tree AVL like BST, but ensures balance balanced tree has height of log N Balancing rotations on insert and delete 42
43
AVL Complexity Insert inserting node: O(H) == O(logN) rebalance: O(rotations) == O(2) == O(1) Erase deleting a node: O(H) == O(logN) rebalance: O(rotations) == O(logN) 43
44
44
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.