computer notes - Data Structures - 21

Page 1

Class No.21 Data Structures http://ecomputernotes.com 1


singleRightRotation ď‚´

TreeNode<int>* singleRightRotation(TreeNode<int>* k2) { if( k2 == NULL ) return NULL; k2 // k1 (first node in k2's left subtree) // will be the new root k1 TreeNode<int>* k1 = k2->getLeft(); // Y moves from k1's right to k2's left k2->setLeft( k1->getRight() ); Y k1->setRight(k2); X // 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; }

Z

k1 k2 X

http://ecomputernotes.com 2

Y

Z


singleRightRotation ď‚´

TreeNode<int>* singleRightRotation(TreeNode<int>* k2) { if( k2 == NULL ) return NULL; k2 // k1 (first node in k2's left subtree) // will be the new root k1 TreeNode<int>* k1 = k2->getLeft(); // Y moves from k1's right to k2's left k2->setLeft( k1->getRight() ); Y k1->setRight(k2); X // 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; }

Z

k1 k2 X

http://ecomputernotes.com 3

Y

Z


singleRightRotation

ď‚´

TreeNode<int>* singleRightRotation(TreeNode<int>* k2) { if( k2 == NULL ) return NULL; k2 // k1 (first node in k2's left subtree) // will be the new root k1 TreeNode<int>* k1 = k2->getLeft(); // Y moves from k1's right to k2's left k2->setLeft( k1->getRight() ); Y k1->setRight(k2); X // 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; }

Z

k1 k2 X

http://ecomputernotes.com 4

Y

Z


singleRightRotation

ď‚´

TreeNode<int>* singleRightRotation(TreeNode<int>* k2) { if( k2 == NULL ) return NULL; k2 // k1 (first node in k2's left subtree) // will be the new root k1 TreeNode<int>* k1 = k2->getLeft(); // Y moves from k1's right to k2's left k2->setLeft( k1->getRight() ); Y k1->setRight(k2); X // 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; }

Z

k1 k2 X

http://ecomputernotes.com 5

Y

Z


singleRightRotation TreeNode<int>* singleRightRotation(TreeNode<int>* k2) { if( k2 == NULL ) return NULL; k2 // k1 (first node in k2's left subtree) // will be the new root k1 TreeNode<int>* k1 = k2->getLeft(); // Y moves from k1's right to k2's left k2->setLeft( k1->getRight() ); Y k1->setRight(k2); X // 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; }

Z

k1 k2 X

http://ecomputernotes.com 6

Y

Z


singleRightRotation TreeNode<int>* singleRightRotation(TreeNode<int>* k2) { if( k2 == NULL ) return NULL; k2 // k1 (first node in k2's left subtree) // will be the new root k1 TreeNode<int>* k1 = k2->getLeft(); // Y moves from k1's right to k2's left k2->setLeft( k1->getRight() ); Y k1->setRight(k2); X // 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; }

Z

k1 k2 X

http://ecomputernotes.com 7

Y

Z


singleRightRotation TreeNode<int>* singleRightRotation(TreeNode<int>* k2) { if( k2 == NULL ) return NULL; k2 // k1 (first node in k2's left subtree) // will be the new root k1 TreeNode<int>* k1 = k2->getLeft(); // Y moves from k1's right to k2's left k2->setLeft( k1->getRight() ); Y k1->setRight(k2); X // 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; }

Z

k1 k2 X

http://ecomputernotes.com 8

Y

Z


height

ď‚´

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

http://ecomputernotes.com 9


height

ď‚´

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

http://ecomputernotes.com 10


singleLeftRotation ď‚´

TreeNode<int>* singleLeftRotation( TreeNode<int>* k1 ) { k1 if( k1 == NULL ) return NULL; // k2 is now the new root TreeNode<int>* k2 = k1->getRight(); X 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()); k1 k2->setHeight( h+1 ); return k2; }

X

http://ecomputernotes.com 11

Y

k2

Y Z

k2

Z


singleLeftRotation

ď‚´

TreeNode<int>* singleLeftRotation( TreeNode<int>* k1 ) { k1 if( k1 == NULL ) return NULL; // k2 is now the new root TreeNode<int>* k2 = k1->getRight(); X 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()); k1 k2->setHeight( h+1 ); return k2; }

X

http://ecomputernotes.com 12

Y

k2

Y Z

k2

Z


singleLeftRotation

ď‚´

TreeNode<int>* singleLeftRotation( TreeNode<int>* k1 ) { k1 if( k1 == NULL ) return NULL; // k2 is now the new root TreeNode<int>* k2 = k1->getRight(); X 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()); k1 k2->setHeight( h+1 ); return k2; }

X

http://ecomputernotes.com 13

Y

k2

Y Z

k2

Z


singleLeftRotation

ď‚´

TreeNode<int>* singleLeftRotation( TreeNode<int>* k1 ) { k1 if( k1 == NULL ) return NULL; // k2 is now the new root TreeNode<int>* k2 = k1->getRight(); X 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()); k1 k2->setHeight( h+1 ); return k2; }

X

http://ecomputernotes.com 14

Y

k2

Y Z

k2

Z


singleLeftRotation

ď‚´

TreeNode<int>* singleLeftRotation( TreeNode<int>* k1 ) { k1 if( k1 == NULL ) return NULL; // k2 is now the new root TreeNode<int>* k2 = k1->getRight(); X 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()); k1 k2->setHeight( h+1 ); return k2; }

X

http://ecomputernotes.com 15

Y

k2

Y Z

k2

Z


singleLeftRotation TreeNode<int>* singleLeftRotation( TreeNode<int>* k1 ) { k1 if( k1 == NULL ) return NULL; // k2 is now the new root TreeNode<int>* k2 = k1->getRight(); X 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()); k1 k2->setHeight( h+1 ); return k2;

ď‚´ }

X

http://ecomputernotes.com 16

Y

k2

Y Z

k2

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

k1 k3

k2

A

A k2

k3 D

B

B

http://ecomputernotes.com 17 C

C

D


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

k1 k3

k2

A

A k2

k3 D

B

B

http://ecomputernotes.com 18 C

C

D


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); }

k2

k1 k1

k2

k3

A k3 B

A

B

C

http://ecomputernotes.com D 19 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); }

k3

k3

k1

k2 D k2

A

D k1 C

B

C A B http://ecomputernotes.com 20


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

k3

k1

k2 D k2

A

D k1 C

B

C A B http://ecomputernotes.com 21


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 k1 C A

B

A

B

http://ecomputernotes.com 22

C

D


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

http://ecomputernotes.com 23


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. http://ecomputernotes.com 24


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 A

I D

G E

K H

J

L M

http://ecomputernotes.com 25


Deletion in AVL Tree  Deleting A upsets balance at C. When rotate (D up, C down) to fix this N F C A

I D

G E

K H

J

L M

http://ecomputernotes.com 26


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 E

K H

J

L M

http://ecomputernotes.com 27


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 C

I E

G

K H

J

L M

http://ecomputernotes.com 28


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 C

I E

G

K H

J

L M

http://ecomputernotes.com 29


Deletion in AVL Tree  This could cause imbalance at N.  The rotations propagated to the root. N I F D C

K G

E

J H

L M

http://ecomputernotes.com 30


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.

http://ecomputernotes.com 31


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.

http://ecomputernotes.com 32


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

Action: change the balance of the parent node and stop. No further effect on balance of any higher node.

http://ecomputernotes.com 33


Deletion in AVL Tree Here is why; the height of left tree does not change. 0

4 2 1

1

6 3

5

7

2

34


Deletion in AVL Tree Here is why; the height of left tree does not change. 0

4 2 1

1

6 3

5

7

2

4 2

6 3

remove(1)

35

5

7


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. 36


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

Action: change the balance of the parent node. May have caused imbalance in higher nodes so continue up the tree. 37


Turn static files into dynamic content formats.

Create a flipbook
Issuu converts static files into: digital portfolios, online yearbooks, online catalogs, digital photo albums and more. Sign up and create your flipbook.