singleRightRotation 

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.
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.
1 AVL Trees (10.2) CSE 2011 Winter April 2015.
AVL Tree Smt Genap Outline AVL Tree ◦ Definition ◦ Properties ◦ Operations Smt Genap
CS202 - Fundamental Structures of Computer Science II
AA Trees another 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 balanced.
4.5 AVL Trees  A tree is said to be balanced if for each node, the number of nodes in the left subtree and the number of nodes in the right subtree differ.
AVL trees. AVL Trees We have seen that all operations depend on the depth of the tree. We don’t want trees with nodes which have large height This can.
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.
Balanced Trees AVL Trees Red-Black Trees 2-3 Trees Trees.
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.
D. ChristozovCOS 221 Intro to CS II AVL Trees 1 AVL Trees: Balanced BST Binary Search Trees Performance Height Balanced Trees Rotation AVL: insert, delete.
Data Structures AVL Trees.
CompSci 100E 41.1 Balanced Binary Search Trees  Pathological BST  Insert nodes from ordered list  Search: O(___) ?  The Balanced Tree  Binary Tree.
AVL Trees 1. Balancing a BST Goal – Keep the height small – For any node, left and right sub-tree have approximately the same height Ensures fast (O(lgn))
AVL Trees. AVL Tree In computer science, an AVL tree is the first-invented self-balancing binary search tree. In an AVL tree the heights of the two child.
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.
BSTs, AVL Trees and Heaps Ezgi Shenqi Bran. What to know about Trees? Height of a tree Length of the longest path from root to a leaf Height of an empty.
AVL Tree: Balanced Binary Search Tree 9.
AA Trees.
File Organization and Processing Week 3
Data Structures – LECTURE Balanced trees
Search Trees.
Data Structures Lecture 22 Sohail Aslam.
Balancing Binary Search Trees
CS202 - Fundamental Structures of Computer Science II
CS202 - Fundamental Structures of Computer Science II
Balanced Binary Search Trees
Introduction Applications Balance Factor Rotations Deletion Example
Chapter 26 AVL Trees Jung Soo (Sue) Lim Cal State LA.
Chapter 29 AVL Trees.
Red-Black Trees 9/12/ :44 AM AVL Trees v z AVL Trees.
CSCS-200 Data Structures and Algorithms
Chapter 22 : Binary Trees, AVL Trees, and Priority Queues
AVL Tree A Balanced Binary Search Tree
Red-Black Trees 11/13/2018 2:07 AM AVL Trees v z AVL Trees.
TCSS 342, Winter 2006 Lecture Notes
Binary Search Tree AVL Tree
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
Lecture No.16 Data Structures Dr. Sohail Aslam.
CSE 373: Data Structures and Algorithms
CS223 Advanced Data Structures and Algorithms
CSE 373: Data Structures and Algorithms
Operations on Binary Tree
AVL Search Tree put(9)
CSE 373 Data Structures and Algorithms
Lecture No.20 Data Structures Dr. Sohail Aslam
Non-Linear Structures
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.
short illustrative repetition
AVL Tree By Rajanikanth B.
Deletion in AVL Tree There are 5 cases to consider.
Data Structures Lecture 21 Sohail Aslam.
ITCS6114 Algorithms and Data Structures
Search Sorted Array: Binary Search Linked List: Linear Search
Red-Black Trees 5/19/2019 6:39 AM AVL Trees v z AVL Trees.
AVL-Trees (Part 2).
CS202 - Fundamental Structures of Computer Science II
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:

singleRightRotation  TreeNode<int>* singleRightRotation(TreeNode<int>* k2) { if( k2 == NULL ) return NULL; // k1 (first node in k2's left subtree) // will be the new root TreeNode<int>* k1 = k2->getLeft(); // Y moves from k1's right to k2's left k2->setLeft( k1->getRight() ); k1->setRight(k2); // reassign heights. First k2 int h = Max(height(k2->getLeft()), height(k2->getRight())); k2->setHeight( h+1 ); // k2 is now k1's right subtree h = Max( height(k1->getLeft()), k2->getHeight()); k1->setHeight( h+1 ); return k1; } k2 k1 Z Y X Start of lecture 23 k1 k2 Z Y X

singleRightRotation  TreeNode<int>* singleRightRotation(TreeNode<int>* k2) { if( k2 == NULL ) return NULL; // k1 (first node in k2's left subtree) // will be the new root TreeNode<int>* k1 = k2->getLeft(); // Y moves from k1's right to k2's left k2->setLeft( k1->getRight() ); k1->setRight(k2); // reassign heights. First k2 int h = Max(height(k2->getLeft()), height(k2->getRight())); k2->setHeight( h+1 ); // k2 is now k1's right subtree h = Max( height(k1->getLeft()), k2->getHeight()); k1->setHeight( h+1 ); return k1; }  k2 k1 Z Y X k1 k2 Z Y X

singleRightRotation  TreeNode<int>* singleRightRotation(TreeNode<int>* k2) { if( k2 == NULL ) return NULL; // k1 (first node in k2's left subtree) // will be the new root TreeNode<int>* k1 = k2->getLeft(); // Y moves from k1's right to k2's left k2->setLeft( k1->getRight() ); k1->setRight(k2); // reassign heights. First k2 int h = Max(height(k2->getLeft()), height(k2->getRight())); k2->setHeight( h+1 ); // k2 is now k1's right subtree h = Max( height(k1->getLeft()), k2->getHeight()); k1->setHeight( h+1 ); return k1; } k2 k1  Z Y X k1 k2 Z Y X

singleRightRotation  TreeNode<int>* singleRightRotation(TreeNode<int>* k2) { if( k2 == NULL ) return NULL; // k1 (first node in k2's left subtree) // will be the new root TreeNode<int>* k1 = k2->getLeft(); // Y moves from k1's right to k2's left k2->setLeft( k1->getRight() ); k1->setRight(k2); // reassign heights. First k2 int h = Max(height(k2->getLeft()), height(k2->getRight())); k2->setHeight( h+1 ); // k2 is now k1's right subtree h = Max( height(k1->getLeft()), k2->getHeight()); k1->setHeight( h+1 ); return k1; } k2 k1 Z  Y X k1 k2 Z Y X

singleRightRotation  TreeNode<int>* singleRightRotation(TreeNode<int>* k2) { if( k2 == NULL ) return NULL; // k1 (first node in k2's left subtree) // will be the new root TreeNode<int>* k1 = k2->getLeft(); // Y moves from k1's right to k2's left k2->setLeft( k1->getRight() ); k1->setRight(k2); // reassign heights. First k2 int h = Max(height(k2->getLeft()), height(k2->getRight())); k2->setHeight( h+1 ); // k2 is now k1's right subtree h = Max( height(k1->getLeft()), k2->getHeight()); k1->setHeight( h+1 ); return k1; } k2 k1 Z Y X  k1 k2 Z Y X

singleRightRotation  TreeNode<int>* singleRightRotation(TreeNode<int>* k2) { if( k2 == NULL ) return NULL; // k1 (first node in k2's left subtree) // will be the new root TreeNode<int>* k1 = k2->getLeft(); // Y moves from k1's right to k2's left k2->setLeft( k1->getRight() ); k1->setRight(k2); // reassign heights. First k2 int h = Max(height(k2->getLeft()), height(k2->getRight())); k2->setHeight( h+1 ); // k2 is now k1's right subtree h = Max( height(k1->getLeft()), k2->getHeight()); k1->setHeight( h+1 ); return k1; } k2 k1 Z Y X k1 k2 Z Y X 

singleRightRotation  TreeNode<int>* singleRightRotation(TreeNode<int>* k2) { if( k2 == NULL ) return NULL; // k1 (first node in k2's left subtree) // will be the new root TreeNode<int>* k1 = k2->getLeft(); // Y moves from k1's right to k2's left k2->setLeft( k1->getRight() ); k1->setRight(k2); // reassign heights. First k2 int h = Max(height(k2->getLeft()), height(k2->getRight())); k2->setHeight( h+1 ); // k2 is now k1's right subtree h = Max( height(k1->getLeft()), k2->getHeight()); k1->setHeight( h+1 ); return k1; } k2 k1 Z Y X k1 k2 Z Y X 

height  int height( TreeNode<int>* node ) { if( node != NULL ) return node->getHeight(); return -1; } 

height  int height( TreeNode<int>* node ) { if( node != NULL ) return node->getHeight(); return -1; } 

singleLeftRotation  TreeNode<int>* singleLeftRotation( TreeNode<int>* k1 ) { if( k1 == NULL ) return NULL; // k2 is now the new root TreeNode<int>* k2 = k1->getRight(); k1->setRight( k2->getLeft() ); // Y k2->setLeft( k1 ); // reassign heights. First k1 (demoted) int h = Max(height(k1->getLeft()), height(k1->getRight())); k1->setHeight( h+1 ); // k1 is now k2's left subtree h = Max( height(k2->getRight()), k1->getHeight()); k2->setHeight( h+1 ); return k2; } k1 k2 X Y Z k1 k2 X Y Z

singleLeftRotation TreeNode<int>* singleLeftRotation( TreeNode<int>* k1 ) { if( k1 == NULL ) return NULL; // k2 is now the new root TreeNode<int>* k2 = k1->getRight(); k1->setRight( k2->getLeft() ); // Y k2->setLeft( k1 ); // reassign heights. First k1 (demoted) int h = Max(height(k1->getLeft()), height(k1->getRight())); k1->setHeight( h+1 ); // k1 is now k2's left subtree h = Max( height(k2->getRight()), k1->getHeight()); k2->setHeight( h+1 ); return k2; } k1 k2 X Y Z  k1 k2 X Y Z

singleLeftRotation TreeNode<int>* singleLeftRotation( TreeNode<int>* k1 ) { if( k1 == NULL ) return NULL; // k2 is now the new root TreeNode<int>* k2 = k1->getRight(); k1->setRight( k2->getLeft() ); // Y k2->setLeft( k1 ); // reassign heights. First k1 (demoted) int h = Max(height(k1->getLeft()), height(k1->getRight())); k1->setHeight( h+1 ); // k1 is now k2's left subtree h = Max( height(k2->getRight()), k1->getHeight()); k2->setHeight( h+1 ); return k2; } k1 k2 X Y Z  k1 k2 X Y Z

singleLeftRotation TreeNode<int>* singleLeftRotation( TreeNode<int>* k1 ) { if( k1 == NULL ) return NULL; // k2 is now the new root TreeNode<int>* k2 = k1->getRight(); k1->setRight( k2->getLeft() ); // Y k2->setLeft( k1 ); // reassign heights. First k1 (demoted) int h = Max(height(k1->getLeft()), height(k1->getRight())); k1->setHeight( h+1 ); // k1 is now k2's left subtree h = Max( height(k2->getRight()), k1->getHeight()); k2->setHeight( h+1 ); return k2; } k1 k2 X Y Z  k1 k2 X Y Z

singleLeftRotation TreeNode<int>* singleLeftRotation( TreeNode<int>* k1 ) { if( k1 == NULL ) return NULL; // k2 is now the new root TreeNode<int>* k2 = k1->getRight(); k1->setRight( k2->getLeft() ); // Y k2->setLeft( k1 ); // reassign heights. First k1 (demoted) int h = Max(height(k1->getLeft()), height(k1->getRight())); k1->setHeight( h+1 ); // k1 is now k2's left subtree h = Max( height(k2->getRight()), k1->getHeight()); k2->setHeight( h+1 ); return k2; } k1 k2 X Y Z k1 k2 X Y Z 

singleLeftRotation TreeNode<int>* singleLeftRotation( TreeNode<int>* k1 ) { if( k1 == NULL ) return NULL; // k2 is now the new root TreeNode<int>* k2 = k1->getRight(); k1->setRight( k2->getLeft() ); // Y k2->setLeft( k1 ); // reassign heights. First k1 (demoted) int h = Max(height(k1->getLeft()), height(k1->getRight())); k1->setHeight( h+1 ); // k1 is now k2's left subtree h = Max( height(k2->getRight()), k1->getHeight()); k2->setHeight( h+1 ); return k2; } k1 k2 X Y Z k1 k2 X Y Z 

doubleRightLeftRotation  TreeNode<int>* doubleRightLeftRotation(TreeNode<int>* k1) { if( k1 == NULL ) return NULL; // single right rotate with k3 (k1's right child) k1->setRight( singleRightRotation(k1->getRight())); // now single left rotate with k1 as the root return singleLeftRotation(k1); } k1 k3 D A B C k2

doubleRightLeftRotation TreeNode<int>* doubleRightLeftRotation(TreeNode<int>* k1) { if( k1 == NULL ) return NULL; // single right rotate with k3 (k1's right child) k1->setRight( singleRightRotation(k1->getRight())); // now single left rotate with k1 as the root return singleLeftRotation(k1); }  k1 k3 D A B C k2

doubleRightLeftRotation TreeNode<int>* doubleRightLeftRotation(TreeNode<int>* k1) { if( k1 == NULL ) return NULL; // single right rotate with k3 (k1's right child) k1->setRight( singleRightRotation(k1->getRight())); // now single left rotate with k1 as the root return singleLeftRotation(k1); }  k1 k2 k2 k1 k3 A B C k3 B A D C D

doubleRightLeftRotation  TreeNode<int>* doubleLeftRightRotation(TreeNode<int>* k3) { if( k3 == NULL ) return NULL; // single left rotate with k1 (k3's left child) k3->setLeft( singleLeftRotation(k3->getLeft())); // now single right rotate with k3 as the root return singleRightRotation(k3); } k1 k3 D A B C k2

doubleRightLeftRotation TreeNode<int>* doubleLeftRightRotation(TreeNode<int>* k3) { if( k3 == NULL ) return NULL; // single left rotate with k1 (k3's left child) k3->setLeft( singleLeftRotation(k3->getLeft())); // now single right rotate with k3 as the root return singleRightRotation(k3); }  k1 k3 D A B C k2

doubleRightLeftRotation TreeNode<int>* doubleLeftRightRotation(TreeNode<int>* k3) { if( k3 == NULL ) return NULL; // single left rotate with k1 (k3's left child) k3->setLeft( singleLeftRotation(k3->getLeft())); // now single right rotate with k3 as the root return singleRightRotation(k3); }  k3 k2 k2 k1 k3 D B C k1 C A D A B

Deletion in AVL Tree Delete is the inverse of insert: given a value X and an AVL tree T, delete the node containing X and rebalance the tree, if necessary. Turns out that deletion of a node is considerably more complex than insert

Deletion in AVL Tree Insertion in a height-balanced tree requires at most one single rotation or one double rotation. We can use rotations to restore the balance when we do a deletion. We may have to do a rotation at every level of the tree: log2N rotations in the worst case.

Deletion in AVL Tree Here is a tree that causes this worse case number of rotations when we delete A. At every node in N’s left subtree, the left subtree is one shorter than the right subtree. N F C I A D G K E H J L M

Deletion in AVL Tree Deleting A upsets balance at C. When rotate (D up, C down) to fix this N F C I A D G K E H J L M

Deletion in AVL Tree Deleting A upsets balance at C. When rotate (D up, C down) to fix this N F C I D G K E H J L M

Deletion in AVL Tree The whole of F’s left subtree gets shorter. We fix this by rotation about F-I: F down, I up. N F D I C E G K H J L M

Deletion in AVL Tree The whole of F’s left subtree gets shorter. We fix this by rotation about F-I: F down, I up. N F D I C E G K H J L M

Deletion in AVL Tree This could cause imbalance at N. The rotations propagated to the root. N I F K D G J L C E H M

Deletion in AVL Tree Procedure Delete the node as in binary search tree (BST). The node deleted will be either a leaf or have just one subtree. Since this is an AVL tree, if the deleted node has one subtree, that subtree contains only one node (why?) Traverse up the tree from the deleted node checking the balance of each node.

Deletion in AVL Tree There are 5 cases to consider. Let us go through the cases graphically and determine what action to take. We will not develop the C++ code for deleteNode in AVL tree. This will be left as an exercise.

Deletion in AVL Tree Case 1a: the parent of the deleted node had a balance of 0 and the node was deleted in the parent’s left subtree. Delete on this side Start of lecture 24 Action: change the balance of the parent node and stop. No further effect on balance of any higher node.

Deletion in AVL Tree Here is why; the height of left tree does not change. 4 2 6 1 1 3 5 7 2

Deletion in AVL Tree Here is why; the height of left tree does not change. 4 4 2 6 1 2 6 1 3 5 7 2 3 5 7 remove(1)

Deletion in AVL Tree Case 1b: the parent of the deleted node had a balance of 0 and the node was deleted in the parent’s right subtree. Delete on this side Action: (same as 1a) change the balance of the parent node and stop. No further effect on balance of any higher node.

Deletion in AVL Tree Case 2a: the parent of the deleted node had a balance of 1 and the node was deleted in the parent’s left subtree. Delete on this side End of lecture 23. Action: change the balance of the parent node. May have caused imbalance in higher nodes so continue up the tree.