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

Slides:



Advertisements
Similar presentations
AVL Trees COL 106 Amit Kumar Shweta Agrawal Slide Courtesy : Douglas Wilhelm Harder, MMath, UWaterloo
Advertisements

Trees Types and Operations
CSE332: Data Abstractions Lecture 7: AVL Trees Dan Grossman Spring 2010.
CSE332: Data Abstractions Lecture 7: AVL Trees Tyler Robison Summer
CSE 326: Data Structures AVL Trees
CSE 326: Data Structures Lecture #10 Balancing Act and What AVL Stands For Steve Wolfman Winter Quarter 2000.
CSE 326: Data Structures Lecture #13 Extendible Hashing and Splay Trees Alon Halevy Spring Quarter 2001.
CSE 326: Data Structures Lecture #8 Binary Search Trees Alon Halevy Spring Quarter 2001.
1 CSE 326: Data Structures Trees Lecture 7: Wednesday, Jan 23, 2003.
CSE373: Data Structures & Algorithms Lecture 7: AVL Trees Nicki Dell Spring 2014 CSE373: Data Structures & Algorithms1.
CPSC 221: Algorithms and Data Structures Lecture #6 Balancing Act Steve Wolfman 2014W1 1.
Week 8 - Wednesday.  What did we talk about last time?  Level order traversal  BST delete  2-3 trees.
CSE 326: Data Structures Lecture #11 AVL and Splay Trees Steve Wolfman Winter Quarter 2000.
Data Structures AVL Trees.
AVL trees1 AVL Trees Height of a node : The height of a leaf is 1. The height of a null pointer is zero. The height of an internal node is the maximum.
CSE373: Data Structures & Algorithms Lecture 7: AVL Trees Linda Shapiro Winter 2015.
Week 15 – Wednesday.  What did we talk about last time?  Review up to Exam 1.
CSE373: Data Structures & Algorithms Lecture 8: AVL Trees and Priority Queues Linda Shapiro Spring 2016.
CSE332: Data Abstractions Lecture 7: AVL Trees
Lecture 15 Nov 3, 2013 Height-balanced BST Recall:
File Organization and Processing Week 3
Lec 13 Oct 17, 2011 AVL tree – height-balanced tree Other options:
BCA-II Data Structure Using C
Week 7 - Friday CS221.
CSE 326: Data Structures Lecture #8 Pruning (and Pruning for the Lazy)
Binary Search Trees One of the tree applications in Chapter 10 is binary search trees. In Chapter 10, binary search trees are used to implement bags.
CPSC 221: Algorithms and Data Structures Lecture #6 Balancing Act
UNIT III TREES.
Binary Search Trees.
CSIT 402 Data Structures II
CPSC 221: Algorithms and Data Structures Lecture #6 Balancing Act
CSE373: Data Structures & Algorithms
Splay Trees.
CSE373: Data Structures & Algorithms Lecture 7: AVL Trees
AVL Tree Mohammad Asad Abbasi Lecture 12
AVL Tree.
CSE373: Data Structures & Algorithms Lecture 7: AVL Trees
Trees (Chapter 4) Binary Search Trees - Review Definition
CSE373: Data Structures & Algorithms Lecture 7: AVL Trees
TCSS 342, Winter 2006 Lecture Notes
Wednesday, April 18, 2018 Announcements… For Today…
B+ Trees What are B+ Trees used for What is a B Tree What is a B+ Tree
Balanced-Trees This presentation shows you the potential problem of unbalanced tree and show two way to fix it This lecture introduces heaps, which are.
AVL Trees: AVL Trees: Balanced binary search tree
CSE 326: Data Structures Lecture #9 AVL II
David Kaplan Dept of Computer Science & Engineering Autumn 2001
CSE 373: Data Structures and Algorithms
CSE373: Data Structures & Algorithms Lecture 5: AVL Trees
Balanced-Trees This presentation shows you the potential problem of unbalanced tree and show two way to fix it This lecture introduces heaps, which are.
Tree Rotations and AVL Trees
AVL Trees CSE 373 Data Structures.
CSE 332: Data Abstractions AVL Trees
CSE 373 Data Structures and Algorithms
CSE 326: Data Structures Lecture #18 Fistful of Data Structures
Analysis of Algorithms - Trees
AVL-Trees.
CSE 326: Data Structures Lecture #9 Amazingly Vexing Letters
AVL-Trees (Part 1).
CSE 326: Data Structures Lecture #8 Balanced Dendrology
Lecture 10 Oct 1, 2012 Complete BST deletion Height-balanced BST
ITCS6114 Algorithms and Data Structures
CSE 373 Data Structures and Algorithms
CSE 326: Data Structures Lecture #9 AVL II
Richard Anderson Spring 2016
CSE 326: Data Structures Splay Trees
CSE 373 Data Structures Lecture 8
CSE 326: Data Structures Lecture #10 Amazingly Vexing Letters
CSE 326: Data Structures Lecture #10 B-Trees
326 Lecture 9 Henry Kautz Winter Quarter 2002
AVL Trees: AVL Trees: Balanced binary search tree
Presentation transcript:

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

Learning Goals After this unit, you should be able to: Compare and contrast balanced/unbalanced trees. Describe and apply the rotation algorithm to a BST to implement a balanced tree. Recognize balanced binary search trees (among other tree types you recognize, e.g., heaps, general binary trees, general BSTs).

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

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?

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

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 13 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 12 14 15

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): 3 2 2 0 1 1 0 0 0 There’s that darn 15 again! It’s not balanced at 15. 7 17 30 NULLs have height -1

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

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

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?

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!

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.

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!

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?

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

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?

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.

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

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

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

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

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

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

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

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?

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 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! (Parent pointers make iterative version easier.)

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. RotateRight brings up the right child. 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.) Notice that root is a reference parameter. MUST be the “correct” pointer.

Double Rotation Code First Rotation a Z b W c X Y a Z c b X Y W void DoubleRotateLeft(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?

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

Coming Up Another approach to balancing, which leads to… Huge Search Tree Data Structure OR: A totally different approach to fast dictionaries: making a hash of your keys.