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

Slides:



Advertisements
Similar presentations
Rizwan Rehman Centre for Computer Studies Dibrugarh University
Advertisements

Lecture 9 : Balanced Search Trees Bong-Soo Sohn Assistant Professor School of Computer Science and Engineering Chung-Ang University.
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.
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
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 Balanced Trees. AVL Tree Property A Binary search tree is an AVL tree if : –the height of the left subtree and the height of the right subtree.
1 Theory I Algorithm Design and Analysis (4 – AVL trees: deletion) Prof. Th. Ottmann.
AVL-Trees (Part 1) COMP171. AVL Trees / Slide 2 * Data, a set of elements * Data structure, a structured set of elements, linear, tree, graph, … * Linear:
AVL Trees / Slide 1 Delete Single rotation Deletion.
AVL-Trees (Part 1: Single Rotations) Lecture COMP171 Fall 2006.
CS2420: Lecture 31 Vladimir Kulyukin Computer Science Department Utah State University.
AVL Trees / Slide 1 Balanced Binary Search Tree  Worst case height of binary search tree: N-1  Insertion, deletion can be O(N) in the worst case  We.
CSC 2300 Data Structures & Algorithms February 13, 2007 Chapter 4. Trees.
Tirgul 5 This tirgul is about AVL trees. You will implement this in prog-ex2, so pay attention... BTW - prog-ex2 is on the web. Start working on it!
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.
CSE373: Data Structures & Algorithms Optional Slides: AVL Delete Dan Grossman Fall 2013.
1 Joe Meehean.  BST efficiency relies on height lookup, insert, delete: O(height) a balanced tree has the smallest height  We can balance an unbalanced.
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.
AVL-Trees (Part 1) COMP171. AVL Trees / Slide 2 * Data, a set of elements * Data structure, a structured set of elements, linear, tree, graph, … * Linear:
Data Structures AVL Trees.
AVL Trees / Slide 1 Height-balanced trees AVL trees height is no more than 2 log 2 n (n is the number of nodes) Proof based on a recurrence formula for.
AVL trees1 AVL Trees Height of a node : The height of a leaf is 1. The height of a null pointer is zero. The height of an internal node is the maximum.
CSE373: Data Structures & Algorithms Lecture 7: AVL Trees Linda Shapiro Winter 2015.
CSE 3358 NOTE SET 13 Data Structures and Algorithms.
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.
Red-Black Trees an alternative to AVL trees. Balanced Binary Search Trees A Binary Search Tree (BST) of N nodes is balanced if height is in O(log N) A.
AVL Tree: Balanced Binary Search Tree 9.
Lecture 15 Nov 3, 2013 Height-balanced BST Recall:
Lec 13 Oct 17, 2011 AVL tree – height-balanced tree Other options:
BCA-II Data Structure Using C
G64ADS Advanced Data Structures
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.
AVL Tree Mohammad Asad Abbasi Lecture 12
AVL Tree.
Red-Black Trees 9/12/ :44 AM AVL Trees v z AVL Trees.
Red-Black Trees 11/13/2018 2:07 AM AVL Trees v z AVL Trees.
TCSS 342, Winter 2006 Lecture Notes
AVL Trees: AVL Trees: Balanced binary search tree
Red-Black Trees 11/26/2018 3:42 PM AVL Trees v z AVL Trees.
CS202 - Fundamental Structures of Computer Science II
singleRightRotation 
Data Structures & Algorithms
CS223 Advanced Data Structures and Algorithms
CSE 373: Data Structures and Algorithms
AVL Search Tree put(9)
Lecture No.20 Data Structures Dr. Sohail Aslam
CS223 Advanced Data Structures and Algorithms
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.
Data Structures Lecture 21 Sohail Aslam.
AVL-Trees (Part 1).
Lecture 10 Oct 1, 2012 Complete BST deletion Height-balanced BST
Red-Black Trees 5/19/2019 6:39 AM AVL Trees v z AVL Trees.
AVL-Trees (Part 2).
Red Black Trees Top-Down Deletion.
CS202 - Fundamental Structures of Computer Science II
Red Black Trees Colored Nodes Definition Binary search tree.
CS202 - Fundamental Structures of Computer Science II
CS210- Lecture 19 July 18, 2005 Agenda AVL trees Restructuring Trees
AVL Trees: AVL Trees: Balanced binary search tree
Presentation transcript:

AVL-Trees (Part 2: Double Rotations) Lecture 19 COMP171 Fall 2006

AVL Trees / Slide 2 Review of Rotations When the AVL property is lost we can rebalance the tree via rotations * Single Right Rotation (SRR) n Performed when A is unbalanced to the left (the left subtree is 2 higher than the right subtree) and B is left- heavy (the left subtree of B is 1 higher than the right subtree of B). A BT3 T1 T2 SRR at A B A T3 T1 T2

AVL Trees / Slide 3 Rotations * Single Left Rotation (SLR) n performed when A is unbalanced to the right (the right subtree is 2 higher than the left subtree) and B is right-heavy (the right subtree of B is 1 higher than the left subtree of B). A T1B T2 T3 SLR at A B A T3 T1T2

AVL Trees / Slide 4 Rotations * Double Left Rotation (DLR) n Performed when C is unbalanced to the left (the left subtree is 2 higher than the right subtree), A is right-heavy (the right subtree of A is 1 higher than the left subtree of A) n Consists of a single left rotation at node A, followed by a single right at node C C AT4 T1B SLR at A C BT4 AT3 T2T3T1T2 B AC T1T2 SRR at C T3T4 DLR = SLR + SRR Note: this is case 1

AVL Trees / Slide 5 Rotations * Double Right Rotation (DRR) n Performed when A is unbalanced to the right (the right subtree is 2 higher than the left subtree), C is left heavy (the left subtree of C is 1 higher than the right subtree of C) n Consists of a single right rotation at node C, followed by a single left rotation at node A A T1C BT4 SRR at C A T1B T2C T3 T4 B AC T1T2 SLR at A T3T4 DRR = SRR + SLR Note: this is case 4!

AVL Trees / Slide 6 Recall Cases 2&3 * Single rotation fails to fix case 2&3 * Take case 2 as an example (case 3 is a symmetric to it ) n The problem is that the subtree Y is too deep n Single rotation doesn’t make Y any less deep… Single rotation failsCase 2: violation in k2 because of insertion in subtree Y

AVL Trees / Slide 7 Double Rotation * Facts n The new key is inserted in the subtree B or C n The AVL-property is violated at k 3 n k 3 -k 1 -k 2 forms a zig-zag shape: LR case * Solution n place k 2 as the new root Double rotation to fix case 2

AVL Trees / Slide 8 Double Rotation to fix Case 3(right-left) * Facts n The new key is inserted in the subtree B or C n The AVL-property is violated at k 1 n k 1 -k 3 -k 2 forms a zig-zag shape * Case 3 is a symmetric case to case 2 Double rotation to fix case 3

AVL Trees / Slide 9 * Restart our example We’ve inserted 3, 2, 1, 4, 5, 6, 7, 16 We’ll insert 15, 14, 13, 12, 11, 10, 8, Insert 16, fine Insert 15 violation at node Double rotation k1 k3 k2 k1k3

AVL Trees / Slide Insert 14 k1 k3 k Double rotation k2 k3 5 k1 A C D Insert Single rotation k1 k2 Z X Y

AVL Trees / Slide Insert Single rotation Insert Single rotation 14

AVL Trees / Slide Insert Single rotation Insert 8, fine then insert Double rotation 10

AVL Trees / Slide 13 Insertion Analysis * Insert the new key as a new leaf just as in ordinary binary search tree: O(logN) * Then trace the path from the new leaf towards the root, for each node x encountered: O(logN) n Check height difference: O(1) n If satisfies AVL property, proceed to next node: O(1) n If not, perform a rotation: O(1) * The insertion stops when n A rotation is performed n Or, we’ve checked all nodes in the path * Time complexity for insertion O(logN) logN

AVL Trees / Slide 14 Deletion from AVL Tree * Delete a node x as in ordinary binary search tree n Note that the last (deepest) node in a tree deleted is a leaf or a node with one child * Then trace the path from the new leaf towards the root * For each node x encountered, check if heights of left(x) and right(x) differ by at most 1. n If yes, proceed to parent(x) n If no, perform an appropriate rotation at x Continue to trace the path until we reach the root

AVL Trees / Slide 15 Deletion Example 1 Delete 5, Node 10 is unbalanced Single Rotation

AVL Trees / Slide 16 Cont’d For deletion, after rotation, we need to continue tracing upward to see if AVL-tree property is violated at other node Continue to check parents Oops!! Node 20 is unbalanced!! Single Rotation

AVL Trees / Slide 17 Rotation in Deletion * The rotation strategies (single or double) we learned can be reused here * Except for one new case: two subtrees of y are of the same height  in that case, a single rotation is ok rotate with right child rotate with left child

AVL Trees / Slide 18 Deletion Example 2 Double rotation Right most child of left subtree = I Ok here!

AVL Trees / Slide 19 Example 2 Cont’d New case