computer notes - singlerightrotation

Post on 14-Dec-2014

369 Views

Category:

Education

0 Downloads

Preview:

Click to see full reader

DESCRIPTION

 

TRANSCRIPT

1

Class No.21

Data Structures

http://ecomputernotes.com

2

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

k1

k2

Z

Y

X

k1

k2

ZYX

singleRightRotation

http://ecomputernotes.com

3

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

k1

k2

Z

Y

X

k1

k2

ZYX

singleRightRotation

http://ecomputernotes.com

4

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

k1

k2

Z

Y

X

k1

k2

ZYX

singleRightRotation

http://ecomputernotes.com

5

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

k1

k2

Z

Y

X

k1

k2

ZYX

singleRightRotation

http://ecomputernotes.com

6

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

k1

k2

Z

Y

X

k1

k2

ZYX

singleRightRotation

http://ecomputernotes.com

7

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

k1

k2

Z

Y

X

k1

k2

ZYX

singleRightRotation

http://ecomputernotes.com

8

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

k1

k2

Z

Y

X

k1

k2

ZYX

singleRightRotation

http://ecomputernotes.com

9

int height( TreeNode<int>* node )

{

if( node != NULL ) return node->getHeight();

return -1;

}

height

http://ecomputernotes.com

10

int height( TreeNode<int>* node )

{

if( node != NULL ) return node->getHeight();

return -1;

}

height

http://ecomputernotes.com

11

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 YZ

singleLeftRotation

http://ecomputernotes.com

12

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 YZ

singleLeftRotation

http://ecomputernotes.com

13

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 YZ

singleLeftRotation

http://ecomputernotes.com

14

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 YZ

singleLeftRotation

http://ecomputernotes.com

15

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 YZ

singleLeftRotation

http://ecomputernotes.com

16

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 YZ

singleLeftRotation

http://ecomputernotes.com

17

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

k1

k3

D

A

B

C

k2

doubleRightLeftRotation

http://ecomputernotes.com

18

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

k1

k3

D

A

B

C

k2

doubleRightLeftRotation

http://ecomputernotes.com

19

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

DAB C

k3

k1

k3

D

A

B

C

k2

doubleRightLeftRotation

http://ecomputernotes.com

20

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 k1

k3

D

A B

C

k2

doubleRightLeftRotation

http://ecomputernotes.com

21

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 k1

k3

D

A B

C

k2

doubleRightLeftRotation

http://ecomputernotes.com

22

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

DAB C

k2

k1

k3

D

A B

C

k2

doubleRightLeftRotation

http://ecomputernotes.com

23

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

24

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

25

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.

A

C

D

N

E J

G

I

F

H

K

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

A

C

D

N

E J

G

I

F

H

K

L

M

http://ecomputernotes.com

27

Deletion in AVL Tree Deleting A upsets balance at C. When rotate (D up, C down)

to fix this

C

D

N

E J

G

I

F

H

K

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.

C

D

N

E

J

G

I

F

H

K

L

M

http://ecomputernotes.com

29

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.

C

D

N

E

J

G

I

F

H

K

L

M

http://ecomputernotes.com

30

Deletion in AVL Tree This could cause imbalance at N. The rotations propagated to the root.

C

D

N

E

JG

I

F

H

K

L

M

http://ecomputernotes.com

31

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

32

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

33

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.

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

Delete on this side

http://ecomputernotes.com

34

Deletion in AVL Tree

Here is why; the height of left tree does not change.

1

2

3

4

5

6

7

0

1

2

35

Deletion in AVL Tree

Here is why; the height of left tree does not change.

1

2

3

4

5

6

7

2

3

4

5

6

7

0

1

2

remove(1)

36

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.

Action: (same as 1a) change the balance of the parent node and stop. No further effect on balance of any higher node.

Delete on this side

37

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.

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

Delete on this side

top related