

Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!


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:
rotRoot
newRoot
rotRoot (^) newRoot
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)
Lab 10 - 1
Before right rotation: After left rotation at pivot:
rotRoot
newRoot
rotRoot (^) newRoot
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.)
Lab 10 - 2