AVL Trees 1. 2 Outline Background Define balance Maintaining balance within a tree –AVL trees –Difference of heights –Rotations to maintain balance.

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 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.
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.
Chapter 4: Trees Part II - AVL Tree
AVL Trees COL 106 Amit Kumar Shweta Agrawal Slide Courtesy : Douglas Wilhelm Harder, MMath, UWaterloo
ECE 250 Algorithms and Data Structures Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo,
AVL Tree Smt Genap Outline AVL Tree ◦ Definition ◦ Properties ◦ Operations Smt Genap
Time Complexity of Basic BST Operations Search, Insert, Delete – These operations visit the nodes along a root-to- leaf path – The number of nodes encountered.
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
AVL Trees Balanced Trees. AVL Tree Property A Binary search tree is an AVL tree if : –the height of the left subtree and the height of the right subtree.
Balanced Binary Search Trees
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:
AVL-Trees (Part 1: Single Rotations) Lecture COMP171 Fall 2006.
1 Binary Search Trees Implementing Balancing Operations –AVL Trees –Red/Black Trees Reading:
Self-Balancing Search Trees Chapter 11. Chapter 11: Self-Balancing Search Trees2 Chapter Objectives To understand the impact that balance has on the performance.
Self-Balancing Search Trees Chapter 11. Chapter Objectives  To understand the impact that balance has on the performance of binary search trees  To.
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.
Chapter 13 Binary Search Trees. Copyright © 2005 Pearson Addison-Wesley. All rights reserved Chapter Objectives Define a binary search tree abstract.
ECE 250 Algorithms and Data Structures Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo,
More Trees COL 106 Amit Kumar and Shweta Agrawal Most slides courtesy : Douglas Wilhelm Harder, MMath, UWaterloo
ECE 250 Algorithms and Data Structures Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo,
CSE373: Data Structures & Algorithms Lecture 7: AVL Trees Nicki Dell Spring 2014 CSE373: Data Structures & Algorithms1.
CSE373: Data Structures & Algorithms Optional Slides: AVL Delete Dan Grossman Fall 2013.
INTRODUCTION TO AVL TREES P. 839 – 854. INTRO  Review of Binary Trees: –Binary Trees are useful for quick retrieval of items stored in the tree –order.
CS 146: Data Structures and Algorithms June 23 Class Meeting Department of Computer Science San Jose State University Summer 2015 Instructor: Ron Mak
SELF-BALANCING SEARCH TREES Chapter 9. Self-Balancing Search Trees  The performance of a binary search tree is proportional to the height of the tree.
BINARY SEARCH TREE. Binary Trees A binary tree is a tree in which no node can have more than two children. In this case we can keep direct links to the.
Balancing Binary Search Trees. Balanced Binary Search Trees A BST is perfectly balanced if, for every node, the difference between the number of nodes.
MA/CSSE 473 Day 21 AVL Tree Maximum height 2-3 Trees Student questions?
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.
1 Binary Search Trees (Continued) Study Project 3 Solution Balanced Binary Search Trees Balancing Operations Reading: L&C 11.1 – 11.4.
Chapter 19: Binary Search Trees or How I Learned to Love AVL Trees and Balance The Tree Group 6: Tim Munn.
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.
ECE 250 Algorithms and Data Structures Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo,
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.
Tree Data Structures. Heaps for searching Search in a heap? Search in a heap? Would have to look at root Would have to look at root If search item smaller.
AVL Trees and Heaps. AVL Trees So far balancing the tree was done globally Basically every node was involved in the balance operation Tree balancing can.
1 CSC TREES AVL & BALANCED TREES. 2 Balanced Trees The advantage of balanced trees is that we can perform most operation in time proportional to.
CSE373: Data Structures & Algorithms Lecture 7: AVL Trees Linda Shapiro Winter 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.
1 Binary search trees Outline This topic covers binary search trees: –Abstract Sorted Lists –Background –Definition and examples –Implementation: Front,
Balancing Binary Search Trees. Balanced Binary Search Trees A BST is perfectly balanced if, for every node, the difference between the number of nodes.
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:
Data Structures – LECTURE Balanced trees
Balancing Binary Search Trees
AVL Trees.
AVL Trees A BST in which, for any node, the number of levels in its two subtrees differ by at most 1 The height of an empty tree is -1. If this relationship.
CS 201 Data Structures and Algorithms
CSE373: Data Structures & Algorithms
CSE373: Data Structures & Algorithms Lecture 7: AVL Trees
AVL Tree Mohammad Asad Abbasi Lecture 12
CSE373: Data Structures & Algorithms Lecture 7: AVL Trees
AVL Trees CENG 213 Data Structures.
Multi-Way Search 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.
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.
CSE 332: Data Abstractions AVL Trees
Lecture 10 Oct 1, 2012 Complete BST deletion Height-balanced BST
Richard Anderson Spring 2016
CSE 373 Data Structures Lecture 8
CS210- Lecture 19 July 18, 2005 Agenda AVL trees Restructuring Trees
Presentation transcript:

AVL Trees 1

2 Outline Background Define balance Maintaining balance within a tree –AVL trees –Difference of heights –Rotations to maintain balance

AVL Trees 3 Background From previous lectures: –Binary search trees store linearly ordered data –Best case height:  (ln(n)) –Worst case height: O(n) Requirement: –Define and maintain a balance to ensure  (ln(n)) height

AVL Trees 4 Prototypical Examples These two examples demonstrate how we can correct for imbalances: starting with this tree, add 1:

AVL Trees 5 Prototypical Examples This is more like a linked list; however, we can fix this…

AVL Trees 6 Prototypical Examples Promote 2 to the root, demote 3 to be 2’s right child, and 1 remains the left child of 2

AVL Trees 7 Prototypical Examples The result is a perfect, though trivial tree

AVL Trees 8 Prototypical Examples Alternatively, given this tree, insert 2

AVL Trees 9 Prototypical Examples Again, the product is a linked list; however, we can fix this, too

AVL Trees 10 Prototypical Examples Promote 2 to the root, and assign 1 and 3 to be its children

AVL Trees 11 Prototypical Examples The result is, again, a perfect tree These examples may seem trivial, but they are the basis for the corrections in the next data structure we will see: AVL trees

AVL Trees 12 AVL Trees We will focus on the first strategy: AVL trees –Named after Adelson-Velskii and Landis Balance is defined by comparing the height of the two sub-trees of a node –An empty tree has height –1 –A tree with a single node has height 0

AVL Trees 13 AVL Trees A binary search tree is said to be AVL balanced if: –The difference in the heights between the left and right sub-trees is at most 1, and –Both sub-trees are themselves AVL trees

AVL Trees 14 AVL Trees AVL trees with 1, 2, 3, and 4 nodes:

AVL Trees 15 AVL Trees Here is a larger AVL tree ( 42 nodes):

AVL Trees 16 AVL Trees The root node is AVL-balanced: –Both sub-trees are of height 4 :

AVL Trees 17 AVL Trees All other nodes (e.g., AF and BL) are AVL balanced –The sub-trees differ in height by at most one

AVL Trees 18 Height of an AVL Tree By the definition of complete trees, any complete binary search tree is an AVL tree Thus an upper bound on the number of nodes in an AVL tree of height h a perfect binary tree with 2 h + 1 – 1 nodes –What is an lower bound?

AVL Trees 19 Height of an AVL Tree Let F(h) be the fewest number of nodes in a tree of height h From a previous slide: F(0) = 1 F(1) = 2 F(2) = 4 Can we find F(h) ?

AVL Trees 20 Height of an AVL Tree The worst-case AVL tree of height h would have: –A worst-case AVL tree of height h – 1 on one side, –A worst-case AVL tree of height h – 2 on the other, and –The root node We get: F(h) = F(h – 1) F(h – 2)

AVL Trees 21 Height of an AVL Tree This is a recurrence relation: The solution?

AVL Trees 22 Height of an AVL Tree Use Maple: > rsolve( {F(0) = 1, F(1) = 2, F(h) = 1 + F(h - 1) + F(h - 2)}, F(h) ); > asympt( %, h );

AVL Trees 23 Height of an AVL Tree This is approximately F(h) ≈  h where  ≈ is the golden ratio –That is, F(h) =  (  h ) Thus, we may find the maximum value of h for a given n : log  ( n / ) = log  ( n ) –

AVL Trees 24 Height of an AVL Tree In this example, n = 88, the worst- and best-case scenarios differ in height by only 2

AVL Trees 25 Height of an AVL Tree If n = 10 6, the bounds on h are: –The minimum height: log 2 ( 10 6 ) – 1 ≈ 19 –the maximum height : log  ( 10 6 / ) < 28

AVL Trees 26 Maintaining Balance To maintain AVL balance, observe that: –Inserting a node can increase the height of a tree by at most 1 –Removing a node can decrease the height of a tree by at most 1

AVL Trees 27 Maintaining Balance Insert either 15 or 42 into this AVL tree

AVL Trees 28 Maintaining Balance Inserting 15 does not change any heights

AVL Trees 29 Maintaining Balance Inserting 42 changes the height of two of the sub-trees

AVL Trees 30 Maintaining Balance To calculate changes in height, the member function must run in O(1) time Our implementation of height is O(n) : template int Binary_search_node ::height() const { return max( ( left() == 0 ) ? 0 : 1 + left()->height(), ( right() == 0 ) ? 0 : 1 + right()->height() ); }

AVL Trees 31 Maintaining Balance Introduce a member variable int tree_height; The member function is now: template int BinarySearchNode ::height() const { return tree_height; }

AVL Trees 32 Maintaining Balance Only insert and erase may change the height –This is the only place we need to update the height –These algorithms are already recursive

AVL Trees 33 Insert template void AVL_node ::insert( const Comp & obj ) { if ( obj < element ) { if ( left() == 0 ) { left_tree = new AVL_node ( obj ); tree_height = 1; } else { left()->insert( obj ); tree_height = max( tree_height, 1 + left()->height() ); } } else if ( obj > element ) { //... }

AVL Trees 34 Maintaining Balance Using the same, tree, we mark the stored heights:

AVL Trees 35 Maintaining Balance Again, consider inserting 42 –We update the heights of 38 and 44:

AVL Trees 36 Maintaining Balance If a tree is AVL balanced, for an insertion to cause an imbalance: –The heights of the sub-trees must differ by 1 –The insertion must increase the height of the deeper sub-tree by 1

AVL Trees 37 Maintaining Balance Starting with our sample tree

AVL Trees 38 Maintaining Balance Inserting 23: –4 nodes change height

AVL Trees 39 Maintaining Balance Insert 9 –Again 4 nodes change height

AVL Trees 40 Maintaining Balance In both cases, there is one deepest node which becomes unbalanced

AVL Trees 41 Maintaining Balance Other nodes may become unbalanced –E.g., 36 in the left tree is also unbalanced We only have to fix the imbalance at the lowest point

AVL Trees 42 Maintaining Balance Inserting 23 –Lowest imbalance at 17 –The root is also unbalanced

AVL Trees 43 Maintaining Balance A quick rearrangement solves all the imbalances at all levels

AVL Trees 44 Maintaining Balance Insert 31 –Lowest imbalance at 17

AVL Trees 45 Maintaining Balance A simple rearrangement solves the imbalances at all levels

AVL Trees 46 Maintaining Balance: Case 1 Consider the following setup –Each blue triangle represents a tree of height h

AVL Trees 47 Maintaining Balance: Case 1 Insert a into this tree: it falls into the left subtree B L of b –Assume B L remains balanced –Thus, the tree rooted at b is also balanced

AVL Trees 48 Maintaining Balance: Case 1 However, the tree rooted at node f is now unbalanced –We will correct the imbalance at this node

AVL Trees 49 Maintaining Balance: Case 1 We will modify these three pointers –At this point, this references the unbalanced root node f

AVL Trees 50 Maintaining Balance: Case 1 Specifically, we will rotate these two nodes around the root: –Recall the first prototypical example –Promote node b to the root and demote node f to be the right child of b AVL_node *pl = left(), *BR = left()->right();

AVL Trees 51 Maintaining Balance: Case 1 This requires the address of node f to be assigned to the right_tree member variable of node b AVL_node *pl = left(), *BR = left()->right(); pl->right_tree = this;

AVL Trees 52 Maintaining Balance: Case 1 Assign any former parent of node f to the address of node b Assign the address of the tree B R to left_tree of node f AVL_node *pl = left(), *BR = left()->right(); pl->right_tree = this; ptr_to_this = pl; left_node = BR;

AVL Trees 53 Maintaining Balance: Case 1 The nodes b and f are now balanced and all remaining nodes of the subtrees are in their correct positions

AVL Trees 54 Maintaining Balance: Case 1 Additionally, height of the tree rooted at b equals the original height of the tree rooted at f –Thus, this insertion will no longer affect the balance of any ancestors all the way back to the root

AVL Trees 55 Maintaining Balance: Case 2 Alternatively, consider the insertion of c where b < c < f into our original tree

AVL Trees 56 Maintaining Balance: Case 2 Assume that the insertion of c increases the height of B R –Once again, f becomes unbalanced

AVL Trees 57 Maintaining Balance: Case 2 Unfortunately, the previous rotation does not fix the imbalance at the root of this subtree: the new root, b, remains unbalanced

AVL Trees 58 Maintaining Balance: Case 2 Unfortunately, this does not fix the imbalance at the root of this subtree

AVL Trees 59 Maintaining Balance: Case 2 Re-label the tree by dividing the left subtree of f into a tree rooted at d with two subtrees of height h – 1

AVL Trees 60 Maintaining Balance: Case 2 Now an insertion causes an imbalance at f –The addition of either c or e will cause this

AVL Trees 61 Maintaining Balance: Case 2 We will reassign the following pointers AVL_node *pl = left(), *plr = left()->right(), *LRL = left()->right()->left(), *LRR = left()->right()->right();

AVL Trees 62 Maintaining Balance: Case 2 Specifically, we will order these three nodes as a perfect tree –Recall the second prototypical example

AVL Trees 63 Maintaining Balance: Case 2 To achieve this, b and f will be assigned as children of the new root d AVL_node *pl = left(), *plr = left()->right(), *LRL = left()->right()->left(), *LRR = left()->right()->right(); plr->left_tree = pl; plr->right_tree = this;

AVL Trees 64 Maintaining Balance: Case 2 We also have to connect the two subtrees and original parent of f AVL_node *pl = left(), *plr = left()->right(), *LRL = left()->right()->left(), *LRR = left()->right()->right(); plr->left_tree = pl; plr->right_tree = this; ptr_to_this = lrptr; pl->right_tree = LRL; left_tree = LRR;

AVL Trees 65 Maintaining Balance: Case 2 Now the tree rooted at d is balanced

AVL Trees 66 Maintaining Balance: Case 2 Again, the height of the root did not change

AVL Trees 67 Rotations: Summary There are two symmetric cases to those we have examined: –insertions into either the left-left sub-tree or the right-right sub-tree which cause an imbalance require a single rotation to balance the node –insertions into either the left-right sub-tree or the right-left sub-tree which cause an imbalance require two rotations to balance the node

AVL Trees 68 Insert (Implementation) template void AVL_node ::insert( const Comp & obj ) { if ( obj < element ) { if ( left() == 0 ) { // cannot cause an AVL imbalance left_tree = new AVL_node ( obj ); tree_height = 1; } else { left()->insert( obj ); if ( left()->height() - right()->height() == 2 ) { // determine if it is a left-left or left-right insertion // perform the appropriate rotation } } else if ( obj > element ) { //...

AVL Trees 69 Insertion (Implementation) Comments: –All the rotation statements are  (1) –An insertion requires at most 2 rotations –All insertions are still  (ln(n)) –It is possible to tighten the previous code –Aside if you want to explicitly rotate the nodes A and B, you must also pass a reference to the parent pointer as an argument: insert( Object obj, AVL_node * & parent )

AVL Trees 70 Example Rotations Consider the following AVL tree

AVL Trees 71 Example Rotations Usually an insertion does not require a rotation

AVL Trees 72 Example Rotations Inserting 71 cause a right-right imbalance at 61

AVL Trees 73 Example Rotations A single rotation balances the tree

AVL Trees 74 Example Rotations The insertion of 46 causes a right-left imbalance at 42

AVL Trees 75 Example Rotations First, we rotate outwards with 43-48

AVL Trees 76 Example Rotations We balance the tree by rotating 42-43

AVL Trees 77 Example Rotations Inserting 37 causes a left-left imbalance at 42

AVL Trees 78 Example Rotations A single rotation around balances the tree

AVL Trees 79 Example Rotations Inserting 54 causes a left-right imbalance at 48

AVL Trees 80 Example Rotations We start with a rotation around 48-43

AVL Trees 81 Example Rotations We balance the tree by rotating 48-55

AVL Trees 82 Example Rotations Finally, we insert 99, causing a right-right imbalance at the root 36

AVL Trees 83 Example Rotations A single rotation balances the tree Note the weight has been shifted to the left sub-tree

AVL Trees 84 Erase Removing a node from an AVL tree may cause more than one AVL imbalance –Like insert, erase must check after it has been successfully called on a child to see if it caused an imbalance –Unfortunately, it may cause h/2 imbalances that must be corrected Insertions will only cause one imbalance that must be fixed

AVL Trees 85 Erase Consider the following AVL tree

AVL Trees 86 Erase Suppose we erase the front node: 1

AVL Trees 87 Erase While its previous parent, 2, is not unbalanced, its grandparent 3 is –The imbalance is in the right-right subtree

AVL Trees 88 Erase We can correct this with a simple rotation

AVL Trees 89 Erase The node of that subtree, 5, is now balanced

AVL Trees 90 Erase Recursing to the root, however, 8 is also unbalanced –This is a right-left imbalance

AVL Trees 91 Erase Promoting 11 to the root corrects the imbalance

AVL Trees 92 Erase At this point, the node 11 is balanced

AVL Trees 93 Erase Still, the root node is unbalanced –This is a right-right imbalance

AVL Trees 94 Erase Again, a simple rotation fixes the imbalance

AVL Trees 95 Erase The resulting tree is now AVL balanced –Note, few erases will require one rotation, even fewer will require more than one

AVL Trees 96 AVL Trees as Arrays? We previously saw that: –Complete tree can be stored using an array using  (n) memory –An arbitrary tree of n nodes requires O(2 n ) memory Is it possible to store an AVL tree as an array and not require exponentially more memory?

AVL Trees 97 AVL Trees as Arrays? Recall that in the worst case, an AVL tree of n nodes has a height at most log  (n) – Such a tree requires an array of size 2 log  (n) – – 1 We can rewrite this as 2 – n log  (2) ≈ n 1.44 Thus, we would require O(n 1.44 ) memory

AVL Trees 98 AVL Trees as Arrays? While the polynomial behaviour of n 1.44 is not as bad as exponential behaviour, it is still reasonably sub-optimal when compared to the linear growth associated with node- based trees Here we see n and n 1.44 on [0, 1000]

AVL Trees 99 Summary In this topic we have covered: –Insertions and removals are like binary search trees –With each insertion or removal there is at most one correction required a single rotation, or a double rotation; which runs in O(1) time –Depth is O( ln(n) ) ∴ all operations are O( ln(n) )