Hierarchical Data Structures
CS205 Data Structures
Use arrow keys or buttons to navigate
A tree is a hierarchical data structure consisting of nodes connected by edges.
Like a family tree: one ancestor at the top, descendants branching down. Or a file system: root folder containing subfolders and files.
A tree T is either:
Each subtree is itself a tree! This recursive structure is why many tree algorithms are naturally recursive.
A tree with n nodes always has exactly n - 1 edges. There is exactly one path between any two nodes (no cycles!).
Every node is the root of its own subtree. "Zoom in" on any node and you see a smaller tree.
| Method | Description |
|---|---|
root() | Return the root node |
parent(v) | Return parent of v |
children(v) | Return children of v |
size() | Number of nodes |
| Method | Description |
|---|---|
isInternal(v) | Does v have children? |
isExternal(v) | Is v a leaf? |
isRoot(v) | Is v the root? |
depth() goes up toward root. height() goes down toward leaves. Both are recursive!
Depth(v) = number of edges from root to v.
Root has depth 0. Goes top-down.
Height(v) = number of edges on the longest path from v down to a leaf.
Leaves have height 0. Goes bottom-up.
Height(T) = height of the root = max depth of any leaf.
Here: height(A) = 3, because the longest path is A → B → D → F.
A binary tree is a tree where each node has at most 2 children: a left child and a right child.
| Property | Value |
|---|---|
| Max nodes at depth d | 2d |
| Max leaves (height h) | 2h |
| Max total nodes | 2h+1 - 1 |
| Min height (n nodes) | ⌊log2 n⌋ |
Binary trees are the most common tree structure in CS. They enable efficient searching, sorting, and expression evaluation.
In a binary tree, a node with only one child — it matters whether it's the left or right child. They are structurally different.
| Type | Rule | Relationship |
|---|---|---|
| Full | Every node: 0 or 2 children | Not necessarily complete |
| Complete | All levels full; last level left-filled | Used in heaps! |
| Perfect | All leaves at same depth, all internals have 2 children | Both full AND complete |
Traversal = visiting every node exactly once in a specific order.
| Traversal | Order | Mnemonic |
|---|---|---|
| Preorder | Node, Left, Right | NLR |
| Inorder | Left, Node, Right | LNR |
| Postorder | Left, Right, Node | LRN |
| Level-order | Top to bottom, L to R | BFS |
Reference tree for next slides:
The mnemonic tells you when you visit the node: before children (pre), between children (in), or after children (post).
Visit node FIRST, then recurse left, then recurse right.
Read chapter title first (node), then read sections in order (left to right children).
Recurse left, visit node IN THE MIDDLE, then recurse right.
Inorder traversal of a Binary Search Tree visits nodes in ascending sorted order. This is the most important traversal for BSTs.
Recurse left, recurse right, visit node LAST.
The root is always visited last. You must finish all descendants before processing a node. Think: "clean up children before yourself."
Visit nodes level by level, left to right. Uses a queue (not recursion).
This is the only standard traversal that uses a queue instead of recursion (or a stack).
For every node v in the tree:
This must hold for every node, not just the root!
Like binary search on a sorted array, but stored as a tree. At each node you decide: go left (smaller) or go right (larger).
| Operation | BST (balanced) | Array | Linked List |
|---|---|---|---|
| Search | O(log n) | O(log n)* | O(n) |
| Insert | O(log n) | O(n) | O(1)** |
| Delete | O(log n) | O(n) | O(n) |
* sorted array with binary search
** insert at head, but finding position is O(n)
Compare target with current node. Go left if smaller, right if larger.
Like binary search on an array! Each step goes down one level, so the number of comparisons equals the depth of the target node.
Search for the key. When you reach null, that is where the new node goes.
Insert never modifies existing nodes. It simply creates a new leaf at the correct position.
Inserting 1, 2, 3, 4, 5 in order creates a skewed tree (linked list!). Different insertion orders create different tree shapes.
Like filing a paper: walk through the file cabinet making left/right decisions until you find the empty slot.
Case 1 (leaf): trivial. Case 2 (one child): just bypass. Case 3 (two children): most complex — next slide!
Delete 8 from the BST. Node 8 has two children, so we cannot simply remove it.
The inorder successor is the smallest value larger than the deleted node. It always has at most one child (right only), so deleting it is Case 1 or Case 2. You could also use the inorder predecessor (largest in left subtree).
Height ~ log2(n). Each operation touches at most h + 1 nodes.
Sorted input creates a degenerate tree (linked list). Height = n - 1. All operations become O(n)!
How do we guarantee O(log n) performance? Self-balancing trees!
AVL trees are more strictly balanced (faster lookup). Red-Black trees do fewer rotations (faster insert/delete). Java's TreeMap uses Red-Black.
These will be covered in detail in a future lecture. For now, know they exist and why they're needed: to prevent the O(n) worst case.
Arithmetic expressions can be represented as binary trees. Operators are internal nodes; operands are leaves.
Process children (operands) before parent (operator). Naturally computes the result bottom-up.
| Traversal | Output | Notation |
|---|---|---|
| Preorder | * + 3 4 2 | Prefix (Polish) |
| Inorder | 3 + 4 * 2 | Infix (needs parens!) |
| Postorder | 3 4 + 2 * | Postfix (RPN) |
3 + 4 * 2 is ambiguous without parentheses. Prefix and postfix are unambiguous — no parens needed.
| Name | Order | Use For |
|---|---|---|
| Preorder | NLR | Copy/serialize |
| Inorder | LNR | Sorted output (BST) |
| Postorder | LRN | Delete / eval expr |
| Level-order | BFS | Level-by-level |
| BST Type | Height | Ops |
|---|---|---|
| Balanced | O(log n) | O(log n) |
| Skewed | O(n) | O(n) |
Trees let us organize data hierarchically. BSTs give us O(log n) search, insert, and delete — but only when balanced. Self-balancing trees (AVL, Red-Black) guarantee this.