AVL Trees 2/23/2019 AVL Trees 6 3 8 4 v z AVL Trees.

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.
Lecture13: Tree III Bohyung Han CSE, POSTECH CSED233: Data Structures (2014F)
AVL Tree Smt Genap Outline AVL Tree ◦ Definition ◦ Properties ◦ Operations Smt Genap
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.
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.
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.
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.
AVL Trees CSE, POSTECH.
Part-D1 Binary Search Trees
Lecture 15 Nov 3, 2013 Height-balanced BST Recall:
AA Trees.
File Organization and Processing Week 3
COMP9024: Data Structures and Algorithms
BCA-II Data Structure Using C
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.
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.
Balancing Binary Search Trees
CS202 - Fundamental Structures of Computer Science II
CS202 - Fundamental Structures of Computer Science II
Binary Search Tree Chapter 10.
Red-Black Trees 9/12/ :44 AM AVL Trees v z AVL Trees.
AVL Tree 27th Mar 2007.
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.
Chapter 6 Transform and Conquer.
Red-Black Trees v z Red-Black Trees 1 Red-Black 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.
v z Chapter 10 AVL Trees Acknowledgement: These slides are adapted from slides provided with Data Structures and Algorithms in C++, Goodrich,
Copyright © Aiman Hanna All rights reserved
1 Lecture 13 CS2013.
Binary Search Trees < > =
AVL Trees CSE 373 Data Structures.
CSE 332: Data Abstractions 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 Tree By Rajanikanth B.
AVL-Trees (Part 1).
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
CS202 - Fundamental Structures of Computer Science II
Dictionaries 二○一九年九月二十四日 ADT for Map:
CS210- Lecture 19 July 18, 2005 Agenda AVL trees Restructuring Trees
Presentation transcript:

AVL Trees 2/23/2019 AVL Trees 6 3 8 4 v z AVL Trees

AVL Tree Definition Adelson-Velsky and Landis binary search tree balanced each internal node v the heights of the children of v can differ by at most 1 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 Fact: The height of an AVL tree storing n keys is O(log n). Proof (by induction): n(h): the minimum number of internal nodes of an AVL tree of height h. n(1) = 1 and n(2) = 2 For n > 2, an AVL tree of height h contains the root node, one AVL subtree of height n-1 and another of height n-2. That is, n(h) = 1 + n(h-1) + n(h-2) Knowing n(h-1) > n(h-2), we get n(h) > 2n(h-2). So n(h) > 2n(h-2), n(h) > 4n(h-4), n(h) > 8n(n-6), … (by induction), n(h) > 2in(h-2i) Solving the base case we get: n(h) > 2 h/2 - 1 Taking logarithms: h < 2log n(h) +2 Thus the height of an AVL tree is O(log n) AVL Trees

Insertion Insertion is as in a binary search tree Always done by expanding an external node. Insert 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 after insertion AVL Trees

Insertion Insertion is as in a binary search tree Always done by expanding an external node. Insert 54: Imbalance Node z 44 17 78 32 50 88 48 62 44 17 78 32 50 88 48 62 54 c=z a=y b=x Insert Node w w before insertion after insertion AVL Trees

Overview of 4 Cases of Trinode Restructuring 2 6 4 z -> y -> x -> 6 2 4 2 4 6 6 4 2 2 6 4 Red-Black Trees

Rotation operation Consider subTree points to y and we also have x and y y.left = x.right x.right = y subTree = x With a linked structure Constant number of updates O(1) time AVL Trees

Trinode Restructuring: Case 1 Keys: a < b < c Nodes: grandparent z is not balanced, y is parent, x is node Trinode Restructuring: Case 1 Single Rotation: Not balanced at a, the smallest key x has the largest key c Result: middle key b at the top AVL Trees

Example for Case 1 Case 1 T0 T1 T2 T3 z y x T0 T1 T2 T3 2 4 6 4 2 6 Red-Black Trees

Trinode Restructuring: Case 2 Keys: a < b < c Nodes: grandparent z is not balanced, y is parent, x is node Trinode Restructuring: Case 2 Single Rotation: Not balanced at c, the largest key x has the smallest key a Result: middle key b at the top c = z single rotation b = y b = y a = x c = z a = x T 3 T T T 2 T T T 1 2 3 AVL Trees T 1

Example for Case 2 Case 2 z y x T2 T3 T0 T1 T0 T1 T2 T3 6 4 2 4 2 6 Red-Black Trees

Trinode Restructuring: Case 3 Keys: a < b < c Nodes: grandparent z is not balanced, y is parent, x is node Trinode Restructuring: Case 3 double rotation: Not balanced at a, the largest key x has the middle key b x is rotated above y x is then rotated above z Result: middle key b at the top AVL Trees

Example for Case 3 Case 3 T0 T1 T2 T3 z y x T0 T3 T1 T2 T0 T1 T2 T3 2 6 4 2 4 6 z y x T0 T3 T1 T2 2 6 4 T0 T1 T2 T3 Red-Black Trees

Trinode Restructuring: Case 4 Keys: a < b < c Nodes: grandparent z is not balanced, y is parent, x is node Trinode Restructuring: Case 4 double rotation Not balanced at c, the largest key x has the middle key b x is rotated above y x is then rotated above x Result: middle key b at the top T3 T1 T0 T1 T0 T2 T3 T2 AVL Trees

Example for Case 4 Case 4 T2 T3 z y x T3 T0 T1 T0 T1 T2 T0 T1 T2 T3 6 Red-Black Trees

Insert 54 (Case 3 or 4?) Draw the double rotation unbalanced... 88 44 17 78 32 50 48 62 2 5 1 3 4 54 T x y z Draw the double rotation unbalanced... 4 T 1 44 x 2 3 17 62 y z 1 2 2 32 50 78 1 1 1 ...balanced 48 54 88 T 2 T T 1 T AVL Trees 3

Trinode Restructuring summary Case imbalance/ grandparent z Node x Rotation 1 Smallest key a Largest key c single 2 3 Middle key b double 4 AVL Trees

Overview of 4 Cases of Trinode Restructuring 2 6 4 z -> y -> x -> 6 2 4 2 4 6 6 4 2 2 6 4 Red-Black Trees

Trinode Restructuring Summary Case imbalance/ grandparent z Node x Rotation 1 Smallest key a Largest key c single 2 3 Middle key b double 4 The resulting balanced subtree has: middle key b at the top smallest key a as left child T0 and T1 are left and right subtrees of a largest key c as right child T2 and T3 are left and right subtrees of c AVL Trees

Removal Removal begins as in a binary search tree the node removed will become an empty external node. Its parent, w, may cause an imbalance. Remove 32, imbalance at 44 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 z = first unbalanced node encountered while travelling up the tree from w. y = child of z with the larger height, x = child of y with the larger height trinode restructuring to restore balance at z—Case 1 in example a=z 44 44 17 78 50 88 48 62 54 w b=y 17 62 c=x 50 78 48 54 88 AVL Trees

Rebalancing after a Removal this restructuring may upset the balance of another node higher in the tree continue checking for balance until the root of T is reached a=z 44 44 17 78 50 88 48 62 54 w b=y 17 62 c=x 50 78 48 54 88 AVL Trees

Balanced tree 50 35 70 20 60 45 80 30 10 40 55 1 AVL Trees

Delete 80 50 35 70 20 60 45 80 30 10 40 55 1 AVL Trees

Not balanced at 70 50 35 70 20 60 45 30 10 40 55 1 AVL Trees

Single rotation 50 35 60 20 55 45 70 30 10 40 1 AVL Trees

Anything wrong? 50 35 60 20 55 45 70 30 10 40 1 AVL Trees

Not balanced at 50! 50 35 60 20 55 45 70 30 10 40 1 AVL Trees

AVL Tree Performance n entries O(n) space A single restructuring takes O(1) time using a linked-structure binary tree Operation Worst-case Time Complexity Get/search O(log n) Up to height log n Put/insert O(log n): searching & restructuring Remove/delete O(log n): searching & restructuring up to height log n AVL Trees

AVL Trees balanced Binary Search Tree (BST) Insert/delete operations include rebalancing if needed Worst-case time complexity: O(log n) expected O(log n) for skip lists No duplicated keys in skip lists No moving a bunch of keys in sorted array AVL Trees