David Kaplan Dept of Computer Science & Engineering Autumn 2001

Slides:



Advertisements
Similar presentations
AVL Trees1 Part-F2 AVL Trees v z. AVL Trees2 AVL Tree Definition (§ 9.2) AVL trees are balanced. An AVL Tree is a binary search tree such that.
Advertisements

AVL Trees COL 106 Amit Kumar Shweta Agrawal Slide Courtesy : Douglas Wilhelm Harder, MMath, UWaterloo
Trees Types and Operations
CS202 - Fundamental Structures of Computer Science II
Splay Trees CSIT 402 Data Structures II. Motivation Problems with other balanced trees – AVL: extra storage/complexity for height fields Periulous delete.
CSE332: Data Abstractions Lecture 7: AVL Trees Dan Grossman Spring 2010.
CSE332: Data Abstractions Lecture 7: AVL Trees Tyler Robison Summer
Tirgul 5 AVL trees.
CSE 326: Data Structures Splay Trees Ben Lerner Summer 2007.
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 #8 Binary Search Trees Alon Halevy Spring Quarter 2001.
1 CSE 326: Data Structures Trees Lecture 7: Wednesday, Jan 23, 2003.
David Kaplan Dept of Computer Science & Engineering Autumn 2001
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.
1 Trees 4: AVL Trees Section 4.4. Motivation When building a binary search tree, what type of trees would we like? Example: 3, 5, 8, 20, 18, 13, 22 2.
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.
CSE373: Data Structures & Algorithms Lecture 8: AVL Trees and Priority Queues Linda Shapiro Spring 2016.
CSE332: Data Abstractions Lecture 7: AVL Trees
File Organization and Processing Week 3
Lec 13 Oct 17, 2011 AVL tree – height-balanced tree Other options:
BCA-II Data Structure Using C
Balancing Binary Search Trees
CSE373: Data Structures & Algorithms
CPSC 221: Algorithms and Data Structures Lecture #6 Balancing Act
Binary Search Trees.
CSIT 402 Data Structures II
CS202 - Fundamental Structures of Computer Science II
CS202 - Fundamental Structures of Computer Science 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
CSE373: Data Structures & Algorithms Lecture 7: AVL Trees
Lecture 25 Splay Tree Chapter 10 of textbook
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
Instructor: Lilian de Greef Quarter: Summer 2017
AVL Trees: AVL Trees: Balanced binary search tree
CS202 - Fundamental Structures of Computer Science II
CSE 326: Data Structures Lecture #9 AVL II
CPSC 221: Algorithms and Data Structures Lecture #6 Balancing Act
v z Chapter 10 AVL Trees Acknowledgement: These slides are adapted from slides provided with Data Structures and Algorithms in C++, Goodrich,
CSE373: Data Structures & Algorithms Lecture 5: AVL Trees
AVL Trees CSE 373 Data Structures.
CSE 332: Data Abstractions AVL Trees
CS202 - Fundamental Structures of Computer Science II
Analysis of Algorithms - 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
CSE 326: Data Structures Lecture #9 AVL II
Richard Anderson Spring 2016
CSE 326: Data Structures Splay Trees
CSE 373 Data Structures Lecture 8
1 Lecture 13 CS2013.
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
CS202 - Fundamental Structures of Computer Science II
CS202 - Fundamental Structures of Computer Science II
AVL Trees: AVL Trees: Balanced binary search tree
Presentation transcript:

David Kaplan Dept of Computer Science & Engineering Autumn 2001 CSE 326 AVL Trees David Kaplan Dept of Computer Science & Engineering Autumn 2001

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 2001 2

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 2001 3

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 2001 4

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 2001 5

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 2001 6

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 2001 7

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 2001 8

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 2001 9

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 2001 10

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 2001 11

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 2001 12

General Bad Case #2 a a b X b X Z Y Z Y Note: imbalance is left-right AVL Trees CSE 326 Autumn 2001 13

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 2001 14

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

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 2001 16

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 2001 17

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 2001 18

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 2001 19

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 2001 20

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 2001 21

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 2001 22

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

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 2001 24

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 2001 25

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 2001 26

Deletion: Really Easy Case 1 2 3 Delete(17) 10 5 15 2 9 12 20 3 17 30 AVL Trees CSE 326 Autumn 2001 27

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 2001 28

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 2001 29

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 2001 30

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 2001 31

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 2001 32

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 2001 33

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 2001 34

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 2001 35

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 2001 36

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 2001 37

Building an AVL Tree Input: sequence of n keys (unordered) 19 3 4 18 7 19 3 4 18 7 Insert each into initially empty AVL tree But, suppose input is already sorted … 3 4 7 18 19 Can we do better than O(n log n)? AVL Trees CSE 326 Autumn 2001 38

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 2001 39

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 2001 40

BuildTree Analysis (Approximate) T(n) = 2T(n/2) + 1 T(n) = 2(2T(n/4)+1) + 1 T(n) = 4T(n/4) + 2 + 1 T(n) = 4(2T(n/8)+1) + 2 + 1 T(n) = 8T(n/8) + 4 + 2 + 1 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 2001 41

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 2001 42

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 2001 43

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 2001 44