session: "5101-design-analysis-of-algo/2026-11-23 1241 DAA 25 lab Oct"
title: "DAA 25 lab Oct"
date: "2026-11-23 12:41"
duration: "1h 56m"
language: "en"
model: "deepseek-ai/DeepSeek-V3.2"
parts: 2
transcript_chars: 39403
generated: "2026-11-23T13:51:20+05:30"
DAA 25 lab Oct
Overview
This class introduced AVL trees, a type of self-balancing binary search tree. The lecture covered the motivation for self-balancing trees to maintain O(log n) search time, the definition of the balance factor, and the four rotation cases (LL, RR, LR, RL) used to restore balance. Detailed step-by-step examples of insertions and rotations were worked through. This topic fits into the course's broader study of efficient data structures for dynamic sets.
Learning outcomes
- Explain why self-balancing BSTs are necessary and how AVL trees achieve it.
- Calculate the height and balance factor for any node in a tree using both edge-based and node-based definitions.
- Identify an unbalanced node and determine which of the four rotation cases (LL, RR, LR, RL) applies.
- Perform the necessary pointer swaps for left and right rotations to rebalance a tree.
- Trace the complete process of inserting a new key into an AVL tree, including height updates and rotations.
Core concepts
AVL Tree
An AVL tree is a self-balancing binary search tree. It maintains the BST property (left child < parent < right child) and an additional invariant: for every node, the heights of its left and right subtrees differ by at most 1. This prevents the tree from becoming skewed, ensuring O(log n) time for search, insertion, and deletion.
Balance Factor
The balance factor of a node is defined as the height of its left subtree minus the height of its right subtree. In an AVL tree, the balance factor of every node must be -1, 0, or +1. This concept is central to detecting imbalance and determining the required rotation.
Rotations
Rotations are local operations that rearrange nodes and pointers to restore the AVL balance property while preserving the BST ordering. The four cases (LL, RR, LR, RL) dictate which rotations (single or double) to apply based on the balance factors of the unbalanced node and its child.
Height Calculation
The height of a node is the length of the longest path from that node to a leaf. The class discussed two common definitions: edge-based (counting edges, leaf height = 0) and node-based (counting nodes, leaf height = 1). The recursive calculation of height is fundamental for computing balance factors.
Formulas and maths
- Balance Factor:
BF(node) = height(node.left) - height(node.right) BF: Balance Factor of a node.height(): Function returning the height of a subtree.- Used: To check if a node (and thus the tree) is balanced. Must be in {-1, 0, +1}.
- Height (Edge-based, Recursive):
height(node) = 1 + max(height(node.left), height(node.right))
node.left, node.right: Left and right child pointers.max(): Returns the larger of two numbers.- Used: To recursively calculate the height of a node for balance factor computation. Base case:
height(None) = -1.
- Height (Node-based, Recursive):
height(node) = 1 + max(height(node.left), height(node.right))
node.left, node.right: Left and right child pointers.max(): Returns the larger of two numbers.- Used: Alternative definition. Base case:
height(None) = 0.
Worked examples
Height Calculation Examples
- Single node (value 10):
- Edge-based:
height(10) = 1 + max(-1, -1) = 1 + (-1) = 0. - Node-based:
height(10) = 1 + max(0, 0) = 1.
Skewed BST Creation
- Inserting 10, 20, 30, 40 in ascending order creates a right-skewed tree (a linked list), degrading search time to O(n).
Rotation Cases
1. LL Case (Right Rotation):
- Insert: 30, 20, 10 (descending order).
- Before: Unbalanced node 30 with BF = +2 (left-left skew).
- After right rotation: Balanced tree with root 20, left child 10, right child 30.
2. RR Case (Left Rotation):
- Insert: 10, 20, 30 (ascending order).
- Before: Unbalanced node 10 with BF = -2 (right-right skew).
- After left rotation: Balanced tree with root 20, left child 10, right child 30.
3. LR Case (Left-Right Rotation):
- Insert: 30, 10, 20.
- Steps: 1) Left rotation on node 10. 2) Right rotation on node 30.
- Result: Balanced tree with root 20, left child 10, right child 30.
4. RL Case (Right-Left Rotation):
- Insert: 10, 30, 20.
- Steps: 1) Right rotation on node 30. 2) Left rotation on node 10.
- Result: Balanced tree with root 20, left child 10, right child 30.
Complex Insertion Example
- Initial balanced tree: Nodes 30, 20, 40, 10, 25.
- Insert 22: Causes imbalance at node 30 (RL case).
- Steps: Right rotation at 25? [unclear per transcript], then left rotation at 30. Detailed pointer swaps shown involving nodes 20, 25, 22.
Rotation Step Walkthrough
- For a right rotation at node
x=20, with y=30: y.left (30's left) is 25.- Swapping pointers:
x.right = y and y.left = subtree. - After rotation, structure changes: e.g., 30's left becomes none, right is 40.
Recursive Height Update
- Leaf nodes (e.g., 10, 25, 40) have height 0 (edge-based).
- Node 20 with children 10 (height 0) and 25 (height 0):
height(20) = 1 + max(0, 0) = 1.
Questions asked in class
- Q: "If any node's balance factor goes beyond {-1,0,+1}, that node is qualified for rotation, right?"
- A: "Correct."
- Q: "How do we determine clockwise or anti-clockwise rotation? For larger trees?"
- A: "Observe the two nodes below the unbalanced node. Identify the pattern: LL (left-left skew), RR (right-right skew), LR (left-right), or RL (right-left). Then apply the corresponding rotation."
- Q: Clarification on height calculation formulas: node-based vs. edge-based.
- A: Confirmed edge-based uses
1 + max(left, right) with base case -1; node-based uses max(left, right) with base case 0. - Q: "After rotation, we must update heights, right?"
- A: "Yes, update heights after rotation."
- Q (Anil/Jai): About height calculation at node 20, why it is
max(0,0) even though it has children 10 and 25. - A: Height is calculated recursively. For node 20, the height of its children (10 and 25) is 0 (since they are leaves), so
max(0,0) gives 0. The process is recursive. - Q: Why at node 20 the height is not same as at node 40 where there are no children?
- A: Both are leaves in the edge-based approach? [Answer references "edge space approach" and recursive calculation].
- Q: About rotation pattern vs. balance factor.
- A: Emphasized to use balance factor to decide rotation, not just pattern recognition: "based on balance vector not based on pattern".
- Q (Anujay): How to visualize/output the tree after a left rotation in a readable form (like an array or level order).
- A: Suggests using level order traversal to read the tree after rotation.
- Q: About the overall process when inserting a new element (e.g., 22).
- A: Process involves: insert, calculate height, calculate balance factor, check if balanced, if not, find the case (rotation type), perform rotation, recalculate height and balance factor, and repeat iteratively until balanced.
Key takeaways
- AVL trees are self-balancing BSTs that guarantee O(log n) operations by ensuring every node's balance factor is in {-1, 0, +1}.
- The balance factor is
height(left subtree) - height(right subtree). - A node with a balance factor of -2 or +2 is unbalanced and requires rotation.
- The four rotation cases are LL (right rotation), RR (left rotation), LR (left then right), and RL (right then left).
- Heights must be updated recursively after every insertion and rotation.
- The insertion algorithm is iterative: insert, update heights up the tree, check balance factors, and rotate if necessary.
- Use the balance factor, not just visual pattern, to determine the correct rotation case.
- Level-order traversal is a useful way to visualize the tree structure after rotations.
Exam-style questions
1. Question: Insert the keys [50, 30, 70, 20, 40, 60, 80, 10] into an initially empty AVL tree. Show the tree after each insertion that causes a rotation, and name the rotation performed.
- Outline: Perform insertions sequentially. After inserting 10, an LL imbalance occurs at node 20 (BF=+2), requiring a right rotation on the subtree rooted at 20. The final tree should be balanced.
2. Question: For the following AVL tree, calculate the balance factor for every node using the edge-based height definition (leaf height = 0). Identify any unbalanced nodes.
```
44
/ \
17 78
/ \ /
10 32 50
```
- Outline: Calculate height recursively: leaves (10,32,50) = 0. Node 17: height = 1+max(0,0)=1, BF=0-0=0. Node 78: height=1+max(0,-1)=1, BF=-1-0=-1. Node 44: height=1+max(1,1)=2, BF=1-1=0. All nodes are balanced.
3. Question: Describe the step-by-step process to rebalance an AVL tree after inserting a new node, starting from the point of insertion back to the root.
- Outline: 1) Insert node as in a standard BST. 2) Recursively update the height of the current node. 3) Calculate the balance factor. 4) If BF is +2 or -2, identify the imbalance case (LL, RR, LR, RL) and perform the corresponding rotation(s). 5) Continue this process recursively up to the root.
Glossary
- AVL tree - A self-balancing binary search tree where the heights of the two child subtrees of any node differ by at most one.
- Balance factor - For a node, the height of its left subtree minus the height of its right subtree.
- LL case - An imbalance where the left child of a left-heavy node is also left-heavy; fixed with a single right rotation.
- LR case - An imbalance where the left child of a left-heavy node is right-heavy; fixed with a left rotation on the left child followed by a right rotation on the node.
- RL case - An imbalance where the right child of a right-heavy node is left-heavy; fixed with a right rotation on the right child followed by a left rotation on the node.
- RR case - An imbalance where the right child of a right-heavy node is also right-heavy; fixed with a single left rotation.
- Rotation - A local operation that swaps node pointers to restore tree balance while preserving BST order.
- Skewed tree - A degenerate tree where nodes are inserted in strictly ascending or descending order, resembling a linked list.
Follow-ups
- The inventors of AVL trees are Georgy Adelson-Velsky and Evgenii Landis.
- Implementation details (Node class with key, left, right, height; insert function; rotation functions) were described but will be revisited.
- Rotation code snippets were described verbally but not fully written out; to be covered in a subsequent part.
- The iterative balancing loop process ("check if balance then find the case and then again check...") will be practiced.
- Output/visualization of the tree structure after rotation in a standard format (e.g., level order) will be explored further.