CSE 326: Data Structures AVL Trees

Slides:



Advertisements
Similar presentations
AVL Trees CSE 373 Data Structures Lecture 8. 12/26/03AVL Trees - Lecture 82 Readings Reading ›Section 4.4,
Advertisements

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.
AVL Trees COL 106 Amit Kumar Shweta Agrawal Slide Courtesy : Douglas Wilhelm Harder, MMath, UWaterloo
AVL Tree Smt Genap Outline AVL Tree ◦ Definition ◦ Properties ◦ Operations Smt Genap
AVL Trees CS II – Fall /8/2010. Announcements HW#2 is posted – Uses AVL Trees, so you have to implement an AVL Tree class. Most of the code is provided.
CS202 - Fundamental Structures of Computer Science II
CSE332: Data Abstractions Lecture 7: AVL Trees Dan Grossman Spring 2010.
CSE332: Data Abstractions Lecture 7: AVL Trees Tyler Robison Summer
AVL-Trees (Part 1) COMP171. AVL Trees / Slide 2 * Data, a set of elements * Data structure, a structured set of elements, linear, tree, graph, … * Linear:
Tirgul 5 AVL trees.
TCSS 342 AVL Trees v1.01 AVL Trees Motivation: we want to guarantee O(log n) running time on the find/insert/remove operations. Idea: keep the tree balanced.
AVL-Trees (Part 1: Single Rotations) Lecture COMP171 Fall 2006.
AVL Trees / Slide 1 Balanced Binary Search Tree  Worst case height of binary search tree: N-1  Insertion, deletion can be O(N) in the worst case  We.
CSE 326: Data Structures B-Trees Ben Lerner Summer 2007.
CSC 2300 Data Structures & Algorithms February 13, 2007 Chapter 4. Trees.
AVL Trees ITCS6114 Algorithms and Data Structures.
AVL Trees v z. 2 AVL Tree Definition AVL trees are balanced. An AVL Tree is a binary search tree such that for every internal node v of T, the.
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.
1 AVL-Trees: Motivation Recall our discussion on BSTs –The height of a BST depends on the order of insertion E.g., Insert keys 1, 2, 3, 4, 5, 6, 7 into.
1 Balanced Trees There are several ways to define balance Examples: –Force the subtrees of each node to have almost equal heights –Place upper and lower.
Search Trees. Binary Search Tree (§10.1) A binary search tree is a binary tree storing keys (or key-element pairs) at its internal nodes and satisfying.
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.
Binary trees -2 Chapter Threaded trees (depth first) Binary trees have a lot of wasted space: the leaf nodes each have 2 null pointers We can.
11/7/20151 Balanced Trees Data Structures Ananda Gunawardena.
D. ChristozovCOS 221 Intro to CS II AVL Trees 1 AVL Trees: Balanced BST Binary Search Trees Performance Height Balanced Trees Rotation AVL: insert, delete.
Data Structures AVL Trees.
AVL Trees An AVL tree is a binary search tree with a balance condition. AVL is named for its inventors: Adel’son-Vel’skii and Landis AVL tree approximates.
AVL Trees / Slide 1 Height-balanced trees AVL trees height is no more than 2 log 2 n (n is the number of nodes) Proof based on a recurrence formula for.
CSE373: Data Structures & Algorithms Lecture 7: AVL Trees Linda Shapiro Winter 2015.
AVL TREES By Asami Enomoto CS 146 AVL Tree is… named after Adelson-Velskii and Landis the first dynamically balanced trees to be propose Binary search.
AVL Trees AVL (Adel`son-Vel`skii and Landis) tree = – A BST – With the property: For every node, the heights of the left and right subtrees differ at most.
CSE332: Data Abstractions Lecture 7: AVL Trees
Lecture 15 Nov 3, 2013 Height-balanced BST Recall:
Lec 13 Oct 17, 2011 AVL tree – height-balanced tree Other options:
Balancing Binary Search Trees
CSE373: Data Structures & Algorithms
UNIT III TREES.
CSIT 402 Data Structures II
CS202 - Fundamental Structures of Computer Science II
CS202 - Fundamental Structures of Computer Science II
CSE373: Data Structures & Algorithms
CSE373: Data Structures & Algorithms Lecture 7: AVL Trees
CSE373: Data Structures & Algorithms Lecture 7: AVL Trees
Trees (Chapter 4) Binary Search Trees - Review Definition
AVL Trees "The voyage of discovery is not in seeking new landscapes but in having new eyes. " - Marcel Proust.
CSE373: Data Structures & Algorithms Lecture 7: AVL Trees
Monday, April 16, 2018 Announcements… For Today…
Red-Black Trees 11/13/2018 2:07 AM AVL Trees v z AVL Trees.
AVL Trees CENG 213 Data Structures.
Instructor: Lilian de Greef Quarter: Summer 2017
Red-Black Trees 11/26/2018 3:42 PM AVL Trees v z AVL Trees.
CS202 - Fundamental Structures of Computer Science II
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
Red-Black Trees 2/24/ :17 AM AVL Trees v z AVL Trees.
Data Structures Lecture 21 Sohail Aslam.
AVL Trees (a few more slides)
Lecture 10 Oct 1, 2012 Complete BST deletion Height-balanced BST
ITCS6114 Algorithms and Data Structures
Richard Anderson Spring 2016
Red-Black Trees 5/19/2019 6:39 AM AVL Trees v z AVL Trees.
CSE 373 Data Structures Lecture 8
326 Lecture 9 Henry Kautz Winter Quarter 2002
CS202 - Fundamental Structures of Computer Science II
CS202 - Fundamental Structures of Computer Science II
CS210- Lecture 19 July 18, 2005 Agenda AVL trees Restructuring Trees
Self-Balancing Search Trees
Presentation transcript:

CSE 326: Data Structures AVL Trees Ben Lerner Summer 2007

The AVL Balance Condition Adelson-Velskii and Landis The AVL Balance Condition Left and right subtrees of every node have equal heights differing by at most 1 Define: balance(x) = height(x.left) – height(x.right) AVL property: –1  balance(x)  1, for every node x Ensures small depth Will prove this by showing that an AVL tree of height h must have a lot of (i.e. O(2h)) nodes Easy to maintain Using single and double rotations

The AVL Tree Data Structure Structural properties Binary tree property Balance property: balance of every node is between -1 and 1 Result: Worst case depth is O(log n) Ordering property Same as for BST 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

Is this an AVL tree? We need to be able to: 1. Track the balance 2. Detect imbalance 3. Restore balance 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. By the way, is it leftist? No! because of 15 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)

How about these? 6 4 8 1 7 11 12 10 3 11 7 1 8 4 6 2 5

Height of an AVL Tree M(h) = minimum number of nodes in an AVL tree of height h. Basis M(0) = 1, M(1) = 2 Induction M(h) = M(h-1) + M(h-2) + 1 Solution M(h) > h - 1 ( = (1+5)/2  1.62) h h-2 h-1

Proof that M(h) > h Basis: M(0) = 1 > 0 -1, M(1) = 2 > 1-1 Induction step. M(h) = M(h-1) + M(h-2) + 1 > (h-1 - 1) + (h-2 - 1) + 1 = h-1 + h-2 - 1 = h-2 ( +1) - 1 = h - 1 (Uses fact that 2 =  +1)

Height of an AVL Tree M(h) > h (  1.62) Suppose we have n nodes in an AVL tree of height h. N > M(h) N > h - 1 log(N+1) > h (relatively well balanced tree!!)

Node Heights 6 6 4 9 4 9 1 5 1 5 8 2 2 1 1 1 height of node = h 1 1 4 9 4 9 1 5 1 5 8 height of node = h balance factor = hleft-hright empty height = -1

Node Heights after Insert 7 balance factor 1-(-1) = 2 2 3 6 6 1 1 1 2 4 9 4 9 -1 1 1 5 7 1 5 8 7 height of node = h balance factor = hleft-hright empty height = -1

Insert and Rotation in AVL Trees Insert operation may cause balance factor to become 2 or –2 for some node only nodes on the path from insertion point to root node have possibly changed in height So after the Insert, go back up to the root node by node, updating heights If a new balance factor (i.e. the difference hleft-hright) is 2 or –2, adjust tree by rotation around the node

Single Rotation in an AVL Tree 2 2 6 6 1 2 1 1 4 9 4 8 1 1 5 8 1 5 7 9 7

Insertions in AVL Trees Let the node that needs rebalancing be . There are 4 cases: Outside Cases (require single rotation) : 1. Insertion into left subtree of left child of . 2. Insertion into right subtree of right child of . Inside Cases (require double rotation) : 3. Insertion into right subtree of left child of . 4. Insertion into left subtree of right child of . The rebalancing is performed through four separate rotation algorithms.

Where is AVL property violated? Bad Case #1 Insert(6) Insert(3) Insert(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. Where is AVL property violated?

Fix: Apply Single Rotation AVL Property violated at this node (x) 2 1 6 3 1 3 1 6 This is the basic operation we’ll use in AVL trees. Since this is a left child, it could legally have the parent as its right child. When we finish the rotation, we have a balanced tree! 1 Single Rotation: 1. Rotate between x and child

AVL Insertion: Outside Case j Consider a valid AVL subtree k h Z h h X Y

AVL Insertion: Outside Case j Inserting into X destroys the AVL property at node j k h Z h+1 h Y X

AVL Insertion: Outside Case j Do a “rotation from left” k h Z h+1 h Y X

Single rotation from left j k h Z h+1 h Y X

Outside Case Completed k “rotation from left” done! (“rotation from right” is mirror symmetric) j h+1 h h X Z Y AVL property has been restored!

Single rotation example 15 5 20 3 10 17 21 2 4 1 21 10 3 20 5 15 1 2 4 17

Bad Case #2 Insert(1) Insert(6) Insert(3) 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!) Will single rotation fix? (No) (try bringing up Large; doesn’t work)

Fix: Apply Double Rotation Intuition: 3 must become root AVL Property violated at this node (x) 2 2 1 1 1 3 1 1 6 3 1 6 3 6 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? Balanced? Double Rotation 1. Rotate between x’s child and grandchild 2. Rotate between x and x’s new child

AVL Insertion: Inside Case j Consider a valid AVL subtree k h Z h h X Y

AVL Insertion: Inside Case j Inserting into Y destroys the AVL property at node j Does “rotation from left” restore balance? k h Z h h+1 X Y

AVL Insertion: Inside Case k “Rotation from left” does not restore balance… now k is out of balance j h X h h+1 Z Y

AVL Insertion: Inside Case j Consider the structure of subtree Y… k h Z h h+1 X Y

AVL Insertion: Inside Case j Y = node i and subtrees V and W k h Z i h h+1 X h or h-1 V W

AVL Insertion: Inside Case j 2 We will do a “double rotation” . . . k 1 Z i X V W

Double rotation : first rotation j i Z k W V X

Double rotation : second rotation j i Z k W V X

Double rotation : second rotation right rotation complete Balance has been restored to the universe i k j h h h or h-1 V Z X W

Double rotation, step 1 15 8 17 4 10 16 3 6 5 15 8 17 6 10 16 4 3 5

Double rotation, step 2 15 8 17 6 10 16 4 3 5 15 17 6 4 8 16 3 5 10

Imbalance at node X Single Rotation Rotate between x and child Double Rotation Rotate between x’s child and grandchild Rotate between x and x’s new child

Insert into an AVL tree: a b e c d

Single and Double Rotations: Inserting what integer values would cause the tree to need a: single rotation? double rotation? no rotation? 1,14+ 9 5 11 2 7 13 -1,4,12 3 6,8,10 Student Activity

Insertion into AVL tree Find spot for new key Hang new node there with this key Search back up the path for imbalance If there is an imbalance: case #1: Perform single rotation and exit case #2: Perform double rotation and exit Zig-zig 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! Zig-zag Both rotations keep the subtree height unchanged. Hence only one rotation is sufficient!

Easy Insert Insert(3) 10 5 15 2 9 12 20 17 30 Unbalanced? 3 1 2 1 3 No 1 2 9 12 20 Let’s insert 3. This is easy! It just goes under 2 (to the right). Update the balances: any imbalance? NO! 3 17 30 Unbalanced? No

Hard Insert (Bad Case #1) 2 3 Insert(33) 10 Imbalance 5 15 2 9 12 20 Now, let’s insert 33. Where does it go? Right of 30. 3 17 30 Unbalanced? Yes, at 15 How to fix? Single rotate Zig-zig

Single Rotation 1 2 3 1 2 3 10 10 5 15 5 20 2 9 12 20 2 9 15 30 3 17 30 3 12 17 33 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? 33

Hard Insert (Bad Case #2) 1 2 3 Insert(18) 10 Imbalance 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 Unbalanced? Yes, at 15 How to fix? Double rotate Zig-zag

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 3 17 30 3 20 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! 18 18 30 Still unbalanced. But like zig-zig tree!

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

Implementation balance (1,0,-1) key left right

Single Rotation RotateFromRight(n : reference node pointer) { p : node pointer; p := n.right; n.right := p.left; p.left := n; n := p } n X Y Z RotateFromLeft is symmetric

Double Rotation Class participation Implement Double Rotation in two lines. DoubleRotateFromRight(n : reference node pointer) { ???? } n X Z V W

AVL Tree Deletion Similar to insertion Rotations and double rotations needed to rebalance Imbalance may propagate upward so that many rotations may be needed.

Pros and Cons of AVL Trees Arguments for AVL trees: Search is O(log N) since AVL trees are always well balanced. The height balancing adds no more than a constant factor to the speed of insertion, deletion, and find. Arguments against using AVL trees: Difficult to program & debug; more space for height info. Asymptotically faster but rebalancing costs time. Most large searches are done in database systems on disk and use other structures (e.g. B-trees). May be OK to have O(N) for a single operation if total run time for many consecutive operations is fast (e.g. Splay trees).

Double Rotation Solution DoubleRotateFromRight(n : reference node pointer) { RotateFromLeft(n.right); RotateFromRight(n); } n X Z V W