6 3 8 4 v z Chapter 10 AVL Trees Acknowledgement: These slides are adapted from slides provided with Data Structures and Algorithms in C++, Goodrich,

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
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
Red-Black Trees 二○一七年四月十二日 AVL Trees v z AVL Trees.
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.
CSC401 – Analysis of Algorithms Lecture Notes 6 Dictionaries and Search Trees Objectives: Introduce dictionaries and its diverse implementations Introduce.
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.
CS 253: Algorithms Chapter 13 Balanced Binary Search Trees (Balanced BST) AVL Trees.
CSC 213 – Large Scale Programming Lecture 18: Zen & the Art of O (log n ) Search.
Binary Search Trees1 Chapter 3, Sections 1 and 2: Binary Search Trees AVL Trees   
CHAPTER 10 SEARCH TREES ACKNOWLEDGEMENT: THESE SLIDES ARE ADAPTED FROM SLIDES PROVIDED WITH DATA STRUCTURES AND ALGORITHMS IN C++, GOODRICH, TAMASSIA AND.
CHAPTER 10.1 BINARY SEARCH TREES    1 ACKNOWLEDGEMENT: THESE SLIDES ARE ADAPTED FROM SLIDES PROVIDED WITH DATA STRUCTURES AND ALGORITHMS.
Part-D1 Binary Search Trees
AA Trees.
COMP9024: Data Structures and Algorithms
Red-Black 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.
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.
Red-Black Trees 5/22/2018 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and.
AVL Trees AVL Trees.
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
AVL DEFINITION An AVL tree is a binary search tree in which the balance factor of every node, which is defined as the difference between the heights of.
Chapter 10.1 Binary Search Trees
Red-Black Trees 9/12/ :44 AM AVL Trees v z 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.
AVL Trees CENG 213 Data Structures.
Red-Black Trees v z Red-Black Trees 1 Red-Black Trees
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.
Copyright © Aiman Hanna All rights reserved
TREES– Interesting problems
1 Lecture 13 CS2013.
Binary Search Trees < > =
AVL Trees CSE 373 Data Structures.
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.
Red-Black Trees 5/19/2019 6:39 AM AVL Trees v z AVL Trees.
CSE 373 Data Structures Lecture 8
1 Lecture 13 CS2013.
CS202 - Fundamental Structures of Computer Science II
Splay Trees Binary search trees.
CS202 - Fundamental Structures of Computer Science II
Dictionaries 二○一九年九月二十四日 ADT for Map:
CS210- Lecture 19 July 18, 2005 Agenda AVL trees Restructuring Trees
Presentation transcript:

6 3 8 4 v z Chapter 10 AVL Trees Acknowledgement: These slides are adapted from slides provided with Data Structures and Algorithms in C++, Goodrich, Tamassia and Mount (Wiley 2004) and slides from Nancy M. Amato And Jory Denny 1 1

AVL Tree Definition AVL trees are balanced An AVL Tree is a binary search tree such that for every internal node 𝑣 of 𝑇, the heights of the children of 𝑣 can differ by at most 1 48 62 50 88 78 44 32 17 4 3 2 1 An example of an AVL tree where the heights are shown next to the nodes:

Insertion in an AVL Tree Insertion is as in a binary search tree Always done by expanding an external node. Example insert 54: After Insertion 44 17 78 32 50 88 48 62 54 Before Insertion 44 17 78 32 50 88 48 62

Trinode Restructuring let (𝑎, 𝑏, 𝑐) be an inorder listing of 𝑥, 𝑦, 𝑧 perform the rotations needed to make 𝑏 the topmost node of the three Trinode Restructuring b=y a=z c=x T0 T1 T2 T3 case 2: double rotation (a right rotation about c, then a left rotation about a) (other two cases are symmetrical) c=y b=x a=z T0 T1 T2 T3 b=y a=z c=x T0 T1 T2 T3 b=x c=y a=z T0 T1 T2 T3 case 1: single rotation (a left rotation about a)

Insertion Example, continued 88 44 17 78 32 50 48 62 2 4 1 3 54 T x y z ...balanced unbalanced...

Restructuring Single Rotations

Restructuring Double Rotations

Restructuring Algorithm restructure(𝑥) Let (a,b,c) be a left-to-right (inorder) listing of the nodes x, y, and z. Let (T0,T1,T2,T3) be a left-to-right (inorder) listing of the four subtrees of x, y, and z not rooted at x, y, or z. Replace the subtree rooted at z with a new subtree rooted at b. Let a be the left child of b and let T0 and T1 be the left and right subtrees of a, respectively. Let c be the right child of b and let T2 and T3 be the left and right subtrees of c, respectively.

Exercise AVL Trees Insert into an initially empty AVL tree items with the following keys (in this order). Draw the resulting AVL tree 10, 20, 13, 30, 24, 7

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

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

Rebalancing after a Removal As this restructuring may upset the balance of another node higher in the tree, we must continue checking for balance until the root of 𝑇 is reached This can happen at most 𝑂( log 𝑛 ) times. Why? 44 17 78 50 88 48 62 54 44 17 78 50 88 48 62 54 w c=x b=y a=z

Exercise AVL Trees Insert into an initially empty AVL tree items with the following keys (in this order). Draw the resulting AVL tree 10, 20, 13, 30, 24, 7 Now, remove the item with key 24. Draw the resulting tree Now, remove the item with key 20. Draw the resulting tree

Height of an AVL Tree 3 4 n(1) n(2) Fact: The height of an AVL tree storing 𝑛 keys is 𝑂 log 𝑛 . Proof: Let us bound 𝑛 ℎ : the minimum number of internal nodes of an AVL tree of height ℎ. We easily see that 𝑛 1 =1 and 𝑛 2 =2 For 𝑛>2, an AVL tree of height ℎ contains the root node, one AVL subtree of height ℎ−1 and another of height ℎ−2. That is, 𝑛 ℎ =1+𝑛 ℎ−1 +𝑛 ℎ−2 Knowing 𝑛 ℎ−1 >𝑛 ℎ−2 , we get 𝑛 ℎ >2𝑛 ℎ−2 . So 𝑛 ℎ >2𝑛 ℎ−2 >4𝑛 ℎ−4 >8𝑛 𝑛−6 ,… (by induction), 𝑛 ℎ > 2 𝑖 𝑛 ℎ−2𝑖 Solving the base case we get: 𝑛 ℎ > 2 ℎ 2 −1 Taking logarithms: ℎ<2 log 𝑛(ℎ) +2 Thus the height of an AVL tree is 𝑂( log 𝑛 )

Running Times for AVL Trees A single restructure is 𝑂(1) – using a linked-structure binary tree find 𝑘 takes 𝑂 log 𝑛 time – height of tree is 𝑂 log 𝑛 , no restructures needed put 𝑘, 𝑣 takes 𝑂 log 𝑛 time Initial find is 𝑂 log 𝑛 Restructuring up the tree, maintaining heights is 𝑂 log 𝑛 erase 𝑘 takes 𝑂 log 𝑛 time

Other Types of Self-balancing Trees Splay Trees – A binary search tree which uses an operation splay 𝑥 to allow for amortized complexity of 𝑂 log 𝑛 2, 4 Trees – A multiway search tree where every node stores internally a list of entries and has 2, 3, or 4 children. Defines self- balancing operations Red-Black Trees – A binary search tree which colors each internal node red or black. Self-balancing dictates changes of colors and required rotation operations 9 10 14 2 5 7 9 15 4 6 2 12 7 21