Presentation is loading. Please wait.

Presentation is loading. Please wait.

CPSC 221: Algorithms and Data Structures Lecture #6 Balancing Act

Similar presentations


Presentation on theme: "CPSC 221: Algorithms and Data Structures Lecture #6 Balancing Act"— Presentation transcript:

1 CPSC 221: Algorithms and Data Structures Lecture #6 Balancing Act
Steve Wolfman 2011W2 Alright, today we’ll get a little Yin and Yang.

2 Today’s Outline Addressing one of our problems
Single and Double Rotations AVL Tree Implementation

3 Beauty is Only (log n) Deep
Binary Search Trees are fast if they’re shallow: perfectly complete perfectly complete except the one level fringe (like a heap) anything else? What makes a good BST good? Here’s two examples. Are these the only good BSTs? No! Anything without too many long branches is good, right? Problems occur when one branch is much longer than the other! What matters here?

4 Balance Balance Balance between -1 and 1 everywhere 
5 7 Balance height(left subtree) - height(right subtree) zero everywhere  perfectly balanced small everywhere  balanced enough We’ll use the concept of Balance to keep things shallow. Balance between -1 and 1 everywhere  maximum height of 1.44 lg n

5 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

6 Testing the Balance Property
10 5 15 2 9 20 We need to know a few things now: How do we track the balance? How do we detect imbalance? How do we restore balance? Let’s start with this tree and see if it’s balanced. Height (level order): There’s that darn 15 again! It’s not balanced at 15. 7 17 30 NULLs have height -1

7 An AVL Tree 10 10 3 5 20 2 9 15 30 7 17 data 3 height children 2 2 1 1
2 9 15 30 Here’s a revision of that tree that’s balanced. (Same values, similar tree) This one _is_ an AVL tree. 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? 7 17

8 Today’s Outline Addressing one of our problems
Single and Double Rotations AVL Tree Implementation

9 But, How Do We Stay Balanced?
Need: three volunteers proud of their very diverse height. Alright, so we now what balance is and how to detect imbalance. How do we keep the tree balanced? I need some data points to do this. Can I have the {smallest, tallest, middlest} person in the class, please?

10 Beautiful Balance (SIMPLEST version)
Insert(middle) Insert(small) Insert(tall) 1 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!

11 Bad Case #1 (SIMPLEST version)
Insert(small) Insert(middle) Insert(tall) 2 1 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.

12 Single Rotation (SIMPLEST version)
2 1 1 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!

13 General Single Rotation
h + 2 h + 1 a a X Y b Z h h + 1 h - 1 b X h h - 1 h h - 1 h - 1 Z Y  An insert made this BAD! 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! After rotation, subtree’s height same as before insert! Height of all ancestors unchanged. So?

14 Bad Case #2 (SIMPLEST version)
Insert(small) Insert(tall) Insert(middle) 2 1 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!)

15 Double Rotation (SIMPLEST version)
2 2 1 1 1 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?

16 General Double Rotation
h + 2 a 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 h - 1? 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? Height of subtree still the same as it was before insert! Height of all ancestors unchanged.

17 Today’s Outline Addressing one of our problems
Single and Double Rotations AVL Tree Implementation

18 Insert Algorithm Find spot for value Hang new node
Search back up for imbalance If there is an imbalance: case #1: Perform single rotation and exit case #2: Perform double rotation and exit Mirrored cases also possible OK, thank you BST Three! And 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!

19 Easy Insert Insert(3) 10 5 15 2 9 12 20 17 30 3 1 2 1 Let’s insert 3.
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

20 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

21 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 right subtree of the right 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

22 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

23 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

24 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 18 30 Look familiar?

25 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

26 AVL 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!

27 Single Rotation Code X Y Z
root X Y Z temp void RotateLeft(Node *& root) { Node * temp = root->right; root->right = temp->left; temp->left = root; root->height = max(height(root->right), height(root->left)) + 1; temp->height = max(height(temp->right), height(temp->left) + 1; root = temp; } Here’s code for one of the two single rotate cases. RotateLeft “moves the two nodes to the left”. We’ve inserted into Z, and now we want to fix it. (The “height” function returns -1 for a NULL subtree or the “height” field of a non-NULL subtree.)

28 Double Rotation Code First Rotation a Z b W c X Y a Z c b X Y W
void DoubleRotateLeft(Node *& root) { RotateRight(root->right); RotateLeft(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?

29 Double Rotation Completed
First Rotation Second Rotation a Z c b X Y W c a b X W Z Y

30 AVL Adelson-Velskii Landis Automatically Virtually Leveled
Architecture for inVisible Leveling (the “in” is inVisible) All Very Low Articulating Various Lines Amortizing? Very Lousy! Absolut Vodka Logarithms Amazingly Vexing Letters Alright, now you know (AVL trees) what they do… let’s find out what does AVL stand for. We’ll vote! Adelson-Velskii Landis

31 To Do Read KW , 11.5

32 Coming Up Another approach to balancing, which leads to…
Huge Search Tree Data Structure


Download ppt "CPSC 221: Algorithms and Data Structures Lecture #6 Balancing Act"

Similar presentations


Ads by Google