Lecture 15 AVL Trees Slides modified from © 2010 Goodrich, Tamassia & by Prof. Naveen Garg’s Lectures.

Slides:



Advertisements
Similar presentations
AVL-Trees (Part 2: Double Rotations) Lecture 19 COMP171 Fall 2006.
Advertisements

AVL-Trees (Part 2) COMP171. AVL Trees / Slide 2 A warm-up exercise … * Create a BST from a sequence, n A, B, C, D, E, F, G, H * Create a AVL tree for.
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 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.
AVL Trees COL 106 Amit Kumar Shweta Agrawal Slide Courtesy : Douglas Wilhelm Harder, MMath, UWaterloo
Trees Types and Operations
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
CSC311: Data Structures 1 Chapter 10: Search Trees Objectives: Binary Search Trees: Search, update, and implementation AVL Trees: Properties and maintenance.
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.
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.
Data Structures AVL Trees.
COSC 2007 Data Structures II Chapter 13 Advanced Implementation of Tables II.
CSE332: Data Abstractions Lecture 7: AVL Trees
AVL Trees CSE, POSTECH.
Part-D1 Binary Search Trees
BCA-II Data Structure Using C
CO4301 – Advanced Games Development Week 11 AVL Trees
Data Structures – LECTURE Balanced trees
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.
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.
Balancing Binary Search Trees
Lecture 17 Red-Black Trees
Lecture 16 Multiway Search Trees
CSE373: Data Structures & Algorithms Lecture 7: AVL Trees
CO4301 – Advanced Games Development Week 10 Red-Black Trees continued
Binary Search Tree Chapter 10.
Red-Black Trees 9/12/ :44 AM AVL Trees v z AVL Trees.
AVL Tree 27th Mar 2007.
Dynamic Dictionaries Primary Operations: Additional operations:
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.
AVL Tree A Balanced Binary Search Tree
Red-Black Trees 11/13/2018 2:07 AM AVL Trees v z AVL Trees.
Chapter 6 Transform and Conquer.
Red-Black Trees 11/26/2018 3:42 PM AVL Trees v z AVL Trees.
Red-Black Trees 2018年11月26日3时46分 AVL Trees v z AVL Trees.
v z Chapter 10 AVL Trees Acknowledgement: These slides are adapted from slides provided with Data Structures and Algorithms in C++, Goodrich,
CSE373: Data Structures & Algorithms Lecture 5: AVL Trees
Copyright © Aiman Hanna All rights reserved
CSE 332: Data Abstractions AVL Trees
Lecture No.20 Data Structures Dr. Sohail Aslam
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.
Lecture 9: Self Balancing Trees
AVL-Trees (Part 1).
Binary SearchTrees [CLRS] – Chap 12.
AVL Trees B.Ramamurthy 4/27/2019 BR.
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
AVL-Trees (Part 2).
1 Lecture 13 CS2013.
CS202 - Fundamental Structures of Computer Science II
Dictionaries 二○一九年九月二十四日 ADT for Map:
CS210- Lecture 19 July 18, 2005 Agenda AVL trees Restructuring Trees
Presentation transcript:

Lecture 15 AVL Trees Slides modified from © 2010 Goodrich, Tamassia & by Prof. Naveen Garg’s Lectures

Linked List Priority Queue Stack Heap Queue BST Hash Table Ordered Trees

Most map operations on a BST take O(h)!

How to fix an imbalanced BST? 1 3 4 5 2 7 6

Rotation Keys(T1) < key (v) < keys(T2)<key(u) < keys (T3)

Rotation Keys(T1) < key (v) < keys(T2)<key(u) < keys (T3)

Apply Rotations Iteratively rotate right (6,7), (5,6), and (4,5) rotate right (2,3) rotate left (6,5)

Complete BST 1 3 2 4 5 7 6

Adelson-Velsky and Landis

Rotate while Inserting Itself (7, 6, 5 4, 3, 2, 1)

-if left and right subtrees can differ in height by at most 1 height still remains O(log n) restructuring is easy and O(log n) insert and delete

AVL Trees

Height-Balanced Binary Search Trees

Height Balance Property! For every internal node v of T, the heights of the children of v can differ by at most 1!

Examples 1 3 2 4 5 7 6

Examples 14 10 16 8 11 15 17 5

Examples 14 10 16 8 11 15 17 5 13

Examples 14 10 16 8 11 20 5 17

Examples 44 17 78 32 50 88 48 62 54

Examples 44 17 62 32 50 78 48 54 88

Let the height start from 1, and n(h) be minimum nodes in a tree of height h n(1) = 1 and n(2) = 2 n(h) = 1 + n(h-1) + n(h-2) Since n(h-1) >= n(h-2) n(h) > 2n(h-2) > 4n(h-4) > 8n(h-6) ... > 2in(h-2i) Using n(2) = 2, we get i = h/2-1 n(h) > 2h/2-1*n(2) = 2h/2 Taking logarithms: h < 2log n(h)

What is minimum number of nodes with height h? n(h)>2h/2!

The height of an AVL tree is thus O( log2 n)!

Draw an AVL Tree of height 3 with Minimum number of nodes!

If the closest leaf is at level k, what is the maximum height of the AVL tree?

-given closest leaf at level k -find maximum height of BST? What about a BST? -given closest leaf at level k -find maximum height of BST?

If the closest leaf is k, what can we say about nodes at levels k-2?

Example

If the closest leaf is at level k, all nodes at levels 0. 2 If the closest leaf is at level k, all nodes at levels 0.2..k-2 have 2 children! k

What about level k-1? k

Levels 0 through k-1 are full!

Number of nodes in an AVL tree with closest leaf at level k 2k ≤ n ≤2 2k+1 -1

Question: If the height of an AVL tree is h, where will the closest (to the root) leaf be?

Number of nodes in an AVL tree with closest leaf at level k 2h/2 ≤ n ≤2 h+1 -1

Structural Facts the closest leaf it at level h/2 upto level h/2-1, its complete after that, the density starts decreasing

AVL Tree Operations search insert delete

Insert 15 44 32 78 17 50 88 48 62

Is it balanced? 44 32 78 17 50 88 15 48 62

Re-balanced 44 17 78 15 32 50 88 48 62

Insert 54 44 17 78 32 50 88 48 62

Is it balanced? 44 17 78 32 50 88 48 62 54

1- go up and find the first unbalanced node (Z) 2 – name the child of Z we encountered while going up Y 3 – name the child of Y we encountered while going up X

Imbalanced Tree 44 17 78 Z 32 Y 50 88 48 62 X 54

Imbalanced Tree 44 32 78 Z 17 50 88 Y 15 48 62 X

What are different possibilities of X, Y, and Z?

Case 1 h+3h+4 z h+2h+3 y T4 h+1h+2 x T3 h+1 T1 T2 h+1 hh+1 h

After Right Rotation at Z y h+2 h+2 z x T4 T3 T1 T2 h+1 h+1 h h+1

Case 2 h+3h+4 z h+2h+3 y T4 h+1 T1 x h+1h+2 h+1 T2 T3 hh+1 h

Right Rotation at Z Fails y h+3 z h+2 T1 x h+1 T2 T3 T4 h+1 h+1 h Fails

Case 2 h+3h+4 z h+2h+3 y T4 h+1 T1 x h+1h+2 h+1 T2 T3 hh+1 h

Rotate Left at y h+4 z h+3 x T4 h+1 h+2 T3 y T2 T1 h h+1 h+1

Now Rotate Right at Z h+3 x h+2 h+2 z y T4 T3 T1 T2 h+1 h+1 h h+1

insert summary insert the element check the height balance property on the way up to root rebalance the subtree rooted at the first unbalanced node STOP

Time complexity insert in BTS – O(log n) Checking balance property – O(log n) Maximum rotations – 2 Hence O(log n)

Claim: In a BST, we always delete a leaf or a node with one child!

Delete 99 44 17 78 12 32 50 88 10 13 40 48 62 55 90 49 65 99 7 47 55 45

We can assume that deletion only takes place at leaf in an AVL tree!

Delete 99 44 17 78 12 32 50 88 10 13 40 48 62 55 90 49 65 99 7 47 55 45

1- let z be the first unbalanced node going up 2- let y be the child of z with larger height 3- let x be the child of y with larger height - if both child are of the same height, choose the one in the same direction as parent

Delete 99 44 Z 17 78 Y 12 32 50 88 X 10 13 40 48 62 55 90 49 65 7 47 55 45

Case 1 h+2 z h+1 y T4 h x T3 hh-1 T1 T2 h or h-1 h-1 or h-2

After right rotation at z What do we do if it is h+1? h+1 or h+2 y h h or h+1 x z T1 T2 T3 T4 h-1 or h-2 h-1 or h-2 h or h-1 h-1

Case 2 h+2 z h+1 y T4 h h-1 T1 x h h-1 T2 T3 h-1 or h-2

Rotate Left at y h+2 z h+1 x h-1 T4 h y T3 h-1 or h-2 T2 T1 h-1 or h-2

Tree height reduced, so we need to go up and check! Now rotate right at z h+1 x h h y z T1 T2 T3 T4 h-1 or h-2 h-1 or h-2 h-1 or h-2 h-1 Tree height reduced, so we need to go up and check!

delete summary delete the element until root is reached go to the parent and check height balance property if unbalanced, balance the subtree rooted at that node if height nor reduced, stop

Time Complexity of delete Delete in BST takes O(log n). Rebalancing also requires O(log n) time. O(log n)

Time Complexity of insert We perform rotation only once. We need O(log n) to find unbalanced node. Insert in BST takes O(log n) O(log n)

Time complexity insert – O(n) remove – O(n) search – O(log n)

Comparison of Ordered Map Implementations Sorted Array BST/List AVL Trees insert O(n) O(log n) delete search