Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSE 326: Data Structures Lecture #9 Amazingly Vexing Letters

Similar presentations


Presentation on theme: "CSE 326: Data Structures Lecture #9 Amazingly Vexing Letters"— Presentation transcript:

1 CSE 326: Data Structures Lecture #9 Amazingly Vexing Letters
Bart Niswonger Summer Quarter 2001

2 Today’s Outline Project II Discussion AVL Trees Testing
“Software Engineering” Threads Role of Documentation AVL Trees Rotations Insertions Today we are going to start by talking about the second project Hopefully everyone has started! I will be putting the first cut of the code out today – with an upgrade adding real network support coming later this week. The program we are building is pretty complex and I feel it would be worth taking some time to go into its general architecture, why it is complex, why we are using it as a project, where data structures fit in and that sort of thing. Why is it complex? Because I wanted it to be real enough for you to see why design choices make a difference; I wanted it to be big enough to use templates reasonably, to require some code reuse, and to have a wide variety of data structures. Why are we using it – because it is complex, it has space for a wide variety of data structures, etc; software engineering – you will have to come to understand this beast to some degree, this skill is crucial when you get a job out in the real world and yet we never teach it, that sucks – I believe that experience is a valid (perhaps the only) way to learn this stuff, so here is a pile of experience. Unfortunately, the design is not as clean as it could be, feel free to try and clean it up!! It is at the point where I would like to do a complete design review and rewrite, but as often happens in the real world, there is no time Data structures: stack & queues in maze runners; dictionaries in a bunch of places mapping one thing to another; priority queues to manage event queues and network queues

3 Architecture USER Simulator Square Maze Simulator Visualization
MazeRunner Simulator USER Square maze Simulator Simulated maze Maze runner Events Simulated Maze Simulated Maze MazeRunner MazeRunner DFS Maze Runner BFS Maze Runner

4 Networked Architecture
Square Maze Simulator Visualization MazeRunner Simulator USER Events A special network listener which sends events just like a simulated maze, although it does have a closer friendship with the simulator for certain things. Network Listener Simulated Maze Square Maze MazeRunner Simulator MazeRunner Events Simulated Maze BFS Maze Runner Network Listener MazeRunner BFS Maze Runner

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 Balance Balance: Balance between -1 and 1 everywhere 
height(left subtree) - height(right subtree) zero everywhere  perfectly balanced small everywhere  balanced enough t 5 7 We’ll use the concept of Balance to keep things shallow. Balance between -1 and 1 everywhere  maximum height of 1.44 log n

7 But, How Do We Stay Balanced?
I need: the smallest person in the class the tallest person in the class the averagest (?) person in the class 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?

8 Beautiful Balance 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!

9 Bad Case #1 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.

10 Single Rotation 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!

11 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 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 subtree same as it was before insert! Height of all ancestors unchanged. So?

12 Bad Case #2 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!)

13 Double Rotation 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?

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

15 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 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!

16 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

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

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

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

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

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

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

23 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!

24 Single Rotation Code X Y Z root temp 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; } 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.

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?

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

27 AVL 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

28 Bonus: Deletion (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

29 Deletion (Hard Case #1) Delete(12) 10 5 17 2 9 12 20 3 30 3 2 1
2 3 Delete(12) 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

30 Single Rotation on Deletion
1 2 3 1 2 3 10 10 5 17 5 20 2 9 20 2 9 17 30 But what happened on the fix? Something very disturbing. What? The subtree’s height changed!! So, the deletion can propagate. We’ll find out how on Monday. 3 30 3 Something very bad happened!

31 To Do Project II-A for Wednesday Read through section 4.7 in the book
Comments & Feedback Homework III

32 Coming Up No Quiz Thursday Midterm next week Project II – the writeup!
Even more balancing acts A Huge Search Tree Data Structure


Download ppt "CSE 326: Data Structures Lecture #9 Amazingly Vexing Letters"

Similar presentations


Ads by Google