Data Structure and AlgorithmsUnit 815 min read
Trees & Binary Trees: Types, Operations, Applications & BSTs
Unit 8 of Data Structure and Algorithms covers tree fundamentals (binary trees, BSTs, AVL trees), traversal methods (in-order, pre-order, post-order), insertion/deletion operations, and real-world applications like file systems, expression parsing, and hierarchical data (e.g., eSewa transaction trees). Includes visual
TAKEAWAYS:
- A binary tree is a hierarchical structure where each node has at most two children (left/right), while a binary search tree (BST) enforces left < parent < right for efficient searching.
- Traversals (in-order, pre-order, post-order) visit nodes in different orders, critical for applications like expression evaluation and file system navigation.
- BST operations (insertion, deletion, search) run in O(h) time, where h is tree height—optimal for dynamic datasets like Daraz order queues or Ncell call logs.
- Balanced trees (e.g., AVL) guarantee O(log n) operations by maintaining height constraints, used in databases (e.g., NEPSE stock price indexing) and compilers.
- Complete vs. full trees differ in node placement: complete trees fill levels left-to-right, while full trees require all nodes to have 0 or 2 children.
- Real-world ties: eSewa’s transaction hierarchy (parent-child relationships), YouTube’s video recommendations (tree-based similarity), and Kathmandu’s traffic route optimization (minimum spanning trees).
1. Tree Basics: Definitions and Terminology
A tree is a nonlinear data structure representing hierarchical relationships. Key terms:
- Node: A data element (e.g.,
value,left,rightpointers). - Root: The topmost node (no parent).
- Leaf: A node with no children.
- Parent/Child: Nodes directly connected (parent → child).
- Subtree: A tree derived from a node (e.g., left subtree of root).
- Height: Longest path from root to leaf (e.g., height = 2 for root → child → leaf).
Binary Tree Definition
A tree where each node has at most two children (left and right). Examples:
- Full Binary Tree: Every node has 0 or 2 children.
- Complete Binary Tree: All levels filled except possibly the last, which is filled left-to-right.
- Perfect Binary Tree: All levels completely filled (e.g., height h has nodes).
graph TD
A["Root"] --> B["Left Child"]
A --> C["Right Child"]
B --> D["Left Leaf"]
B --> E["Right Leaf"]
C --> F["Left Leaf"]2. Binary Search Tree (BST): Properties and Operations
A BST is a binary tree with the BST property:
For any node, all left descendants ≤ node < all right descendants.
Key Operations
A. Insertion
- Start at the root.
- Compare the new value with the current node:
- If smaller, move to the left child (recurse).
- If larger, move to the right child (recurse).
- If equal, insert as a duplicate (or ignore, depending on implementation).
- Insert the new node where the path ends.
Example: Insert [47, 50, 25, 27, 17, 61, 5, 26] into an empty BST.
Step-by-Step Trace:
| Step | Value | Path | Resulting BST Structure |
|---|---|---|---|
| 1 | 47 | Root | 47 |
| 2 | 50 | 47 → right | 47 → 50 |
| 3 | 25 | 47 → left | 47 → 25 (left), 50 (right) |
| 4 | 27 | 47 → 25 → right | 25 → 27 |
| 5 | 17 | 47 → 25 → left | 25 → 17 (left) |
| 6 | 61 | 47 → 61 | 61 (right of 47) |
| 7 | 5 | 47 → 25 → 17 → left | 17 → 5 |
| 8 | 26 | 47 → 25 → 27 → left | 27 → 26 |
B. Search
- Start at the root.
- Compare the search key with the current node:
- If equal, return the node.
- If smaller, search the left subtree.
- If larger, search the right subtree.
- If the path ends without finding the key, return
null.
Example: Search for 26 in the BST above.
- Path:
47 → 25 → 27 → 26(found).
C. Deletion
Three cases:
- Node is a leaf: Simply remove it.
- Node has one child: Replace it with its child.
- Node has two children: Replace it with its in-order successor (smallest in the right subtree) or predecessor (largest in the left subtree), then delete the successor/predecessor.
Example: Delete 25 from the BST above.
25has two children (17and27).- In-order successor of
25is26(leftmost in right subtree). - Replace
25with26, then delete26(which has no children). Resulting BST:
graph TD
A["47"] --> B["26"]
A --> C["61"]
B --> D["17"]
B --> E["50"]
D --> F["5"]3. Tree Traversals
Traversals visit nodes in a specific order. Three primary methods:
| Traversal | Order of Visits | Example (BST: 47, 25, 61, 17, 27, 5, 26) | Use Cases |
|---|---|---|---|
| In-order | Left → Root → Right | 5, 17, 25, 26, 27, 47, 61 | Retrieve sorted data (e.g., NEPSE stock prices). |
| Pre-order | Root → Left → Right | 47, 25, 17, 5, 27, 26, 61 | Copy a tree (e.g., save/load file systems). |
| Post-order | Left → Right → Root | 5, 17, 27, 26, 25, 61, 47 | Delete a tree (e.g., clear cache). |
Visual Trace of In-order Traversal: Steps:
- Traverse left subtree of
47→25. - Traverse left subtree of
25→17. - Traverse left subtree of
17→5(leaf, print). - Back to
17(print). - Traverse right subtree of
17→ none. - Back to
25(print). - Traverse right subtree of
25→27. - Traverse left subtree of
27→26(leaf, print). - Back to
26(print). - Back to
27(print). - Back to
47(print). - Traverse right subtree of
47→61(print).
4. Types of Binary Trees
| Type | Definition | Example Structure | Applications |
|---|---|---|---|
| Binary Tree | Nodes have ≤ 2 children (no ordering). | Any tree with left/right children. | Organization charts. |
| BST | Left < Parent < Right. | Sorted tree (see above). | Databases (e.g., NEPSE indices). |
| AVL Tree | Balanced BST (height difference ≤ 1 between subtrees). | Self-balancing during insertions/deletions. | Real-time systems (e.g., Pathao routes). |
| B-Tree | Multi-way tree (used in filesystems). | Nodes with multiple keys/children. | Hard disk indexing (e.g., Windows NTFS). |
5. Applications of Binary Trees
In the Real World
eSewa Transaction Hierarchy:
- Idea: BSTs store transaction records (e.g.,
user_id,amount,timestamp). - How: Each transaction is a node. Searching for a user’s transactions uses BST’s O(log n) search time.
- Example: To find all transactions for
user_12345, traverse the BST starting at the root, comparinguser_idvalues.
- Idea: BSTs store transaction records (e.g.,
YouTube Recommendations:
- Idea: A trie (prefix tree) stores video categories (e.g., "Travel", "Gaming").
- How: BSTs rank videos by views/likes. When you search "Nepal trekking", the system traverses the trie to fetch relevant videos efficiently.
Ncell Call Logs:
- Idea: A binary search tree organizes call records by
timestamp. - How: Inserting a new call (
timestamp,duration,contact) maintains sorted order. To find calls in January 2023, perform a range search (e.g.,2023-01-01to2023-01-31).
- Idea: A binary search tree organizes call records by
File Systems (NTFS/FAT):
- Idea: B-Trees index file locations on disks.
- How: Each node stores file paths and pointers to child nodes. Searching for
C:\Users\file.txttraverses the B-tree in O(log n) time.
Compiler Design (Expression Parsing):
- Idea: Binary trees represent arithmetic expressions (e.g.,
3 + 4 * 2). - How: The tree structure mirrors the order of operations (PEMDAS). Post-order traversal evaluates the expression:
Post-order:+ / \ 3 * / \ 4 23 4 2 * +→3 + (4 * 2) = 11.
- Idea: Binary trees represent arithmetic expressions (e.g.,
6. Time Complexity Analysis
| Operation | Average Case (Balanced BST) | Worst Case (Skewed BST) | Notes |
|---|---|---|---|
| Search | O(log n) | O(n) | Skewed = linear (e.g., sorted input). |
| Insertion | O(log n) | O(n) | Same as search. |
| Deletion | O(log n) | O(n) | Requires search + restructuring. |
| Traversal | O(n) | O(n) | Visits all nodes. |
Example: Inserting n sorted elements into a BST degrades to O(n²) time (degenerates to a linked list).
Solution: Use self-balancing trees (AVL, Red-Black) to guarantee O(log n) operations.
7. Balanced Trees: AVL Trees
An AVL tree maintains balance by ensuring the heights of left and right subtrees differ by at most 1 (balance factor). Rotations restore balance:
- Left Rotation: Right-heavy subtree.
- Right Rotation: Left-heavy subtree.
- Combined Rotations: Left-Right or Right-Left cases.
Example: Insert 10, 20, 30 into an AVL tree.
- Insert
10(root). - Insert
20(right child of10). - Insert
30(right child of20).- Imbalance detected: Right subtree of
10has height 2, left has height 0. - Fix: Left rotation at
10. Result:
- Imbalance detected: Right subtree of
8. Worked Example: BST for Daraz Order Queue
Scenario: Daraz uses a BST to manage orders by order_id (e.g., ORD123, ORD456). Customers frequently check order statuses.
Operations:
- Insertion: New order
ORD789(value = 789).- Path:
ORD123(root) → right (ORD456) → left (ORD789).
- Path:
- Search: Check status of
ORD456.- Path:
ORD123→ right (ORD456, found).
- Path:
- Deletion: Cancel
ORD123(root with two children).- Replace with in-order successor
ORD456, then deleteORD456(leaf).
- Replace with in-order successor
BST Before/After Deletion:
Exam Tip
- Diagrams Are Mandatory: Always draw BSTs after each insertion/deletion step. Examiners deduct marks for missing intermediate states.
- Traversal Order: Memorize the in-order output for BSTs (sorted order). For pre/post-order, use the mnemonic "Root First/Last" (pre: root → left → right; post: left → right → root).
- Balanced vs. Unbalanced: Questions often ask about worst-case scenarios (e.g., "Why is BST search O(n) for sorted input?"). Answer: Degenerates to a linked list.
- Applications: Link BSTs to real-world systems (e.g., "How would NEPSE use a BST to track stock prices?"). Use examples from the "In the real world" section.
- Pseudocode + Trace: For algorithms (e.g., insertion), write pseudocode and provide a step-by-step trace table (like the one for BST insertion above).
- Common Pitfalls:
- Forgetting to update pointers during deletion (e.g., not linking the successor’s right subtree).
- Misidentifying balance factors in AVL trees (always calculate as
left_height - right_height).
Practice Questions (Exam-Style)
Describe the merits and demerits of a BST over a linear array for storing student records (roll_no, name, marks) at TU.
- Merits: O(log n) search/insertion; dynamic size.
- Demerits: Overhead of pointer management; worst-case O(n) for skewed trees.
Show the BST after inserting [15, 25, 10, 20, 30]. Perform in-order traversal.
- BST:
15 / \ 10 25 / \ 20 30 - In-order:
10, 15, 20, 25, 30.
- BST:
Why might a complete binary tree be preferred over a full binary tree for a file system?
- Complete trees minimize height for a given number of nodes, reducing disk I/O latency. Full trees require all nodes to have 0 or 2 children, which is restrictive for variable data sizes.
Write pseudocode for BST deletion and trace deleting node
25from the BST in the worked example.def delete(node, key): if node is None: return node if key < node.value: node.left = delete(node.left, key) elif key > node.value: node.right = delete(node.right, key) else: # Node with one child or no child if node.left is None: return node.right elif node.right is None: return node.left # Node with two children: get inorder successor temp = minValueNode(node.right) node.value = temp.value node.right = delete(node.right, temp.value) return node def minValueNode(node): current = node while current.left is not None: current = current.left return currentTrace for deleting
25:Step Action BST State 1 Find 25(root’s right child).Original BST. 2 25has two children (20,30).Replace 25with successor26.3 Delete 26(leaf).Final BST (see earlier example).
Code Example: BST Implementation in Python
class Node:
def __init__(self, key):
self.left = None
self.right = None
self.value = key
def insert(root, key):
if root is None:
return Node(key)
if key < root.value:
root.left = insert(root.left, key)
else:
root.right = insert(root.right, key)
return root
def inorder(root):
if root:
inorder(root.left)
print(root.value, end=" ")
inorder(root.right)
# Example usage:
root = None
keys = [47, 50, 25, 27, 17, 61, 5, 26]
for key in keys:
root = insert(root, key)
print("In-order traversal:")
inorder(root) # Output: 5 17 25 26 27 47 61
Summary Table: BST Operations
| Operation | Steps | Time Complexity |
|---|---|---|
| Insertion | Traverse to insertion point; attach new node. | O(h) |
| Search | Compare key with nodes until match or null. | O(h) |
| Deletion | Find node; handle 0/1/2 children cases; restructure if needed. | O(h) |
| Traversal | Recursively visit nodes in order (in-order/pre-order/post-order). | O(n) |
Note: h = height of the tree. For balanced trees, h = O(log n).
Based on the TU BIT syllabus for Data Structure and Algorithms (BIT201), unit 8.
Discussion
Loading…