CSC2103: Data Structures and Algorithms
Week 6: Exploring Trees, Self-balancing Trees
This weekly activity aims to enable the students to explore more about Tree
data structures and their operations.
Binary Search Tree
The attached code (BST_Python) implements the Binary Search Tree in
Python. Using the code, complete the below tasks.
Task 1: Basic Operations and Traversals
1. Create a BST:
o Instantiate a BST object.
o Insert the following values in this order: 50, 30, 70, 20, 40, 60,
80
2. Traversals:
o Print the elements in the BST using in-order, pre-order, and
post-order traversals.
Task 2: Search and Deletion
1. Search:
o Search for a key in the BST (e.g., 40). Does it return the correct
node?
o Search for a key that doesn't exist (e.g., 90). What happens?
2. Deletion:
o Delete a node with no children (e.g., 20).
o Delete a node with one child (e.g., 80).
o Delete a node with two children (e.g., 30).
o After each deletion, print the tree to verify that it maintains the
BST property.
Task 3: Advanced Operations
1. Find Minimum and Maximum:
o Write function to maximum values in the BST.
AVL Tree:
Task 1:
Use the following hyperlink for the AVL tree visualization.
[Link]
Use the above visualization tool to insert the following keys in order:
15, 20, 12, 13, 9,10, 8, 26, 49
Document the process for the following tasks: Analyze each step where a
rotation occurs:
• Identify the unbalanced node and its balance factor before the
rotation.
• State the type of rotation performed.
• Explain why this rotation is necessary.
• Show the resulting tree structure after the rotation.
• Draw or capture the final AVL tree structure.
List the elements of the final tree using pre-order traversal.
Remove the values 9 and 10 from the tree, observe the changes, document
the changes, and finally list the tree in the pre-order visit.
Task 2:
The attached code (AVL_Python) implements the AVL Tree in Python. Using
the code, complete the below tasks.
• Task: Insert a set of keys (e.g., 10, 5, 15, 3, 8, 2, 18) into the AVL tree.
• Questions:
o Print the tree after each insertion using print_tree. Observe how
the rotations maintain balance.
o Can you identify the types of rotations that occurred (left, right,
left-right, right-left)?
Submission:
Please submit your answers in the form of a Word document.