Copyright © Aiman Hanna All rights reserved

Slides:



Advertisements
Similar presentations
© 2004 Goodrich, Tamassia AVL Trees v z.
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. 2 AVL Tree AVL trees are balanced. An AVL Tree is a binary search tree such that for every internal node v of T, the heights of the children.
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 (10.2) CSE 2011 Winter April 2015.
Chapter 4: Trees Part II - AVL Tree
CPSC 252 AVL Trees Page 1 AVL Trees Motivation: We have seen that when data is inserted into a BST in sorted order, the BST contains only one branch (it.
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
CS202 - Fundamental Structures of Computer Science II
Data Structures Lecture 11 Fang Yu Department of Management Information Systems National Chengchi University Fall 2010.
CSC311: Data Structures 1 Chapter 10: Search Trees Objectives: Binary Search Trees: Search, update, and implementation AVL Trees: Properties and maintenance.
CSC 213 Lecture 7: Binary, AVL, and Splay Trees. Binary Search Trees (§ 9.1) Binary search tree (BST) is a binary tree storing key- value pairs (entries):
Department of Computer Eng. & IT Amirkabir University of Technology (Tehran Polytechnic) Data Structures Lecturer: Abbas Sarraf Search.
Binary Search Trees1 ADT for Map: Map stores elements (entries) so that they can be located quickly using keys. Each element (entry) is a key-value pair.
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.
AVL Trees Amanuel Lemma CS252 Algoithms Dec. 14, 2000.
CSCE 3110 Data Structures & Algorithm Analysis AVL Trees Reading: Chap. 4, Weiss.
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.
© 2004 Goodrich, Tamassia Binary Search Trees1 CSC 212 Lecture 18: Binary and AVL Trees.
Chapter 10: Search Trees Nancy Amato Parasol Lab, Dept. CSE, Texas A&M University Acknowledgement: These slides are adapted from slides provided with Data.
Binary Search Trees1 Chapter 3, Sections 1 and 2: Binary Search Trees AVL Trees   
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.
Part-D1 Binary Search Trees
COMP9024: Data Structures and Algorithms
BCA-II Data Structure Using C
AVL Trees 5/17/2018 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and M.
Week 7 - Friday CS221.
Binary Search Trees < > =
Search Trees.
AVL Trees 6/25/2018 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and M.
Lecture 15 AVL Trees Slides modified from © 2010 Goodrich, Tamassia & by Prof. Naveen Garg’s Lectures.
CS202 - Fundamental Structures of Computer Science II
CS202 - Fundamental Structures of Computer Science II
Red-Black Trees 9/12/ :44 AM AVL Trees v z 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
Chapter 2: Basic Data Structures
AVL Trees 11/10/2018 AVL Trees v z AVL Trees.
AVL Trees 4/29/15 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and M. H.
Red-Black Trees 11/13/2018 2:07 AM AVL Trees v z AVL Trees.
TCSS 342, Winter 2006 Lecture Notes
Bucket-Sort and Radix-Sort
Copyright © Aiman Hanna All rights reserved
Breadth-First Search (BFS)
Data Structures and Programming Techniques
Multi-Way Search Trees
Red-Black Trees 11/26/2018 3:42 PM AVL Trees v z AVL Trees.
CS202 - Fundamental Structures of Computer Science II
Red-Black Trees 2018年11月26日3时46分 AVL Trees v z AVL Trees.
Ordered Maps & Dictionaries
v z Chapter 10 AVL Trees Acknowledgement: These slides are adapted from slides provided with Data Structures and Algorithms in C++, Goodrich,
1 Lecture 13 CS2013.
Copyright © Aiman Hanna All rights reserved
Binary Search Trees < > =
CSE 332: Data Abstractions AVL Trees
AVL Trees 2/23/2019 AVL Trees v z AVL Trees.
CS202 - Fundamental Structures of Computer Science II
Red-Black Trees 2/24/ :17 AM AVL Trees v z AVL Trees.
Copyright © Aiman Hanna All rights reserved
Copyright © Aiman Hanna All rights reserved
Copyright © Aiman Hanna All rights reserved
Red-Black Trees 5/19/2019 6:39 AM AVL Trees v z AVL Trees.
1 Lecture 13 CS2013.
CS202 - Fundamental Structures of Computer Science II
CS202 - Fundamental Structures of Computer Science II
Dictionaries 二○一九年九月二十四日 ADT for Map:
CS210- Lecture 19 July 18, 2005 Agenda AVL trees Restructuring Trees
AVL Trees: AVL Trees: Balanced binary search tree
Presentation transcript:

Copyright © 2011-2016 Aiman Hanna All rights reserved AVL Trees Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia University, Montreal, Canada These slides has been extracted, modified and updated from original slides of : Data Structures and Algorithms in Java, 5th edition. John Wiley& Sons, 2010. ISBN 978-0-470-38326-1. Data Structures and the Java Collections Framework by William J. Collins, 3rdedition, ISBN 978-0-470-48267-4. Both books are published by Wiley. Copyright © 2010-2011 Wiley Copyright © 2010 Michael T. Goodrich, Roberto Tamassia Copyright © 2011 William J. Collins Copyright © 2011-2016 Aiman Hanna All rights reserved

Coverage v z Section 10.2: AVL Trees 6 3 8 4 NOTES: For illustrative animation, please see: http://www.youtube.com/watch?v=vTc5eMBc92k, or click on the above self-balancing link above. AVL trees are named after its their Soviet inventors, G. M. Adelson-Velskii and E. M. Landis, in 1962. AVL Trees were the first invented self-balancing tress. AVL Trees

Performance of Binary Search Trees A binary search tree should be an efficient data structure for map implementation. However, as seen, binary search trees may have O(n) in the worst case, which is not any better than list-based or array-based map implementation. That problem is caused by the possibility that the nodes may be arranged such that n = h + 1, where h is the height of the tree. AVL Trees

AVL Tree Definition The performance problem of binary search trees can be corrected using AVL trees. AVL Trees are balanced Trees. An AVL Tree is a binary search tree such that for every internal node v of T, the heights of the children of v can differ by at most 1; this is referred to as the Height-Balance Property. An example of an AVL tree where the heights are shown next to the nodes AVL Trees

3 4 n(1) n(2) Height of an AVL Tree Any binary search tree that satisfies the height-balance property is said to be an AVL tree. AVL trees are named after their inventors (Adel’son Vel’skii and Landis). Fact: The height of an AVL tree storing n keys is O(log n). Proof: Let us bound n(h): the minimum number of internal nodes of an AVL tree of height h. We can easily see that n(1) = 1 and n(2) = 2, since an AVL tree of height 1 must have at least 1 internal node and an AVL of height 2 must have at least 2 internal nodes. 3 4 n(1) n(2) AVL Trees

3 4 n(1) n(2) Height of an AVL Tree Now, for n >= 3, an AVL tree of height h contains the root node, and at minimum, one AVL subtree of height h-1 and another of height h-2. That is, for n >= 3: n(h) = 1 + n(h-1) + n(h-2) Knowing that n(h-1) > n(h-2), we get n(h) > 2n(h-2), which indicates that: n(h) at least doubles each time h increases by 2. So: n(h) > 2n(h-2), n(h) > 4n(h-4), n(h) > 8n(h-6), … (by induction),  n(h) > 2in(h-2i) AVL Trees

3 4 n(1) n(2) Height of an AVL Tree  n(h) > 2in(h-2i), for any integer i such that h-2i >= 1. Recall that we already know n(1) and n(2). So, let us pick up i such that h – 2i is equal to either 1 or 2: i = ceil(h/2) – 1  n(h) > 2in(h-2i)  n(h) > 2ceil(h/2) – 1 n(h- (2ceil(h/2)) +2)  n(h) > 2ceil(h/2) – 1 n(h- (2ceil(h/2)) +2)  n(h) > 2ceil(h/2) – 1 n(h- (ceil(h/2) + ceil(h/2)) +2)  n(h) > 2ceil(h/2) – 1 n(h- (ceil(h/2) -1 + ceil(h/2) -1))  n(h) > 2ceil(h/2) – 1 n(h- (2i))  n(h) >= 2ceil(h/2) – 1 n(1)  n(h) >= 2ceil(h/2) – 1 The idea is n(h) is bigger than many things. Let us then try to refer to it in relation to anyone that we know, which is n(1) and n(2). Now: Let us consider the > 2i.n(h-2i) we need to force n(h-2i) to become either n(1) or n(2) h-2i = 1 or h-2i = 2 2i = h -1 or 2i = h-2 i = ½ h -1/2 or i= ½ h – 1 This can he generalized as: i = ceil( ½ h) - 1 (since if the ceil does not give anything then it is still -1, and if the ceil gives something, which at most ½ since we divide by 2, then are getting ½ more, or in other words, we have just taken off only ½ (that is – ½)) In the last step, we are already started by assuming that h-2i = 1 or h-2i = 2, so n(h) >= 2^ceil( ½ h) - 1 * n(1) or n(h) >= 2^ceil( ½ h) - 1 * n(2). While it does not really matter as both are constants, we can indicate that n(h) >= the smaller one of them, so we need to consider n(1), effectively have the last statement, where we can finally ignore that constant. In the next slide, if it is more than it with the ceiling, then it is more than it without the ceiling. AVL Trees

3 4 n(1) n(2) Height of an AVL Tree  n(h) >= 2ceil(h/2) – 1  n(h) >= 2h/2 – 1  log n(h) > (h/2) -1  h < 2 log n(h) + 2 This implies that the height of an AVL tree storing n entries can, at most, be 2 log n + 2. Thus the height of an AVL tree is O(log n). Consequently, a map implemented with an AVL tree runs in O(log n). AVL Trees

Insertion Insertion is as in a binary search tree (Always done by expanding an external node), but additional computations are needed afterwards. Example: put(54) 44 17 78 32 50 88 48 62 44 17 78 32 50 88 48 62 54 c=z a=y b=x w before insertion AVL Trees after insertion

Insertion Insertion adds the new node at a previously external node then creates two external nodes to this new added node. This would hence increases the height of that part of the tree which may violates the height-balance property, resulting in the tree being unbalanced. Consequently, the tree may have to be “restructured” to restore the height-balance. A balanced tree would have the difference (absolute value) between the heights of its left and right children being at most 1. NOTE: If a tree is balanced, then each of its internal nodes (subtrees at these nodes) must be balanced as well. AVL Trees after insertion

Trinode Restructuring If the tree becomes unbalanced after the addition, then the nodes in the path from this new node to the root have to be the only involved nodes where the unbalance has occurred. Let w be the new added node causing the unbalance to occur. Let z be the first encountered node from w up towards the root when the unbalance started to occur. Let y be the child of z with the higher height. Finally, let x be the child of y with the higher height. 88 44 17 78 32 50 48 62 2 5 1 3 4 54 T x y z w Unbalanced, after put(54)

Trinode Restructuring We now need to rebalance the tree rooted at z. The rebalancing method is referred to as trinode restructuring. Trinode restructuring starts by temporarily renaming x, y and z to a, b and c, where a precedes b and b precedes c in an inorder traversal (see illustrative example). There are actually 4 possible ways for mapping x, y and z to a, b and c. T0 T1 T2 T3 a = z b = y c = x T1 T3 a = z c = y b = x T0 T2 T1 T3 c = z a = y b = x T0 T2 T1 T3 c = z b = y a = x T0 T2

Trinode Restructuring Trinode restructuring rebalances the tree as follows: 1) Replace z with the node called b (that is, direct the edge heading to z from its parent to b instead of z). 2) Take a and c and make them children of b. 3) Take the 4 subtrees (T0, T1, T2, and T3, which were the previous children of x, y and z) and make them children of a and c. Maintain the inorder relationship between these 4 subtrees when placing them as the children of a and c.  The trinode restructuring adjustments locally the balance of the originally unbalanced subtree at the older node z, which actually results in the entire tree, globally, becoming balanced again. T0 T1 T2 T3 a = z b = y c = x b = y a = z c = x T0 T1 T2 T3

Trinode Restructuring The restructuring of the first two cases are referred to as single rotation (since geometrically can be visualized as rotating y over z. T0 T1 T2 T3 a = z b = y c = x T0 T1 T2 T3 a = z b = y c = x single rotation T1 T3 c = z b = y a = x T0 T2 T0 T1 T2 T3 a = x b = y c = z single rotation

Trinode Restructuring The restructuring of the other two cases are referred to as double rotation (since geometrically can be visualized as rotating x over y then rotating x over z. T0 T1 T2 T3 a = z b = x c = y T1 T3 a = z c = y b = x T0 T2 double rotation T1 T3 c = z a = y b = x T0 T2 T0 T1 T2 T3 a = y b = x c = z double rotation

Insertion Example unbalanced... ...balanced b=x a=y c=z T T T T T c=z 88 44 17 78 32 50 48 62 2 5 1 3 4 54 T b=x a=y c=z w unbalanced... 4 T 1 44 b=x 2 3 17 a=y 62 c=z 1 2 2 32 50 78 1 1 1 ...balanced 48 54 88 T 2 T T 1 T AVL Trees 3

Removal Removal begins as in a binary search tree, which means that the removed node will become an empty external node. Consequently, however, its parent, w, may cause an imbalance. Example: 44 17 78 32 50 88 48 62 54 44 17 62 50 78 48 54 88 before deletion of 32 after deletion AVL Trees

Rebalancing after a Removal Let z be the first unbalanced node encountered while travelling up the tree from w. Also, let y be the child of z with the larger height, and let x be the child of y with the larger height We perform restructure(x) to restore balance at z 62 a=z 44 44 78 w 17 62 b=y 17 50 88 c=x 50 78 48 54 48 54 88 AVL Trees

Rebalancing after a Removal Unfortunately, this restructuring may upset the balance of another node higher in the tree. That is, locally adjusting the tree does not guarantee the tree being adjusted globally. For instance, assume a node has height 6 with its children having heights of 5 and 4, and the one with height 4 has children with heights of 3 and 2. If the removal is taken from the child with height 2, then that tree height becomes 1, which is unbalanced with its sibling (having 3). The restructuring will turn these two children to height 2, which is balanced, but this will consequently change the height of their parent from 4 to 3, causing further unbalance between this node (now having height 3) and its sibling, which has height 5. Thus, we must continue checking for balance until the root of T is reached, and readjust along the way if needed (which may result in log n constructions in the worst case). AVL Trees

AVL Tree Performance a single restructure takes O(1) time using a linked-structure binary tree get takes O(log n) time height of tree is O(log n), no restructures needed put takes O(log n) time initial find is O(log n) Restructuring up the tree, maintaining heights is O(1) However, we still need O(log n) to find out if restructuring is needed remove takes O(log n) time Restructuring up the tree, maintaining heights is O(log n) AVL Trees