Docsity

Rotation - Data Structures - Lab, Exercises of Data Structures and Algorithms

Toggle sidebar
bg1
Objectives: You will gain experience with AVL put implementation
To start the lab: Download and unzip the file:
Part A: In lecture 23 we discussed the AVL tree rotateLeft method. For this lab you need to implement the
rotateRight method. The rotateRight method has two parts:
updating the “pointers” to the nodes to do the rotation (look at the rotateLeft method code)
updating the balanceFactors for the rotRoot and newRoot nodes (you need to use math similar to lecture 23)
(See below too)
After left rotation at pivot:
Before right rotation:
B
B
D
D
rotRoot
newRoot
rotRoot newRoot
T
T
T
Th
hh
hh
hT
T
E
E
A
A
A
A C
C E
E
C
C height
height
height
height height
height
Rotate
Right at
Pivot
Consider the balance factor formulas for rotateRight. We know from the above diagram:
oldBal(B) = hA - hC and newBal(B) = hA - (1 + max(hC, hE)) and
oldBal(D) = (1+max(hA, hC)) - hE newBal(D) = hC - hE
To determine newBal(D), consider:
newBal(D) - oldBal(D) =
(See back for newBal(B) calculation)
Data Structures (CS 1520) Lab 10 Name:___________________________
Lab 10 - 1
Docsity.com
pf2

Partial preview of the text

Download Rotation - Data Structures - Lab and more Exercises Data Structures and Algorithms in PDF only on Docsity!

Objectives: You will gain experience with AVL put implementation

To start the lab: Download and unzip the file:

Part A: In lecture 23 we discussed the AVL tree rotateLeft method. For this lab you need to implement the rotateRight method. The rotateRight method has two parts:  updating the “pointers” to the nodes to do the rotation (look at the rotateLeft method code)  updating the balanceFactors for the rotRoot and newRoot nodes (you need to use math similar to lecture 23) (See below too)

Before right rotation: After left rotation at pivot:

B

B

D

D

rotRoot

newRoot

rotRoot (^) newRoot

T

T

T

T h

h (^) h h h

h T T E

E A

A

A

A C C E

E C C height

height height

height (^) height height

Rotate Right at Pivot

Consider the balance factor formulas for rotateRight. We know from the above diagram: oldBal(B) = hA - hC and newBal(B) = hA - (1 + max ( hC, hE )) and oldBal(D) = (1+ max ( hA, hC )) - hE newBal(D) = hC - hE

To determine newBal(D) , consider:

newBal(D) - oldBal(D) =

(See back for newBal(B) calculation)

Data Structures (CS 1520) Lab 10 Name:___________________________

Lab 10 - 1

Docsity.com

Before right rotation: After left rotation at pivot:

B

B

D

D

rotRoot

newRoot

rotRoot (^) newRoot

T

T

T

T h

h (^) h h h

h T T E

E A

A

A

A C C E

E C C height

height height

height (^) height height

Rotate Right at Pivot

Consider the balance factor formulas for rotateRight. We know from the above diagram: oldBal(B) = hA - hC and newBal(B) = hA - (1 + max ( hC, hE )) and oldBal(D) = (1+ max ( hA, hC )) - hE newBal(D) = hC - hE

To determine newBal(B) , consider:

newBal(B) - oldBal(B) =

After completing your implementation of rotateRight, test your code by running the avl_tree.py program. Once you think it is working, run the timeAVLTree.py program. The height of AVL tree after adding in sorted order should be 13, and the height of AVL tree after adding in shuffled order should be about 15.

When it is working, raise your hand and we will check you off.

(If you have extra time, consider working on HW #6 in lab.)

Data Structures (CS 1520) Lab 10 Name:___________________________

Lab 10 - 2

Docsity.com