AVL Trees. Reading: 9.2

Similar documents
Search Trees. Chapter 11

Splay Trees. Splay Trees 1

Splay Trees Goodrich, Tamassia, Dickerson. Splay Trees 1

Splay Trees 3/20/14. Splay Trees. Splay Trees are Binary Search Trees. note that two keys of equal value may be wellseparated (7,T) (1,Q) (1,C) (5,H)

AVL Tree Definition. An example of an AVL tree where the heights are shown next to the nodes. Adelson-Velsky and Landis

AVL Trees Goodrich, Tamassia, Goldwasser AVL Trees 1

CHAPTER 10 AVL TREES. 3 8 z 4

AVL Trees (10.2) AVL Trees

B Tree. Also, every non leaf node must have at least two successors and all leaf nodes must be at the same level.

Search Trees (Ch. 9) > = Binary Search Trees 1

Search Trees - 1 Venkatanatha Sarma Y

Lecture 16 Notes AVL Trees

Algorithms. AVL Tree

Chapter 10: Search Trees

DATA STRUCTURES AND ALGORITHMS. Hierarchical data structures: AVL tree, Bayer tree, Heap

Lecture Notes on AVL Trees

Binary Trees. Binary Search Trees

Data Structures and Algorithms

Advanced Tree. Structures. AVL Tree. Outline. AVL Tree Recall, Binary Search Tree (BST) is a special case of. Splay Tree (Ch 13.2.

AVL Trees / Slide 2. AVL Trees / Slide 4. Let N h be the minimum number of nodes in an AVL tree of height h. AVL Trees / Slide 6

Self-Balancing Search Trees. Chapter 11

AVL Trees Outline and Required Reading: AVL Trees ( 11.2) CSE 2011, Winter 2017 Instructor: N. Vlajic

COMP171. AVL-Trees (Part 1)

CS350: Data Structures AVL Trees

Data Structures in Java

Analysis of Algorithms

AVL Trees. (AVL Trees) Data Structures and Programming Spring / 17

Chapter 2: Basic Data Structures

Ch04 Balanced Search Trees

CS Transform-and-Conquer

Trees. (Trees) Data Structures and Programming Spring / 28

Balanced Binary Search Trees

ADVANCED DATA STRUCTURES STUDY NOTES. The left subtree of each node contains values that are smaller than the value in the given node.

Binary Search Tree: Balanced. Data Structures and Algorithms Emory University Jinho D. Choi

Module 4: Index Structures Lecture 13: Index structure. The Lecture Contains: Index structure. Binary search tree (BST) B-tree. B+-tree.

AVL trees and rotations

ECE 242 Data Structures and Algorithms. Trees IV. Lecture 21. Prof.

Uses for Trees About Trees Binary Trees. Trees. Seth Long. January 31, 2010

Dynamic Access Binary Search Trees

CS 261 Data Structures. AVL Trees

Data Structures Lesson 7

10.2 AVL Trees. Definition of an AVL Tree. 438 Chapter 10. Search Trees

CS2210 Data Structures and Algorithms. Lecture 9: AVL TREES definition, properties, insertion

Advanced Tree Data Structures

ECE250: Algorithms and Data Structures AVL Trees (Part A)

CISC 235: Topic 4. Balanced Binary Search Trees

Week 2. TA Lab Consulting - See schedule (cs400 home pages) Peer Mentoring available - Friday 8am-12pm, 12:15-1:30pm in 1289CS

Section 4 SOLUTION: AVL Trees & B-Trees

AVL Trees Heaps And Complexity

Dynamic Access Binary Search Trees

B + -trees. Kerttu Pollari-Malmi

Search Trees. COMPSCI 355 Fall 2016

CSI33 Data Structures

CSC 263 Lecture 4. September 13, 2006

COSC160: Data Structures Balanced Trees. Jeremy Bolton, PhD Assistant Teaching Professor

Search Structures. Kyungran Kang

CSCI2100B Data Structures Trees

Binary search trees (chapters )

Search Trees - 2. Venkatanatha Sarma Y. Lecture delivered by: Assistant Professor MSRSAS-Bangalore. M.S Ramaiah School of Advanced Studies - Bangalore

AVL trees and rotations

Balanced Binary Search Trees

CS350: Data Structures Red-Black Trees

DATA STRUCTURES AND ALGORITHMS

Lecture Overview. Readings. Recall: Binary Search Trees (BSTs) The importance of being balanced. AVL trees. Balance Insert. Other balanced trees

Fundamental Algorithms

Binary Search Trees. Analysis of Algorithms

Binary search trees (chapters )

Algorithms and Data Structures

CMPE 160: Introduction to Object Oriented Programming

Trees. Reading: Weiss, Chapter 4. Cpt S 223, Fall 2007 Copyright: Washington State University

CS 206 Introduction to Computer Science II

Augmenting Data Structures

3137 Data Structures and Algorithms in C++

Computer Science 210 Data Structures Siena College Fall Topic Notes: Binary Search Trees

CSE 373 OCTOBER 11 TH TRAVERSALS AND AVL

Balanced Search Trees. CS 3110 Fall 2010

9/29/2016. Chapter 4 Trees. Introduction. Terminology. Terminology. Terminology. Terminology

Advanced Tree Structures

Balanced Binary Search Trees. Victor Gao

Programming II (CS300)

More Binary Search Trees AVL Trees. CS300 Data Structures (Fall 2013)

TREES. Trees - Introduction

More BSTs & AVL Trees bstdelete

Introduction. for large input, even access time may be prohibitive we need data structures that exhibit times closer to O(log N) binary search tree

Multiway Search Trees. Multiway-Search Trees (cont d)

Data Structures and Algorithms for Engineers

Splay Trees. (Splay Trees) Data Structures and Programming Spring / 27

8.1. Optimal Binary Search Trees:

Binary Trees, Binary Search Trees

Week 3 Web site:

CIS265/ Trees Red-Black Trees. Some of the following material is from:

LECTURE 18 AVL TREES

Algorithms. Deleting from Red-Black Trees B-Trees

Balanced BST. Balanced BSTs guarantee O(logN) performance at all times

CSC 172 Data Structures and Algorithms. Fall 2017 TuTh 3:25 pm 4:40 pm Aug 30- Dec 22 Hoyt Auditorium

CS 314H Honors Data Structures Fall 2017 Programming Assignment #6 Treaps Due November 12/15/17, 2017

Computer Science 210 Data Structures Siena College Fall Topic Notes: Binary Search Trees

CS60020: Foundations of Algorithm Design and Machine Learning. Sourangshu Bhattacharya

CS60020: Foundations of Algorithm Design and Machine Learning. Sourangshu Bhattacharya

Part 2: Balanced Trees

Transcription:

AVL Trees Reading: 9.2

Balance Factor of a Node The difference in height of its two subtrees (h R -h L ) Balanced Node if -1 BF 1 Unbalanced Node if BF 1 h L h R

Balance Factor of a Binar Tree Corresponds to the balance factor of its root node left-heav if BF -1 equal-heav if BF=0 right-heav if BF 1 h L h R AVL trees BF of each node in {-1,0,1}

AVL Trees An AVL Tree is a binar search tree such that for ever internal node v of T, the heights of the children of v can differ b at most 1. 2 4 44 17 78 1 2 32 50 1 1 48 62 3 88 1 AVL = Adelson - Velskii - Landis An eample of an AVL tree where the heights are shown net to the nodes:

Properties of AVL Trees 1. The height h of an AVL tree T storing n items is O(log 2 n) 2. The number of nodes n in an AVL tree of height h is Ω(2 h ) Search-algorithm compleit is O(log 2 n) If insert/remove destros balance, it must be restored

Insertion into an AVL Tree Insertion is as in a binar search tree Alwas done b epanding an eternal node. Eample: 54 44 44 17 78 17 78 32 50 88 32 50 88 48 62 48 62 54 before insertion after insertion

Trinode Restructuring Restructuring must maintain the binar search tree propert Inorder traversal must produce the same result on both original and restructured tree Tpes of rotation: 1. Single Left Rotation 2. Single Right Rotation 3. Double Left-Right Rotation 4. Double Right-Left Rotation

Trinode Restructuring Perform the rotations needed to make the topmost node of the three Single Left Rotation: X L Y L X L Y L Z L Z R Z L Z R X L,,Y L,,Z L,,Z R X L,,Y L,,Z L,,Z R

TriNode Restructuring Double Right-Left Rotation X L Y L Y R Z R X L Y L Y R Z R X L Y L Y R Z R X L,,Y L,,Y R,,Z R X L,,Y L,,Y R,,Z R XL,,YL,,YR,,ZR

Reconstruction Required BF=0 BF=1 BF=0 a BF()=1, new node in Z R BF()=1 BF()=2 X L X R Z L Z R b BF()=1, new node in Z L BF()=-1 BF()=2

Reconstruction Required (cont) BF=0 BF=-1 BF=0 c BF()=-1, new node in X L BF()=-1 BF()=2 X L X R Z L Z R d BF()=-1, new node in X R BF()=1 BF()=-2

a)insert Into Right Subtree of Right Child BF=+1 BF=+2 BF=0 BF=0 BF=0 BF=+1 X L X R X L X R 1 Z L Z R Z L Z R 2 N

a)insert Into Right Subtree of Right Child BF=+2 BF=0 BF=0 BF=+1 BF=0 BF=0 Z R X L X R Z L Z R X L X R Z L N 2 N

a)insert Into Right Subtree of Right Child BF=0 BF=0 BF=0 Z R Notes Tree height has decreased Similarl, for case c) we can appl a single-right rotation X L X R Z L N

b)insert Into Left Subtree of Right Child BF=+1 BF=+2 BF=0 BF=0 BF=0 BF=-1 X L X R X L X R 1 Z L Z R Z L Z R 2 N

b)insert Into Left Subtree of Right Child Z R X L X R Z L Z R 2 N X L X R Z L N 2 Single rotation not sufficient, need to detail Z L

b)insert Into Left Subtree of Right Child X L X L 1 Y L Y R Z R Y L Y R Z R 2 N

b)insert Into Left Subtree of Right Child X L X L 2 Y L N Y R Z R Y L N Y R Z R

b)insert Into Left Subtree of Right Child X L Y L Y R X L Y L Y R N Z R N Z R

b)insert Into Left Subtree of Right Child X L Y L Y R N Z R Notes Tree height has decreased Similarl, for case d) we can appl a left-right rotation

Rotation-Based Algorithm if root R of a subtree becomes unbalanced to the right if BF(rightChild(R))=+1 single-left rotation around R if BF(rightChild(R))=-1 single-right rotation around rightchild(r) single-left rotation around R if root R of a subtree becomes unbalanced to the left if BF(leftChild(R))=-1 single-right rotation around R if BF(leftChild(R))=+1 single-left rotation around leftchild(r) single-right rotation around R

Removals from AVL Trees Removal begins as in a binar search tree, which means the node removed will become an empt eternal node. Its parent, w, ma cause an imbalance. Eample: w 17 44 62 w 17 44 62 32 50 78 50 78 48 54 88 48 54 88 before deletion of 32 after deletion

Restructuring after Removal Let be the first unbalanced node encountered while traveling up the tree from w. We restructure the tree to restore balance at. As this restructuring ma upset the balance of another node higher in the tree, we must continue checking for balance until the root of T is reached 44 62 17 62 44 78 50 78 17 50 88 48 54 88 48 54

Restructuring after Removal Let be the first unbalanced node encountered while traveling up the tree from w. Also Let be the child of with the larger height Let be the child of with the larger height. a= 44 w 17 62 b= 50 78 c= 48 54 88

Restructure Algorithm (tetbook) Algorithm restructure() Input: A node of a binar search tree T that has both a parent and a grandparent Output: Tree T restructured b a rotation (either single or double) involving nodes,, and. 1: Let (a, b, c) be an inorder listing of the nodes,, and, and let (T 0, T 1, T 2, T 3 ) be an inorder listing of the the four subtrees of,, and, not rooted at,, or. 2. Replace the subtree rooted at with a new subtree rooted at b 3. Let a be the left child of b and let T 0, T 1 be the left and right subtrees of a, respectivel. 4. Let c be the right child of b and let T 2, T 3 be the left and right subtrees of c, respectivel.

Restructure Algorithm - Eample 17 44 50 62 78 48 54 88

Restructure Algorithm - Eample Let (a,b,c) be the inorder traversal of,, and Let (T 0, T 1, T 2, T 3 ) be an inorder listing of the the four subtrees of,, and, not rooted at,, or. 17 a 44 50 b 62 c 78 T0 48 54 88 T 2 T 3 T1

Restructure Algorithm - Eample Replace the subtree rooted at with a new subtree rooted at b b 62 17 a 44 50 c 78 T0 48 54 88 T 2 T 3 T1

Restructure Algorithm - Eample Let a be the left child of b, and T 0 and T 1 be the left and right subtrees of a, respectivel b a 62 17 T0 78 88 c T 2 T 3 44 50 48 54 T1

Restructure Algorithm - Eample Let c be the right child of b and let T 2, T 3 be the left and right subtrees of c, respectivel. b a 44 62 c 78 17 50 48 54 88 T 2 T 3 T0 T1

Restructure Algorithm - Eample 17 44 50 62 78 48 54 88

Restructure Algorithm - Eample Let (a,b,c) be the inorder traversal of,, and Let (T 0, T 1, T 2, T 3 ) be an inorder listing of the the four subtrees of,, and, not rooted at,, or. 17 a 44 b 50 c 62 78 T0 48 54 88 T1 T 2 T3

Restructure Algorithm - Eample Replace the subtree rooted at with a new subtree rooted at b b 50 17 a 44 62 c 78 T0 48 54 88 T1 T 2 T3

Restructure Algorithm - Eample Let a be the left child of b, and T 0 and T 1 be the left and right subtrees of a, respectivel a 44 b 50 62 c 78 T0 17 48 T1 54 88 T 2 T3

Restructure Algorithm - Eample Let c be the right child of b and let T 2, T 3 be the left and right subtrees of c, respectivel. 17 a 44 48 b 50 54 62 c 78 88 T0 T1 T 2 T3

Restructure vs. Rotations Advantages No case analsis More elegant General Disadvantage Mode comple to implement Same time compleit

Implementation, Running Times for AVL Trees Linked structures are better, restructure of arra-based implementation ver costl A single restructure is O(1) using a linked-structure binar tree Find is O(log n) height of tree is O(log n), no restructures needed Insert is O(log n) initial find is O(log n) Restructuring up the tree, maintaining heights is O(log n) Remove is O(log n) initial find is O(log n) Restructuring up the tree, maintaining heights is O(log n)

AVL Trees - Questions 1. Assume the following tree is a standard BST sorted leicographicall. Draw this tree after the insertion of a new node with element R. 2. Assume the tree is an AVL tree. Draw this tree after the insertion of a new node with element R. 3. Assume the tree is an AVL tree. Draw this tree after the insertion of a new node with element T. B G S P Y

AVL Trees - Eercise Restore the balance of the given AVL tree 4 2 7 1 3 6 15 5 14 16 13

AVL Trees - Eercise 4 1. Identif the newl inserted node that caused the imbalance 1 2 3 6 5 7 13 14 15 16 2. Check BF of all nodes from this node up 3. Identif the lowest unbalanced node, perform rotations/restructuring around that node

AVL Trees - Eercise Restore the balance of the given AVL tree 7 4 13 2 6 11 15 1 3 5 10 14 16 8 16

Spla Trees

Spla Trees Spla trees are binar search trees Entries stored at internal nodes onl Kes stores at nodes in the left subtree of v are less than or equal to the ke stored at v Kes stored at nodes in the right subtree of v are greater than or equal to the ke stored at v Inorder traversal will return the kes in order Similar operations to BSTs

Rotations after Ever Operation new operation: spla splaing moves a node to the root using rotations right rotation makes the left child of a node into s parent; becomes the right child of left rotation makes the right child of a node into s parent; becomes the left child of a right rotation about a left rotation about T 3 T 1 T 1 T 2 T 1 T 2 T 3 T 3 (structure of tree above is not modified) T 2 T 3 (structure of tree above is not modified) T 1 T 2

Spla Tree - Definition a spla tree is a binar search tree where a node is splaed after it is accessed (for a search or update) deepest internal node accessed is splaed splaing costs O(h), where h is height of the tree which is still O(n) worst-case O(h) rotations, each of which is O(1) Amortied cost of spla O(log(n))

Splaing Eample search for =8 p (20,Z) (10,A) g (14,J) (7,T) (1,Q) (8,N) (21,O) (35,R) (36,L) (37,P) (40,X) 1. (before rotating) (1,C) (5,H) (7,P) (10,U) (20,Z) (2,R) (5,I) (5,G) (6,Y) g (10,A) (35,R) (20,Z) p (7,T) (8,N) (10,U) (14,J) (21,O) (36,L) (37,P) (40,X) p (7,T) (8,N) (10,A) g (21,O) (35,R) (37,P) (1,Q) (7,P) (1,C) (5,H) (2,R) (5,G) (5,I) (6,Y) 2. (after first rotation) (1,Q) (7,P) (1,C) (5,H) (2,R) (5,G) (5,I) (6,Y) (10,U) (14,J) (36,L) 3. (after second rotation) (40,X) is not et the root, so we spla again

Splaing Eample search for =8 (20,Z) now is the left child of the root right-rotate around root (8,N) (35,R) (7,T) (10,A) (21,O) (37,P) (1,Q) (7,P) (1,C) (5,H) (2,R) (5,G) (5,I) (6,Y) (10,U) (14,J) (36,L) 1. (before appling rotation) (40,X) (1,C) (1,Q) (7,T) (5,H) (7,P) (8,N) (10,A) (14,J) (20,Z) 2. (after rotation) (21,O) (35,R) (37,P) (2,R) (5,G) (10,U) (36,L) (40,X) (5,I) (6,Y) is the root, so stop

Spla Trees - Conclusions Tree ma not be balanced Amortied cost of splaing is log(n) Adapts to perform searches on frequentlrequested items much faster than O(log(n))