Lab12 Tasks
Lab12 Tasks
BS (CS) _Fall_2024
Lab_12 Tasks
Learning Objectives:
1. AVL Tree
Lab Tasks
Submission Instructions
1. Create a separate .h and .cpp file for each class.
2. Now create a new folder with name ROLLNO_SEC_LAB12 e.g.
i23XXXX_A_LAB07(Also name your cpp file like this.)
3. Move all of your files to this newly created directory and compress it into a
.zip file. Return the test file as well.
4. Now you have to submit this zipped file on Google Classroom.
5. If you don’t follow the above-mentioned submission instruction, you will be
marked zero.
6. Plagiarism or AI generated code in the Lab Task will result in zero marks in
the whole category.
Question 1
Suppose you have an AVL tree containing distinct integer keys. Design an algorithm to split this
AVL tree into two separate AVL trees, TreeA and TreeB, such that TreeA contains keys less than
or equal to a given threshold value K, and TreeB contains keys greater than K. The algorithm
should ensure that both resulting trees maintain the AVL property.
Question no. 2
Stock Price Tracking System with AVL Tree Fundamentals
Scenario: Develop a system to monitor and store stock prices throughout the day. The system
should handle frequent price updates efficiently, allowing for quick retrieval of key statistics.
Tasks:
1. Node Structure: Implement a structure for an AVL tree node that includes fields for the stock
price, pointers to the left and right children, and the node's height.
2. Insertion: Write a function to insert a new stock price. Ensure the AVL tree remains balanced
by performing rotations as necessary after each insertion.
3. Deletion: Implement a function to delete a stock price if necessary, rebalancing the AVL tree as
needed.
4. Height Calculation: Write a function to calculate the height of a node in the AVL tree.
5. Balance Factor: Implement a function to calculate the balance factor of a node (i.e., the height
difference between its left and right subtrees).
6. Check AVL Property: Write a function to verify that the AVL tree remains balanced (ensuring
the height difference of each node's subtrees is at most 1).
7. Tree Traversal: Implement recursive functions for in-order, pre-order, and post-order
traversals to display stock prices.
8. Minimum and Maximum Price: Write functions to find the minimum and maximum stock
prices in a given time range.
9. Median Price: Design a function to retrieve the median stock price at any given point in time.
10. Rotations: Implement left and right rotations to balance the tree as part of insertion and deletion
operations.
11. Rebalancing: Write a function to perform the necessary rotations to rebalance any node after
insertion or deletion.
Follow-Up Questions:
o How would you handle frequent price updates efficiently in the AVL tree?
o How can you optimize the process of finding the median price in the AVL tree?
o How would you handle duplicate values in stock prices?