Download presentation
Presentation is loading. Please wait.
Published byVinícius Guterres da Cunha Modified over 6 years ago
1
David Kaplan Dept of Computer Science & Engineering Autumn 2001
CSE 326 AVL Trees David Kaplan Dept of Computer Science & Engineering Autumn 2001
2
Balance Balance == height(left subtree) - height(right subtree)
zero everywhere perfectly balanced small everywhere balanced enough Balance between -1 and 1 everywhere maximum height of ~1.44 log n t 5 7 We’ll use the concept of Balance to keep things shallow. AVL Trees CSE 326 Autumn
3
AVL Tree Dictionary Data Structure
Binary search tree properties binary tree property search tree property Balance property balance of every node is: -1 b 1 result: depth is (log n) 8 5 11 2 6 10 12 So, AVL trees will be Binary Search Trees with one extra feature: They balance themselves! The result is that all AVL trees at any point will have a logarithmic asymptotic bound on their depths 4 7 9 13 14 15 AVL Trees CSE 326 Autumn
4
An AVL Tree 10 10 3 5 15 2 9 12 20 17 30 data 3 height children 1 2 1
1 2 9 12 20 Here’s a revision of that tree that’s balanced. (Same values, similar tree) This one _is_ an AVL tree (and isn’t leftist). I also have here how we might store the nodes in the AVL tree. Notice that I’m going to keep track of height all the time. WHY? 17 30 AVL Trees CSE 326 Autumn
5
Not AVL Trees 0-2 = -2 (-1)-1 = -2 10 10 5 15 15 12 20 20 17 30 3 2 2
2 5 15 1 15 1 12 20 20 Here’s a revision of that tree that’s balanced. (Same values, similar tree) This one _is_ an AVL tree (and isn’t leftist). I also have here how we might store the nodes in the AVL tree. Notice that I’m going to keep track of height all the time. WHY? 17 30 Note: height(empty tree) == -1 AVL Trees CSE 326 Autumn
6
Good Insert Case: Balance Preserved
Good case: insert middle, then small,then tall Insert(middle) Insert(small) Insert(tall) 1 M Let’s make a tree from these people with their height as the keys. We’ll start by inserting [MIDDLE] first. Then, [SMALL] and finally [TALL]. Is this tree balanced? Yes! S T AVL Trees CSE 326 Autumn
7
Bad Insert Case #1: Left-Left or Right-Right Imbalance
Insert(small) Insert(middle) Insert(tall) 2 S 1 BC#1 Imbalance caused by either: Insert into left child’s left subtree Insert into right child’s right subtree M But, let’s start over… Insert [SMALL] Now, [MIDDLE]. Now, [TALL]. Is this tree balanced? NO! Who do we need at the root? [MIDDLE!] Alright, let’s pull er up. T AVL Trees CSE 326 Autumn
8
Single Rotation S M M S T T 2 1 1 Basic operation used in AVL trees:
S T This is the basic operation we’ll use in AVL trees. Since this is a right child, it could legally have the parent as its left child. When we finish the rotation, we have a balanced tree! T Basic operation used in AVL trees: A right child could legally have its parent as its left child. AVL Trees CSE 326 Autumn
9
General Bad Case #1 a a b X b X Z Y Z Y Note: imbalance is left-left
h + 2 a a h + 1 h h - 1 h + 1 h - 1 b X b X h-1 h h - 1 h - 1 Z Y Z Y Here’s the general form of this. We insert into the red tree. That ups the three heights on the left. Basically, you just need to pull up on the child. Then, ensure that everything falls in place as legal subtrees of the nodes. Notice, though, the height of this subtree is the same as it was before the insert into the red tree. So? So, we don’t have to worry about ancestors of the subtree becoming imbalanced; we can just stop here! Note: imbalance is left-left AVL Trees CSE 326 Autumn
10
Single Rotation Fixes Case #1 Imbalance
Y b Z h + 1 h + 2 h h + 1 h - 1 b X h h - 1 h h - 1 h - 1 Z Y Here’s the general form of this. We insert into the red tree. That ups the three heights on the left. Basically, you just need to pull up on the child. Then, ensure that everything falls in place as legal subtrees of the nodes. Notice, though, the height of this subtree is the same as it was before the insert into the red tree. So? So, we don’t have to worry about ancestors of the subtree becoming imbalanced; we can just stop here! Height of left subtree same as it was before insert! Height of all ancestors unchanged We can stop here! AVL Trees CSE 326 Autumn
11
Bad Insert Case #2: Left-Right or Right-Left Imbalance
Insert(small) Insert(tall) Insert(middle) 2 S 1 BC#2 Imbalance caused by either: Insert into left child’s right subtree Insert into right child’s left subtree T There’s another bad case, though. What if we insert: [SMALL] [TALL] [MIDDLE] Now, is the tree imbalanced? Will a single rotation fix it? (Try it by bringing up tall; doesn’t work!) M Will a single rotation fix this? AVL Trees CSE 326 Autumn
12
Double Rotation S S M T M S T M T 2 2 1 1 1 AVL Trees
Let’s try two single rotations, starting a bit lower down. First, we rotate up middle. Then, we rotate up middle again! Is the new tree balanced? S T M T AVL Trees CSE 326 Autumn
13
General Bad Case #2 a a b X b X Z Y Z Y Note: imbalance is left-right
AVL Trees CSE 326 Autumn
14
Double Rotation Fixes Case #2 Imbalance
h + 2 h + 1 h + 1 c h - 1 b Z h h b a h - 1 W h c h - 1 h - 1 X Y W Z X Y Here’s the general form of this. Notice that the difference here is that we zigged one way than zagged the other to find the problem. We don’t really know or care which of X or Y was inserted into, but one of them was. To fix it, we pull c all the way up. Then, put a, b, and the subtrees beneath it in the reasonable manner. The height is still the same at the end! h - 1? h - 1? Initially: insert into either X or Y unbalances tree (root balance goes to 2 or -2) “Zig zag” to pull up c – restores root height to h+1, left subtree height to h AVL Trees CSE 326 Autumn
15
AVL Insert Algorithm Find spot for value Hang new node
Search back up looking for imbalance If there is an imbalance: case #1: Perform single rotation case #2: Perform double rotation Done! (There can only be one imbalance!) Those two cases (along with their mirror images) are the only four that can happen! So, here’s our insert algorithm. We just hang the node. Search for a spot where there’s imbalance. If there is, fix it (according to the shape of the imbalance). And then we’re done; there can only be one problem! AVL Trees CSE 326 Autumn
16
Easy Insert Insert(3) 10 5 15 2 9 12 20 17 30 3 1 2 1 AVL Trees
1 2 9 12 20 Let’s insert 3. This is easy! It just goes under 2 (to the left). Update the balances: any imbalance? NO! 17 30 AVL Trees CSE 326 Autumn
17
Hard Insert (Bad Case #1)
2 3 Insert(33) 10 5 15 2 9 12 20 Now, let’s insert 33. Where does it go? Left of 30. 3 17 30 AVL Trees CSE 326 Autumn
18
Single Rotation 1 2 3 1 2 3 10 10 5 15 5 20 2 9 12 20 2 9 15 30 Here’s the tree with the balances updated. Now, node 15 is bad! Since the problem is in the left subtree of the left child, we can fix it with a single rotation. We pull 20 up. Hang 15 to the left. Pass 17 to 15. And, we’re done! Notice that I didn’t update 10’s height until we checked 15. Did it change after all? 3 17 30 3 12 17 33 33 AVL Trees CSE 326 Autumn
19
Hard Insert (Bad Case #2)
1 2 3 Insert(18) 10 5 15 2 9 12 20 Now, let’s back up to before 33 and insert 18 instead. Goes right of 17. Again, there’s imbalance. But, this time, it’s a zig-zag! 3 17 30 AVL Trees CSE 326 Autumn
20
Single Rotation (oops!)
1 2 3 1 2 3 10 10 5 15 5 20 2 9 12 20 2 9 15 30 We can try a single rotation, but we end up with another zig-zag! 3 17 30 3 12 17 18 18 AVL Trees CSE 326 Autumn
21
Double Rotation (Step #1)
2 3 1 2 3 10 10 5 15 5 15 2 9 12 20 2 9 12 17 So, we’ll double rotate. Start by moving the offending grand-child up. We get an even more imbalanced tree. BUT, it’s imbalanced like a zig-zig tree now! 3 17 30 3 20 18 Look familiar? 18 30 AVL Trees CSE 326 Autumn
22
Double Rotation (Step #2)
1 2 3 1 2 3 10 10 5 15 5 17 2 9 12 17 2 9 15 20 So, let’s pull 17 up again. Now, we get a balanced tree. And, again, 10’s height didn’t need to change. 3 20 3 12 18 30 18 30 AVL Trees CSE 326 Autumn
23
AVL Insert Algorithm Revisited
Recursive 1. Search downward for spot 2. Insert node 3. Unwind stack, correcting heights a. If imbalance #1, single rotate b. If imbalance #2, double rotate Iterative 1. Search downward for spot, stacking parent nodes 2. Insert node 3. Unwind stack, correcting heights a. If imbalance #1, single rotate and exit b. If imbalance #2, double rotate and OK, here’s the algorithm again. Notice that there’s very little difference between the recursive and iterative. Why do I keep a stack for the iterative version? To go bottom to top. Can’t I go top down? Now, what’s left? Single and double rotate! AVL Trees CSE 326 Autumn
24
Single Rotation Code X Y Z void RotateRight(Node *& root) {
Node * temp = root->right; root->right = temp->left; temp->left = root; root->height = max(root->right->height, root->left->height) + 1; temp->height = max(temp->right->height, temp->left->height) + 1; root = temp; } X Y Z root temp Here’s code for one of the two single rotate cases. RotateRight brings up the right child. We’ve inserted into Z, and now we want to fix it. AVL Trees CSE 326 Autumn
25
Double Rotation Code First Rotation a Z b W c X Y a Z c b X Y W
void DoubleRotateRight(Node *& root) { RotateLeft(root->right); RotateRight(root); } First Rotation a Z b W c X Y a Z c b X Y W Here’s the double rotation code. Pretty tough, eh? AVL Trees CSE 326 Autumn
26
Double Rotation Completed
First Rotation Second Rotation a Z c b X Y W c a b X W Z Y AVL Trees CSE 326 Autumn
27
Deletion: Really Easy Case
1 2 3 Delete(17) 10 5 15 2 9 12 20 3 17 30 AVL Trees CSE 326 Autumn
28
Deletion: Pretty Easy Case
1 2 3 Delete(15) 10 5 15 2 9 12 20 OK, if we have a bit of extra time, do this. Let’s try deleting. 15 is easy! It has two children, so we do BST deletion. 17 replaces 15. 15 goes away. Did we disturb the tree? NO! 3 17 30 AVL Trees CSE 326 Autumn
29
Deletion: Pretty Easy Case (cont.)
3 Delete(15) 10 2 2 5 17 1 1 2 9 12 20 OK, if we have a bit of extra time, do this. Let’s try deleting. 15 is easy! It has two children, so we do BST deletion. 17 replaces 15. 15 goes away. Did we disturb the tree? NO! 3 30 AVL Trees CSE 326 Autumn
30
Deletion (Hard Case #1) Delete(12) 10 5 17 2 9 12 20 3 30 3 2 1
2 3 10 5 17 2 9 12 20 Now, let’s delete 12. 12 goes away. Now, there’s trouble. We’ve put an imbalance in. So, we check up from the point of deletion and fix the imbalance at 17. 3 30 AVL Trees CSE 326 Autumn
31
Single Rotation on Deletion
20 9 2 17 5 10 30 3 1 But what happened on the fix? Something very disturbing. What? The subtree’s height changed!! So, the deletion can propagate. Deletion can differ from insertion – How? AVL Trees CSE 326 Autumn
32
Deletion (Hard Case) Delete(9) 10 5 17 2 9 12 12 20 20 3 11 15 15 18
3 4 10 5 17 2 9 12 12 20 20 1 1 3 11 15 15 18 30 30 13 13 33 33 AVL Trees CSE 326 Autumn
33
Double Rotation on Deletion
Not finished! 1 2 3 4 2 1 3 4 10 10 5 17 3 17 2 2 12 20 2 5 12 20 1 1 1 1 3 11 15 18 30 11 15 18 30 13 33 13 33 AVL Trees CSE 326 Autumn
34
Deletion with Propagation
2 1 3 4 10 What different about this case? 3 17 2 5 12 20 We get to choose whether to single or double rotate! 1 1 11 15 18 30 13 33 AVL Trees CSE 326 Autumn
35
Propagated Single Rotation
2 1 3 4 4 10 17 3 2 3 17 10 20 1 2 1 2 5 12 20 3 12 18 30 1 1 1 11 15 18 30 2 5 11 15 33 13 33 13 AVL Trees CSE 326 Autumn
36
Propagated Double Rotation
2 1 3 4 4 10 12 2 3 3 17 10 17 1 1 2 2 5 12 20 3 11 15 20 1 1 1 11 15 18 30 2 5 13 18 30 13 33 33 AVL Trees CSE 326 Autumn
37
AVL Deletion Algorithm
Recursive Search downward for node Delete node Unwind, correcting heights as we go a. If imbalance #1, single rotate b. If imbalance #2 (or don’t care), double rotate Iterative 1. Search downward for node, stacking parent nodes 2. Delete node 3. Unwind stack, correcting heights a. If imbalance #1, single rotate b. If imbalance #2 (or don’t care) double rotate OK, here’s the algorithm again. Notice that there’s very little difference between the recursive and iterative. Why do I keep a stack for the iterative version? To go bottom to top. Can’t I go top down? Now, what’s left? Single and double rotate! AVL Trees CSE 326 Autumn
38
Building an AVL Tree Input: sequence of n keys (unordered) 19 3 4 18 7
Insert each into initially empty AVL tree But, suppose input is already sorted … Can we do better than O(n log n)? AVL Trees CSE 326 Autumn
39
AVL BuildTree 5 8 10 15 17 20 30 35 40 Divide & Conquer 17
Divide the problem into parts Solve each part recursively Merge the parts into a general solution 17 IT DEPENDS! How long does divide & conquer take? 8 10 15 5 20 30 35 40 AVL Trees CSE 326 Autumn
40
BuildTree Example 5 8 10 15 17 20 30 35 40 3 17 5 8 10 15 2 2 20 30 35 40 10 35 20 30 5 8 1 1 8 15 30 40 5 20 AVL Trees CSE 326 Autumn
41
BuildTree Analysis (Approximate)
T(n) = 2T(n/2) + 1 T(n) = 2(2T(n/4)+1) + 1 T(n) = 4T(n/4) T(n) = 4(2T(n/8)+1) T(n) = 8T(n/8) T(n) = 2kT(n/2k) + let 2k = n, log n = k T(n) = nT(1) + T(n) = (n) Summation is 2^logn + 2^logn-1 + 2^logn-2+… n+n/2+n/4+n/8+… ~2n AVL Trees CSE 326 Autumn
42
BuildTree Analysis (Exact)
Precise Analysis: T(0) = b T(n) = T( ) + T( ) + c By induction on n: T(n) = (b+c)n + b Base case: T(0) = b = (b+c)0 + b Induction step: T(n) = (b+c) + b + (b+c) + b + c = (b+c)n + b QED: T(n) = (b+c)n + b = (n) AVL Trees CSE 326 Autumn
43
Thinking About AVL Observations Coding complexity?
+ Worst case height of an AVL tree is about 1.44 log n + Insert, Find, Delete in worst case O(log n) + Only one (single or double) rotation needed on insertion + Compatible with lazy deletion - O(log n) rotations needed on deletion - Height fields must be maintained (or 2-bit balance) Coding complexity? AVL Trees CSE 326 Autumn
44
Alternatives to AVL Trees
Weight balanced trees keep about the same number of nodes in each subtree not nearly as nice Splay trees “blind” adjusting version of AVL trees no height information maintained! insert/find always rotates node to the root! worst case time is O(n) amortized time for all operations is O(log n) mysterious, but often faster than AVL trees in practice (better low-order terms) AVL Trees CSE 326 Autumn
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.