DS2030 Data Structures and Algorithms For Data Science Lab 5 (Take Home) Due On October 2 11.59pm
DS2030 Data Structures and Algorithms For Data Science Lab 5 (Take Home) Due On October 2 11.59pm
Instructions
• You are to use Python as the programming language. Use may use Visual Studio Code (or any
other editor you are comfortable with) as the IDE.
• You are not allowed to share code with your classmates nor allowed to use code from the internet.
You are encouraged engage in high level discussions with your classmates; however ensure to
include their names in the report/code documentation. If you refer to any source on the Internet,
include the corresponding citation in the report/code documentation. If we find that you have
copied code from your classmate or from the Internet, you will get a straight fail grade in the
course.
• The submission must be a zip file with the following naming convention - rollnumber.zip. The
Python files should be contained in a folder named after the question number.
• Include appropriate comments to document the code. Include a read me file containing the
instructions on for executing the code. The code should run on institute linux machines.
• Upload your submission to moodle by the due date and time. Do not email the submission to
the instructor or the TA.
This lab will improve your understanding of AVL Trees, Single and Double Rotations to maintain the
height balance, Insertion and Deletion operations in an AVL Tree.
1 Introduction
An AVL tree is a self-balancing binary search tree (BST) where the difference between heights of left
and right subtrees for every node is at most one. This height difference is known as the balance factor
and can be -1, 0, or 1 for a balanced AVL tree.
2 Rotations
To maintain the AVL property during insertions and deletions, the tree may need to be rebalanced
using rotations. There are four types of rotations:
1
A B
X B A Z
Y Z X Y
Initial: After LL Rotation:
B A
A Z X B
X Y Y Z
Initial: After RR Rotation:
2. Traverse up from the inserted node to the root, updating balance factors.
2
5 Implementation
Implement all the functions of an AVL tree. The code should output a menu asking the input from the
user. The menu options are 1 (for insert), and 2 (for delete) For example 1 23 would result in adding
the key 23 to the AVL Tree. 2 20 would result in removal (if present) the key 20 from the AVL Tree.
6 References
1. M Goodrich, R Tamassia, and M. Goldwasser, “Data Structures and Algorithms in Python”, 1st
edition, Wiley, 2013.