🌍 Data Structures & Algorithms · subject
Data Structures & Algorithms Trees & Hierarchical Structures Syllabus
The Trees & Hierarchical Structures syllabus for Data Structures & Algorithms in full: 5 chapters and 16 topics, every one listed below, plus 50 Trees & Hierarchical Structures flashcards with answers.
Trees & Hierarchical Structures syllabus: all 5 chapters
Expand any chapter to see its topics and sub-topics.
-
Tree Fundamentals
3 topics- Tree Terminology
- Tree Traversals
- Recursive Tree Problems
-
Binary Search Trees
3 topics- BST Property and Operations
- Insertion, Deletion, Search
- Validation and Range Queries
-
Balanced Trees
4 topics- AVL Trees
- Red-Black Trees
- Rotations
- B-Trees and B+ Trees
-
Heaps & Priority Queues
3 topics- Binary Heaps
- Heap Operations
- Top-K and K-Way Merge Problems
-
Tries & Specialized Trees
3 topics- Trie (Prefix Tree)
- Segment Trees
- Fenwick Tree (Binary Indexed Tree)
Data Structures & Algorithms Trees & Hierarchical Structures flashcards — 50 questions with answers
In tree terminology, what is the difference between the *depth* and the *height* of a node?
The **depth** of a node is the number of edges on the path from the root to that node (root has depth $0$). The **height** of a node is the number of edges on the longest path from that node down to a leaf (leaves have height $0$). The height of the tree equals the height of its root.
Define the following tree terms: root, leaf, internal node, sibling, and ancestor.
**Root**: the unique node with no parent. **Leaf** (external node): a node with no children. **Internal node**: a node with at least one child. **Siblings**: nodes sharing the same parent. **Ancestor** of a node $v$: any node on the path from the root to $v$ (excluding $v$ itself for proper ancestors).
For a binary tree of height $h$, what are the minimum and maximum possible numbers of nodes?
Minimum: $h+1$ nodes (a degenerate/skewed tree, one node per level). Maximum: $2^{h+1}-1$ nodes (a perfect binary tree). Thus a tree with $n$ nodes has height between $\lfloor \log_2 n \rfloor$ and $n-1$.
What is a *complete* binary tree, and how does it differ from a *full* and a *perfect* binary tree?
**Complete**: every level is fully filled except possibly the last, which is filled left to right. **Full** (proper/strict): every node has either $0$ or $2$ children. **Perfect**: all internal nodes have $2$ children and all leaves are at the same depth (a perfect tree of height $h$ has exactly $2^{h+1}-1$ nodes).
List the three depth-first binary tree traversals and the visit order of each relative to a node's subtrees.
**Preorder**: Node, Left, Right. **Inorder**: Left, Node, Right. **Postorder**: Left, Right, Node. (The prefix names where the node itself is visited relative to recursing on its two subtrees.)
Which traversal of a Binary Search Tree yields the keys in sorted ascending order, and why?
**Inorder traversal** (Left, Node, Right). Because in a BST every key in the left subtree is smaller and every key in the right subtree is larger, visiting left-node-right recursively emits keys in nondecreasing order.
What traversal produces a level-by-level ordering of a tree, and what data structure implements it?
**Breadth-first / level-order traversal**, implemented with a **FIFO queue**: enqueue the root, then repeatedly dequeue a node, visit it, and enqueue its children. Time $O(n)$, space $O(w)$ where $w$ is the maximum width.
Given preorder and inorder traversal sequences of a binary tree with distinct keys, can the tree be uniquely reconstructed? What about preorder + postorder?
**Preorder + inorder** (or postorder + inorder) uniquely determines the tree. **Preorder + postorder** does NOT in general — it fails to distinguish left-only from right-only children, so it uniquely determines the tree only for full binary trees.
Write a recursive formula for the height of a binary tree node in terms of its children's heights.
$$\text{height}(v) = \begin{cases} -1 & v = \text{null} \\ 1 + \max\big(\text{height}(v.\text{left}),\ \text{height}(v.\text{right})\big) & \text{otherwise} \end{cases}$$ (Using the convention height of an empty subtree $= -1$ so a single leaf has height $0$.)
Give the recursive definition used to count the total number of nodes in a binary tree.
$$\text{count}(v) = \begin{cases} 0 & v = \text{null} \\ 1 + \text{count}(v.\text{left}) + \text{count}(v.\text{right}) & \text{otherwise} \end{cases}$$ Runs in $O(n)$ time and $O(h)$ recursion-stack space.
What is the recursive strategy for finding the Lowest Common Ancestor (LCA) of two nodes in a plain binary tree?
Recurse into both subtrees searching for the two target nodes. If the current node is one of the targets, return it. If the left and right recursive calls both return non-null, the current node is the LCA. Otherwise return whichever side is non-null. Runs in $O(n)$.
State the Binary Search Tree (BST) property precisely.
For every node $x$: all keys in $x$'s left subtree are $< x.\text{key}$, and all keys in $x$'s right subtree are $> x.\text{key}$ (assuming distinct keys). This must hold recursively for every node, not just direct children.
What is the time complexity of search, insert, and delete in a BST, in both the balanced and worst cases?
All three are $O(h)$ where $h$ is the tree height. Balanced: $h = O(\log n)$, giving $O(\log n)$. Worst case (degenerate/skewed tree): $h = O(n)$, giving $O(n)$.
Describe the BST search algorithm for a key $k$.
Start at the root. At each node, if $k$ equals the node's key, return it. If $k <$ node's key, go left; if $k >$ node's key, go right. Stop when you find $k$ or hit null (not present). Time $O(h)$.
Describe how insertion works in a BST and where the new node ends up.
Perform a search for the key; when you reach a null child pointer, that is the insertion point. Insert the new key there as a new **leaf**. BST insertion never restructures existing nodes; it only adds a leaf. Time $O(h)$.
Explain the three cases of BST node deletion.
**Case 1 (leaf)**: simply remove it. **Case 2 (one child)**: replace the node with its single child. **Case 3 (two children)**: replace the node's key with its **inorder successor** (smallest key in the right subtree) or inorder predecessor, then delete that successor node (which has at most one child).
How do you find the inorder successor of a node in a BST?
If the node has a right subtree, the successor is the **leftmost node of that right subtree** (repeatedly go left). If it has no right subtree, the successor is the **lowest ancestor whose left subtree contains the node** (walk up until you move up from a left child).
How can you validate that a binary tree is a valid BST in $O(n)$ time?
Recurse carrying a valid $(\text{low}, \text{high})$ range for each node: check $\text{low} < \text{key} < \text{high}$, then recurse left with range $(\text{low}, \text{key})$ and right with $(\text{key}, \text{high})$. Starting range is $(-\infty, +\infty)$. Alternatively, verify an inorder traversal is strictly increasing.
Why is checking only that each node is greater than its left child and less than its right child insufficient to validate a BST?
The BST property is global, not just parent-child local. A node deep in the left subtree could still exceed an ancestor. Example: root $10$, right child $15$, but $15$'s left child is $6$ — locally valid at each pair, yet $6 < 10$ violates the BST property against the ancestor $10$.
How do you perform a range query (return all keys in $[lo, hi]$) on a BST efficiently?
Recurse from the root: if the current key $> lo$, recurse left; if $lo \leq \text{key} \leq hi$, output it; if the current key $< hi$, recurse right. This prunes subtrees outside the range. Cost is $O(h + k)$ where $k$ is the number of reported keys.
Show the other 30 Trees & Hierarchical Structures flashcards
What defines an AVL tree, and what is its balance factor constraint?
An AVL tree is a self-balancing BST where, for **every** node, the **balance factor** $= \text{height(left)} - \text{height(right)}$ is in $\{-1, 0, +1\}$. If an insertion or deletion violates this, rotations restore balance.
What is the maximum height of an AVL tree with $n$ nodes?
$h \leq 1.44 \log_2(n+2) - 0.328$, i.e. $h = O(\log n)$. The worst case relates to Fibonacci: the minimum number of nodes in an AVL tree of height $h$ is $N(h) = N(h-1) + N(h-2) + 1$, which grows like $\phi^{h}$ with $\phi = \frac{1+\sqrt{5}}{2}$.
Name the four rotation cases used to rebalance an AVL tree after insertion.
**Left-Left (LL)**: single right rotation. **Right-Right (RR)**: single left rotation. **Left-Right (LR)**: left rotation on child then right rotation on node. **Right-Left (RL)**: right rotation on child then left rotation on node. Named by the path from the unbalanced node to the newly inserted node.
What are the defining properties (invariants) of a Red-Black tree?
1) Every node is red or black. 2) The root is black. 3) All leaves (NIL sentinels) are black. 4) A red node's children are both black (no two consecutive reds). 5) Every root-to-leaf path passes through the same number of black nodes (**black-height** is uniform).
What height guarantee do Red-Black trees provide, and how does it follow from the invariants?
Height $h \leq 2\log_2(n+1)$, i.e. $O(\log n)$. Because no path has two consecutive red nodes, the longest root-to-leaf path is at most twice the shortest (all-black) path, and uniform black-height bounds both.
Compare AVL trees and Red-Black trees in terms of balance strictness and use cases.
**AVL** trees are more strictly balanced (height factor $\leq 1.44\log n$), giving faster lookups but more rotations on updates — better for read-heavy workloads. **Red-Black** trees are more loosely balanced ($\leq 2\log n$) with fewer rotations per update ($O(1)$ amortized), better for insert/delete-heavy workloads. Red-Black trees back many standard-library maps/sets.
Describe a left rotation on node $x$ (with right child $y$) and what it preserves.
$y$ becomes the new subtree root; $x$ becomes $y$'s left child; $y$'s original left subtree becomes $x$'s right subtree. It preserves the BST inorder ordering and runs in $O(1)$ time by relinking a constant number of pointers. A right rotation is its mirror inverse.
Do rotations change the inorder traversal of a BST? Why does this matter?
No — rotations **preserve the inorder ordering** of keys; they only change the tree's shape/height. This is precisely why balanced BSTs (AVL, Red-Black) can rebalance via rotations without violating the BST property.
What is a B-tree of minimum degree (order) $t$, and what are its key properties?
A B-tree of minimum degree $t \geq 2$ is a balanced multiway search tree where: every node has at most $2t-1$ keys and at most $2t$ children; every non-root node has at least $t-1$ keys; keys within a node are sorted; and **all leaves are at the same depth**. Height is $O(\log_t n)$.
Why are B-trees and B+ trees preferred for databases and filesystems over binary search trees?
Their high branching factor makes them **shallow**, minimizing the number of slow disk/block accesses (each node maps to a disk page). Height $O(\log_t n)$ with large $t$ means very few I/Os per operation, unlike a binary tree's $\log_2 n$ deeper structure.
What is the key structural difference between a B-tree and a B+ tree?
In a **B-tree**, keys (and their data) live in both internal and leaf nodes. In a **B+ tree**, all actual data/records reside only in the **leaves**; internal nodes hold only separator keys for routing. B+ tree leaves are also linked in a sorted linked list, enabling fast in-order range scans.
What is a binary heap and what is the heap-order property for a min-heap vs. a max-heap?
A binary heap is a **complete** binary tree stored in an array. **Min-heap**: every parent's key $\leq$ its children's keys (root is the minimum). **Max-heap**: every parent's key $\geq$ its children's keys (root is the maximum). It gives no ordering between siblings.
For a 0-indexed array representation of a binary heap, give the index formulas for a node's parent, left child, and right child.
For index $i$: parent $= \lfloor \frac{i-1}{2} \rfloor$, left child $= 2i+1$, right child $= 2i+2$. (For 1-indexed heaps: parent $= \lfloor i/2 \rfloor$, left $= 2i$, right $= 2i+1$.)
Describe the sift-up (bubble-up) operation used when inserting into a binary heap.
Append the new element at the end of the array (next leaf position). Then repeatedly compare it with its parent; if it violates the heap order, swap them. Continue until the parent satisfies the order or the root is reached. Cost $O(\log n)$.
Describe the sift-down (heapify) operation used when extracting the root of a binary heap.
Remove the root, move the last element to the root position, then repeatedly swap it with its **smaller child** (min-heap) / larger child (max-heap) until heap order is restored or a leaf is reached. Cost $O(\log n)$. This underlies extract-min/extract-max.
What is the time complexity of building a heap from an unsorted array of $n$ elements, and why is it $O(n)$ rather than $O(n\log n)$?
**Build-heap runs in $O(n)$**. Applying sift-down from the last internal node up to the root, the work is $\sum_{h} \frac{n}{2^{h+1}} \cdot O(h) = O\!\left(n \sum_{h=0}^{\infty} \frac{h}{2^{h}}\right) = O(n)$, because most nodes are near the bottom with small sift-down cost.
Give the time complexities for the core binary heap operations: peek, insert, extract-min/max, and decrease-key.
**Peek** (find-min/max): $O(1)$. **Insert**: $O(\log n)$. **Extract-min/max**: $O(\log n)$. **Decrease-key / increase-key**: $O(\log n)$. **Build-heap**: $O(n)$. **Delete arbitrary** (given index): $O(\log n)$.
How do you find the $k$-th largest element in an array using a heap, and what is the complexity?
Maintain a **min-heap of size $k$**: iterate through elements, push each; if the heap exceeds size $k$, pop the smallest. After processing, the heap's root is the $k$-th largest. Time $O(n \log k)$, space $O(k)$. (Quickselect gives $O(n)$ average as an alternative.)
Describe the K-way merge problem and how a heap solves it efficiently.
Merge $k$ sorted lists (total $n$ elements) into one sorted output. Use a **min-heap of size $k$** holding the current front element of each list. Repeatedly pop the smallest, output it, and push the next element from that element's list. Time $O(n \log k)$, space $O(k)$.
For the Top-K frequent elements problem, describe an efficient heap-based approach and its complexity.
Count frequencies with a hash map ($O(n)$), then maintain a **min-heap of size $k$** keyed by frequency, popping the least frequent when size exceeds $k$. The heap ends holding the $k$ most frequent. Time $O(n \log k)$. (Bucket sort by frequency gives $O(n)$.)
What is a Trie (prefix tree), and what does each node/edge represent?
A Trie is a tree for storing strings where each **edge represents a character** and each path from the root spells a prefix. Nodes typically hold a map/array of child pointers and a boolean flag marking the end of a valid word. Common prefixes share the same path, saving space.
What are the time complexities of insert, search, and prefix-search (startsWith) in a Trie?
All are $O(L)$ where $L$ is the length of the query string — **independent of the number of stored words $n$**. This is the Trie's key advantage over hashing for prefix queries. Space can be large: up to $O(n \cdot L \cdot \sigma)$ with alphabet size $\sigma$ using array-based children.
Give one advantage of a Trie over a hash table for string storage.
Tries support efficient **prefix queries** (autocomplete, startsWith, longest-prefix matching) and return keys in sorted order, none of which hash tables do naturally. Lookups are $O(L)$ with no hashing collisions, and common prefixes are stored once.
What is a Segment Tree and what class of problems does it solve?
A Segment Tree is a binary tree over an array where each node stores an aggregate (sum, min, max, gcd, …) of a contiguous range. It answers **range queries and point/range updates** on a static-size array, both in $O(\log n)$, using $O(n)$ space (typically a $2n$ to $4n$ array).
State the time and space complexities for building, querying, and updating a Segment Tree.
**Build**: $O(n)$ time. **Range query**: $O(\log n)$. **Point update**: $O(\log n)$. **Range update with lazy propagation**: $O(\log n)$. **Space**: $O(n)$ (allocate about $4n$ or $2n$ for iterative).
What is *lazy propagation* in a Segment Tree and why is it needed?
Lazy propagation defers **range updates** by storing a pending update value at internal nodes and only pushing it down to children when those children are actually visited. Without it, a range update would touch $O(n)$ leaves; with it, both range update and range query stay $O(\log n)$.
What is a Fenwick Tree (Binary Indexed Tree, BIT) and what operations does it support?
A Fenwick Tree is an array-based structure supporting **prefix-sum queries** and **point updates**, each in $O(\log n)$, using only $O(n)$ space. It exploits the binary representation of indices: index $i$ is responsible for a range of length equal to its lowest set bit.
In a 1-indexed Fenwick Tree, how do you move to the next index during an update versus during a prefix-sum query?
Let $\text{lsb}(i) = i \,\&\, (-i)$ (lowest set bit). **Update** (add): move to $i \mathrel{+}= \text{lsb}(i)$, going upward covering ranges. **Prefix-sum query**: move to $i \mathrel{-}= \text{lsb}(i)$, accumulating until $i = 0$. Both take $O(\log n)$ steps.
Compare a Fenwick Tree and a Segment Tree for range-sum problems.
**Fenwick**: simpler, ~$O(n)$ space (single array), lower constant factors, but limited mostly to invertible aggregates (sums) and prefix-based queries. **Segment Tree**: more general (min/max/gcd/assignments, range updates via lazy propagation, arbitrary associative merges) at the cost of more memory (~$4n$) and code complexity. Both query/update in $O(\log n)$.
How is a range-sum $\text{sum}(l, r)$ over indices $l..r$ computed from a Fenwick Tree that supports prefix sums?
Using prefix sums $P(i) = \text{sum}(1..i)$: $$\text{sum}(l, r) = P(r) - P(l-1).$$ Each prefix sum is $O(\log n)$, so a range-sum query is $O(\log n)$. This subtraction trick requires an invertible operation (why Fenwick suits sums but not min/max directly).
Data Structures & Algorithms Trees & Hierarchical Structures FAQ
What is in the Data Structures & Algorithms Trees & Hierarchical Structures syllabus?
Data Structures & Algorithms Trees & Hierarchical Structures is split into 5 chapters: Tree Fundamentals, Binary Search Trees, Balanced Trees, Heaps & Priority Queues and Tries & Specialized Trees. Together they contain 16 topics.
How many chapters are in Data Structures & Algorithms Trees & Hierarchical Structures?
5 chapters, containing 16 topics. The largest chapter is Balanced Trees, with 4 topics.
Are there flashcards for Data Structures & Algorithms Trees & Hierarchical Structures?
Yes — a 50-card Trees & Hierarchical Structures deck, printed in full in the flashcards section of this page and available in the Examius app with spaced repetition. The free plan covers one exam of your choice, including its flashcards; Examius Pro, a one-time purchase, unlocks every other exam and deck.