session: "5101-design-analysis-of-algo/2026-10-18 0000 DAA 18 Oct lab"
title: "DAA 18 Oct lab"
date: "2026-10-18 00:00"
duration: "1h 00m"
language: "en"
model: "deepseek-ai/DeepSeek-V4.1-Flash-1017"
parts: 1
transcript_chars: 23802
generated: "2026-11-24T00:00:17+05:30"
DAA 18 Oct lab
Overview
This lab session motivates trees as a way to represent hierarchical, parent–child relationships (file systems, AI decision trees, organization charts) that arrays and linked lists cannot express directly. It moves from general trees to binary trees, then to the binary search tree (BST) and its ordering property, and shows how ordered insertion produces skewed trees that degrade search to linear time. The class introduces the AVL tree (height-balance condition, rotations) and the red-black tree as alternatives that keep height logarithmic, and closes with the three depth-first traversals (in-order, pre-order, post-order) with pseudocode and complexity analysis of BST search, insertion and deletion.
Learning outcomes
- Explain why trees are needed to represent hierarchical relationships, with real-life examples.
- Distinguish a general tree from a binary tree and state the BST ordering property.
- Demonstrate how inserting values in sorted order creates a skewed BST and why this hurts search time.
- State the AVL height-balance condition and explain the purpose of rotations.
- Describe the red-black tree as an alternative to AVL and why it is used.
- Write and trace in-order, pre-order and post-order traversals, including pseudocode.
- Give best/average/worst-case time and space complexity for BST search, insertion and deletion.
Core concepts
Motivation for trees
Definition: A tree represents hierarchical parent–child relationships; an array or linked list can store file names but cannot directly express these relationships.
Why it matters: Real-life uses include the file explorer (a C drive is a parent of "documents" and "pictures"; "documents" is a parent of two files), decision trees in AI for classification and prediction, and organization charts (managers, employees, junior employees).
Connection: This motivation drives the whole class — the need to represent hierarchy leads to trees, then to binary trees and BSTs.
General tree vs. binary tree
Definition: In a general tree a node can have any number of children; in a binary tree each node can have at most two children — a left child and a right child.
Why it matters: General-tree flexibility is useful but does not help with operations such as searching numerical values or alphanumeric characters; the binary tree gives a simple, predictable structure that works well for recursive algorithms.
Connection: The binary tree is the structure on which the BST, AVL and red-black trees in this class are built.
Binary search tree (BST)
Definition: Values smaller than the root node go to its left; values greater than the root node go to its right.
Why it matters: The ordering property is what makes search behave like binary search (go left or right at each node).
Connection: The ordering property is preserved by AVL rotations and is what makes in-order traversal output sorted values.
Skewed tree
Definition: If values arrive in order (e.g., 10, 20, ...), the BST becomes a skewed tree (right-skewed or left-skewed, as introduced in the previous class).
Why it matters: Searching then requires going all the way down, degrading to linear time.
Connection: This problem is the direct motivation for balancing (AVL and red-black trees).
AVL tree
Definition: A self-balanced binary search tree named after the scientists Adelson-Velsky and Landis. It maintains a height-balance condition: for every node, the heights of its left and right subtrees differ by at most one; each node maintains a balance value of one, zero, or minus one.
Why it matters: It prevents the tree from becoming skewed, keeping operations efficient.
Connection: Rebalancing is done by rotations (the three cases are postponed to the next class); the red-black tree is presented as an alternative.
Rotation
Definition: To rebalance a skewed tree, the tree is rotated in the opposite direction of the skew (the lecture mentions the "RR kind of things").
Why it matters: Rotation restores balance while preserving the BST property (smaller node on the left, bigger node on the right).
Connection: The three AVL rotation cases are flagged for the next class.
Red-black tree
Definition: An alternative to the AVL tree that uses a different set of rules involving red and black node colours to prevent the tree from becoming too tall, maintaining a logarithmic height while allowing insertion and deletion with the balancing rules.
Why it matters: With AVL, deletion (or deletion of a node) can take more time; the red-black tree addresses this.
Connection: It is the second balancing strategy presented after AVL; its colour rules will be explained in more detail later.
Tree traversal
Definition: Visiting the nodes of a tree in a particular order. Three orders: in-order, pre-order, post-order. Real-life framing: a tree representing a computer where you list folders in sorted order, or visit the parent before its children, or process the children before their parents.
Why it matters: In-order traversal of a BST gives sorted (ascending) order of the numerical values.
Connection: Traversals are defined on the binary tree structure and rely on the recursive left/right decomposition; "root" in traversal means the current node, not necessarily the root of the entire tree.
- In-order: Visit the left node, then the root (current node), then the right node.
- Pre-order: Visit the root first, then the left, then the right.
- Post-order: Visit all the left nodes, then all the right nodes, then the root.
Formulas and maths
- AVL balance condition: for every node, the height of its left and right subtrees differs by at most one; the maintained value per node is one, zero, or minus one.
- Symbols: "height" of a subtree = its height in edges/levels as used in class; the per-node balance value is one, zero, or minus one.
- Used when: checking whether an AVL tree needs rebalancing.
- BST search time: depends only on the height. Best/average case: \(O(\log n)\) (like binary search, because we go left or right). Worst case (skewed tree): \(O(n)\).
- Symbols: \(n\) = number of nodes in the tree.
- Used when: analysing search cost in a BST.
- BST insertion: worst case \(O(n)\) (skewed); average case \(O(\log n)\).
- Symbols: \(n\) = number of nodes.
- Used when: analysing insertion cost in a BST.
- BST deletion: same as search/insertion — worst case \(O(n)\), average \(O(\log n)\).
- Symbols: \(n\) = number of nodes.
- Used when: analysing deletion cost in a BST.
- Space: auxiliary space is \(O(h)\) (on the height). The code itself takes no extra space beyond the variables (data, left, right) — \(O(1)\); if the recursive stack is considered, it takes space. Worst-case space complexity mentioned as \(N\).
- Symbols: \(h\) = height of the tree; \(N\) = number of nodes (worst case).
- Used when: analysing the space cost of recursive BST operations/traversals.
Worked examples
- File system example: C drive → documents (folder) → files; pictures → .jpg photos — a hierarchical structure.
- BST insertion example: Insert 15; insert 10 and 20; insert 5; insert 12. Searching for 12 requires going down the tree.
- Skewed tree example: Values 10, 20, ... produce a right-skewed tree; searching 40 requires going to the bottom.
- AVL rotation example: For a right-skewed tree, rotate in the opposite direction; after rotation, 20 becomes the root (initially said 10, then corrected to 20). The result still follows the BST property: the smaller node is on the left, the bigger node on the right.
- In-order traversal example: Tree with 15 as root, left subtree containing 10 (with children 5 and 12), right child 20. In-order output: 5, 10, 12, 15, 20 — ascending order.
- Pre-order example: Same tree, with one value changed to 8 (the instructor notes this makes it a valid tree). Pre-order visits 15 first, then the left subtree (10, 8), then 12, then 20, 17, 25. The order written on the board: 10, 12, 20, 17, 25 (as transcribed; the root 15 and 8 are discussed separately in the narration).
- Post-order example: Same tree. Output: 8, 12, 10, 17, 25, 20, then 15.
- Pseudocode discussed (BST insert): base condition — if root is null, return the value/node; if value is less than root.data, root.left = insert(root.left, value); similarly if value is greater than root.data; return root.
- Pseudocode discussed (in-order): if root is none, return; recurse on root.left; print root.data; recurse on root.right. Pre-order: print data first, then left, then right. Post-order: left, right, then data.
Questions asked in class
- Q: Can you please explain the time and space complexity point?
A: There is no time complexity for the traversal itself as such; for search in a BST, time depends only on the height — it behaves like binary search, \(O(\log n)\); in the worst case (skewed tree) it goes to \(O(n)\). Insertion is the same: worst case \(O(n)\), average \(O(\log n)\). Deletion is the same as search. Space: auxiliary space is \(O(h)\); the code takes no extra space beyond the variables (data, left, right) — \(O(1)\); if the recursive stack is considered, it takes space (temporarily created and then freed). Worst-case space complexity mentioned as \(N\).
- Q (about real-time use): The instructor answers that deletion and insertion have real-life use — e.g., deleting a file in the file explorer.
Key takeaways
- Arrays/linked lists cannot express parent–child relationships; trees can (file systems, AI decision trees, organization charts).
- A general tree allows any number of children; a binary tree allows at most two (left and right), which suits recursive algorithms.
- BST property: smaller values go left of the root, greater values go right.
- Inserting values in order (e.g., 10, 20, ...) produces a skewed tree, making search \(O(n)\).
- AVL tree: for every node, left and right subtree heights differ by at most one; balance value is one, zero, or minus one.
- Rotations rebalance a skewed tree in the opposite direction of the skew and preserve the BST property.
- Red-black tree is an alternative to AVL using red/black colour rules, maintaining logarithmic height; AVL deletion can take more time.
- In-order = left, root, right; on a BST it outputs values in ascending sorted order.
- Pre-order = root, left, right; post-order = left, right, root.
- In traversal, "root" means the current node, not necessarily the tree's root.
- BST search/insert/delete: average \(O(\log n)\), worst case \(O(n)\) (skewed).
- Auxiliary space is \(O(h)\); the code itself is \(O(1)\) beyond the data/left/right variables, with the recursive stack taking space (worst case \(N\)).
Exam-style questions
1. Why are trees preferred over arrays/linked lists for file systems? Give an example.
Outline: arrays/lists cannot directly express parent–child relationships; trees can — C drive → documents → files, pictures → .jpg photos.
2. Insert 15, 10, 20, 5, 12 into a BST and give the in-order traversal. What happens if values arrive as 10, 20, ...?
Outline: build the tree per the BST property; in-order gives 5, 10, 12, 15, 20 (ascending); ordered arrival produces a right-skewed tree and search degrades to \(O(n)\).
3. State the AVL height-balance condition and explain how a right-skewed tree is fixed.
Outline: for every node, subtree heights differ by at most one; balance value one/zero/minus one; rotate in the opposite direction of the skew — after rotation 20 becomes the root, and the BST property still holds (smaller left, bigger right).
4. Compare AVL and red-black trees.
Outline: AVL keeps a strict balance; red-black uses red/black colour rules to prevent the tree from becoming too tall, maintaining logarithmic height; AVL deletion can take more time.
5. Give the pseudocode and output of pre-order and post-order traversal for the tree with root 15, left subtree 10 (children 8 and 12), right subtree 20 (children 17 and 25).
Outline: pre-order prints data first, then left, then right; post-order does left, right, then data — post-order output: 8, 12, 10, 17, 25, 20, then 15.
Glossary
- Tree - a structure representing hierarchical parent–child relationships.
- General tree - a tree in which a node can have any number of children.
- Binary tree - a tree in which each node has at most two children (left and right).
- BST - binary search tree; smaller values go left of the root, greater values go right.
- Skewed tree - a BST built from ordered insertions, degenerating into a chain (left- or right-skewed).
- AVL tree - a self-balanced BST where every node's subtree heights differ by at most one; balance value one, zero, or minus one.
- Rotation - a rebalancing operation performed in the opposite direction of the skew.
- Red-black tree - a balanced BST alternative to AVL using red/black node colour rules to keep logarithmic height.
- In-order traversal - left, root, right; gives sorted ascending order on a BST.
- Pre-order traversal - root, left, right.
- Post-order traversal - left, right, root.
- Auxiliary space - extra space used by an algorithm, here \(O(h)\) on the tree height (recursive stack).
Follow-ups
- The three AVL rotation cases will be studied in the next class.
- Red-black tree rules (red and black nodes) will be explained in more detail later.
- Traversals (in-order, pre-order, post-order) to be studied further.
- Other concepts mentioned as out there to study: Triton (a language used to optimize GPU kernels, similar to concrete Python [unclear]), neural tangent kernel (a mathematical framework for certain neural networks), recognition, CNN, IOU, mean, precision, distribution technique, optimization.
- The instructor mentions a third problem/question about post-order and a sample to be made/submitted — details are [unclear].