CSE332: Data Abstractions Lecture 7: AVL Trees Dan Grossman Spring 2010.

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

1 AVL-Trees (Adelson-Velskii & Landis, 1962) In normal search trees, the complexity of find, insert and delete operations in search trees is in the worst.
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.
1 CSE 373 AVL trees, continued read: Weiss Ch. 4, section slides created by Marty Stepp
CS202 - Fundamental Structures of Computer Science II
AVL Tree Iris Jiaonghong Shi. AVL tree operations AVL find : – Same as BST find AVL insert : – First BST insert, then check balance and potentially.
Balanced Binary Search Trees
CPSC 335 Height Balanced Trees Dr. Marina Gavrilova Computer Science University of Calgary Canada.
CSE332: Data Abstractions Lecture 9: B Trees Dan Grossman Spring 2010.
CSE332: Data Abstractions Lecture 7: AVL Trees Tyler Robison Summer
CSE332: Data Abstractions Lecture 8: AVL Delete; Memory Hierarchy Dan Grossman Spring 2010.
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.
CSE 326: Data Structures AVL Trees
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.
CSC 2300 Data Structures & Algorithms February 13, 2007 Chapter 4. Trees.
Tirgul 5 This tirgul is about AVL trees. You will implement this in prog-ex2, so pay attention... BTW - prog-ex2 is on the web. Start working on it!
CSC 2300 Data Structures & Algorithms February 16, 2007 Chapter 4. Trees.
CSE373: Data Structures & Algorithms Lecture 7: AVL Trees Nicki Dell Spring 2014 CSE373: Data Structures & Algorithms1.
Properties of BST delete We first do the normal BST deletion: – 0 children: just delete it – 1 child: delete it, connect child to parent – 2 children:
CSE373: Data Structures & Algorithms Optional Slides: AVL Delete Dan Grossman Fall 2013.
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.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 223 – Advanced Data Structures Course Review Midterm.
1 Joe Meehean.  BST efficiency relies on height lookup, insert, delete: O(height) a balanced tree has the smallest height  We can balance an unbalanced.
Balancing Binary Search Trees. Balanced Binary Search Trees A BST is perfectly balanced if, for every node, the difference between the number of nodes.
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.
11/7/20151 Balanced Trees Data Structures Ananda Gunawardena.
CSE373: Data Structures & Algorithms Lecture 6: Binary Search Trees continued Aaron Bauer Winter 2014 CSE373: Data Structures & Algorithms1.
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.
AVL Trees 1. 2 Outline Background Define balance Maintaining balance within a tree –AVL trees –Difference of heights –Rotations to maintain balance.
Data Structures AVL Trees.
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.
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 Catie Baker Spring 2015.
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.
Balancing Binary Search Trees. Balanced Binary Search Trees A BST is perfectly balanced if, for every node, the difference between the number of nodes.
CE 221 Data Structures and Algorithms Chapter 4: Trees (AVL Trees) Text: Read Weiss, §4.4 1Izmir University of Economics.
CSE373: Data Structures & Algorithms Lecture 8: AVL Trees and Priority Queues Linda Shapiro Spring 2016.
CSE332: Data Abstractions Lecture 7: AVL Trees
Data Structures – LECTURE Balanced trees
Lecture 15 AVL Trees Slides modified from © 2010 Goodrich, Tamassia & by Prof. Naveen Garg’s Lectures.
CSE373: Data Structures & Algorithms
CPSC 221: Algorithms and Data Structures Lecture #6 Balancing Act
CS202 - Fundamental Structures of Computer Science II
CS202 - Fundamental Structures of Computer Science II
CS 201 Data Structures and Algorithms
CSE373: Data Structures & Algorithms
CSE373: Data Structures & Algorithms Lecture 7: AVL Trees
CSE373: Data Structures & Algorithms Lecture 7: AVL Trees
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
Instructor: Lilian de Greef Quarter: Summer 2017
CS202 - Fundamental Structures of Computer Science II
David Kaplan Dept of Computer Science & Engineering Autumn 2001
CPSC 221: Algorithms and Data Structures Lecture #6 Balancing Act
CSE373: Data Structures & Algorithms Lecture 5: AVL Trees
AVL Trees CSE 373 Data Structures.
CSE 332: Data Abstractions AVL Trees
CE 221 Data Structures and Algorithms
CS202 - Fundamental Structures of Computer Science II
AVL-Trees (Part 1).
Richard Anderson Spring 2016
CSE 373 Data Structures Lecture 8
CS202 - Fundamental Structures of Computer Science II
CS202 - Fundamental Structures of Computer Science II
Presentation transcript:

CSE332: Data Abstractions Lecture 7: AVL Trees Dan Grossman Spring 2010

2 The AVL Tree Data Structure Structural properties 1.Binary tree property 2.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 15 Spring 2010CSE332: Data Abstractions

An AVL tree?

An AVL tree?

5 The shallowness bound Let S(h) = the minimum number of nodes in an AVL tree of height h –If we can prove that S(h) grows exponentially in h, then a tree with n nodes has a logarithmic height Step 1: Define S(h) inductively using AVL property –S(-1)=0, S(0)=1, S(1)=2 –For h  2, S(h) = 1+S(h-1)+S(h-2) Step 2: Show this recurrence grows really fast –Similar to Fibonacci numbers –Can prove for all h, S(h) >  h – 1 where  is the golden ratio, (1+  5)/2, about 1.62 –Growing faster than 1.6 h is “plenty” exponential h-1 h-2 h

Before we prove it Good intuition from plots comparing: –S(h) computed directly from the definition –((1+  5)/2) h S(h) is always bigger –Graphs aren’t proofs, so let’s prove it Spring 20106CSE332: Data Abstractions

7 The Golden Ratio This is a special number Aside: Since the Renaissance, many artists and architects have proportioned their work (e.g., length:height) to approximate the golden ratio: If (a+b)/a = a/b, then a =  b We will need one special arithmetic fact about  :  2 = ((1+5 1/2 )/2) 2 = (1 + 2*5 1/2 + 5)/4 = (6 + 2*5 1/2 )/4 = ( /2 )/2 = 1 + ( /2 )/2 = 1 + 

The proof Theorem: For all h  0, S(h) >  h – 1 Proof: By induction on h Base cases: S(0) = 1 >  0 – 1 = 0 S(1) = 2 >  1 – 1  0.62 Inductive case (k > 1): Show S(k+1) >  k+1 – 1 assuming S(k) >  k – 1 and S(k-1) >  k-1 – 1 S(k+1) = 1 + S(k) + S(k-1)by definition of S > 1 +  k – 1 +  k-1 – 1by induction =  k +  k-1 – 1 by arithmetic (1-1=0) =  k-1 (  + 1) – 1by arithmetic (factor  k-1 ) =  k-1  2 – 1 by special property of  =  k+1 – 1 by arithmetic (add exponents) Spring 20108CSE332: Data Abstractions S(-1)=0, S(0)=1, S(1)=2 For h  2, S(h) = 1+S(h-1)+S(h-2)

Good news Proof means that if we have an AVL tree, then find is O( log n) But as we insert and delete elements, we need to: 1.Track balance 2.Detect imbalance 3.Restore balance Spring 20109CSE332: Data Abstractions Is this AVL tree balanced? How about after insert(30) ? 15 20

An AVL Tree … 3 value height children Track height at all times! 10 key

AVL tree operations AVL find : –Same as BST find AVL insert : –First BST insert, then check balance and potentially “fix” the AVL tree –Four different imbalance cases AVL delete : –The “easy way” is lazy deletion –Otherwise, like insert we do the deletion and then have several imbalance cases

Insert: detect potential imbalance 1.Insert the new node as in a BST (a new leaf) 2.For each node on the path from the root to the new leaf, the insertion may (or may not) have changed the node’s height 3.So after recursive insertion in a subtree, detect height imbalance and perform a rotation to restore balance at that node All the action is in defining the correct rotations to restore balance Fact that an implementation can ignore: –There must be a deepest element that is imbalanced after the insert (all descendants still balanced) –After rebalancing this deepest node, every node is balanced –So at most one node needs to be rebalanced Spring CSE332: Data Abstractions

Case #1: Example Spring CSE332: Data Abstractions Insert(6) Insert(3) Insert(1) Third insertion violates balance property happens to be at the root What is the only way to fix this?

Fix: Apply “Single Rotation” Single rotation: The basic operation we’ll use to rebalance –Move child of unbalanced node into parent position –Parent becomes the “other” child (always okay in a BST!) –Other subtrees move in only way BST allows (next slide) Spring CSE332: Data Abstractions AVL Property violated here Intuition: 3 must become root new-parent-height = old-parent-height-before-insert 1

The example generalized Node imbalanced due to insertion somewhere in left-left grandchild increasing height –1 of 4 possible imbalance causes (other three coming) First we did the insertion, which would make a imbalanced Spring CSE332: Data Abstractions a Z Y b X h h h h+1 h+2 a Z Y b X h+1 h h h+2 h+3

The general left-left case Node imbalanced due to insertion somewhere in left-left grandchild increasing height –1 of 4 possible imbalance causes (other three coming) So we rotate at a, using BST facts: X < b < Y < a < Z Spring CSE332: Data Abstractions A single rotation restores balance at the node –To same height as before insertion (so ancestors now balanced) a Z Y b X h+1 h h h+2 h+3 b ZY a h+1 h h h+2 X

Another example: insert(16) Spring CSE332: Data Abstractions

Another example: insert(16) Spring CSE332: Data Abstractions

The general right-right case Mirror image to left-left case, so you rotate the other way –Exact same concept, but need different code Spring CSE332: Data Abstractions a ZY X h h h+1 h+3 b h+2 b Z Y a X h h h+1 h+2

Two cases to go Unfortunately, single rotations are not enough for insertions in the left-right subtree or the right-left subtree Simple example: insert (1), insert (6), insert (3) –First wrong idea: single rotation like we did for left-left Spring CSE332: Data Abstractions

Two cases to go Unfortunately, single rotations are not enough for insertions in the left-right subtree or the right-left subtree Simple example: insert (1), insert (6), insert (3) –Second wrong idea: single rotation on the child of the unbalanced node Spring CSE332: Data Abstractions

Sometimes two wrongs make a right First idea violated the BST property Second idea didn’t fix balance But if we do both single rotations, starting with the second, it works! (And not just for this example.) Double rotation: 1.Rotate problematic child and grandchild 2.Then rotate between self and new child Spring CSE332: Data Abstractions Intuition: 3 must become root

The general right-left case Spring CSE332: Data Abstractions a X b c h-1 h h h V U h+1 h+2 h+3 Z a X c h-1 h+1 h h V U h+2 h+3 Z b h c X h-1 h+1 h V U h+2 Z b h a h

Comments Like in the left-left and right-right cases, the height of the subtree after rebalancing is the same as before the insert –So no ancestor in the tree will need rebalancing Does not have to be implemented as two rotations; can just do: Spring CSE332: Data Abstractions a X b c h-1 h h h V U h+1 h+2 h+3 Z c X h-1 h+1 h V U h+2 Z b h a h Easier to remember than you may think: Move c to grandparent’s position and then put a, b, X, U, V, and Z in the right places to get a legal BST

The last case: left-right Mirror image of right-left –Again, no new concepts, only new code to write Spring CSE332: Data Abstractions a h-1 h h h V U h+1 h+2 h+3 Z X b c c X h-1 h+1 h V U h+2 Z a h b h

Insert, summarized Insert as in a BST Check back up path for imbalance, which will be 1 of 4 cases: –node’s left-left grandchild is too tall –node’s left-right grandchild is too tall –node’s right-left grandchild is too tall –node’s right-right grandchild is too tall Only one case occurs because tree was balanced before insert After the appropriate single or double rotation, the smallest- unbalanced subtree has the same height as before the insertion –So all ancestors are now balanced Spring CSE332: Data Abstractions

Now efficiency Have argued rotations restore AVL property but do they produce an efficient data structure? Worst-case complexity of find : O( log n) Worst-case complexity of insert : O( log n) –A rotation is O(1) and there’s an O( log n) path to root –(Same complexity even without one-rotation-is-enough fact) Worst-case complexity of buildTree : O(n log n) Will take some more rotation action to handle delete … Spring CSE332: Data Abstractions