Ontological Layer: Hierarchical Data Organization
Trees & Heaps
Branching Logic and Priority Structures
The Escape from Linearity
Linear structures restrict elements to a single successor. In Realm 05, data gains the power of hierarchy. A single node can branch into multiple descendants, transforming an O(N) search into an O(log N) traversal. By enforcing strict mathematical relationships between parents and children, we create Binary Search Trees for rapid retrieval, Min/Max Heaps for absolute priority, and AVL Trees to combat the inevitable degradation of imbalance.
The 12 Structural Nodes
Ontological Mapping: From Roots to Multiway Branches
Tree Terminology
- Root, Parent, Child, Sibling
- Internal nodes vs Leaves
- Edges, Depth, and Height
- Binary restriction (Max 2 children)
Tree Traversals
- Preorder (Root, L, R)
- Inorder (L, Root, R)
- Postorder (L, R, Root)
- Level-order (Breadth-First)
Binary Search Trees
- Left < Root < Right property
- O(h) Search, Insert, Delete
- Inorder traversal yields sorted data
- Susceptible to skew (O(N) worst case)
Max-Heap Concept
- Parent ≥ Children property
- Maximum element always at Root
- Complete binary tree structure
- Basis for descending Priority Queues
Min-Heap Concept
- Parent ≤ Children property
- Minimum element always at Root
- O(1) access to smallest element
- Basis for ascending Priority Queues
Array Representation
- Level-order mapping to array
- Left Child = 2i + 1
- Right Child = 2i + 2
- Parent = floor((i-1)/2)
Heapify Operations
- Restoring heap property after Insert/Delete
- Sift-Up (Bubble up) for Insertion
- Sift-Down (Sink) for Deletion
- O(log N) operation time
The Balancing Problem
- Sequential inserts cause skew
- Tree degrades into a Linked List
- Search degrades from O(log N) to O(N)
- Motivation for self-balancing structures
AVL & Balance Factor
- Strictly balanced BST
- BF = Height(L) - Height(R)
- Valid BF range: {-1, 0, 1}
- Violation triggers structural rotation
AVL Rotations
- LL Case → Single Right Rotation
- RR Case → Single Left Rotation
- LR / RL Cases → Double Rotations
- O(1) time to restore global balance
B-Trees (Multiway)
- Multiple keys per node
- Multiple children per node
- Massively reduces tree height
- Optimized for Disk/Database access
12 — The Hierarchical Laboratory
Hierarchies introduce a trade-off: organization requires maintenance. Inserting into a BST is fast, but only if you mathematically enforce balance through rotations. A Heap guarantees the highest priority element is always accessible, but requires sift-operations to maintain its structure. Understanding these maintenance costs is the key to algorithmic efficiency.