Chapter 5 Revision: Trees
Trees introduce non-linear hierarchical organization, offering logarithmic operational bounds by dividing the search domain hierarchically. From self-balancing memory trees (AVL and Red-Black) to syntax and expression trees, tree structures represent a cornerstone of algorithm design.
Use these flashcards for active recall. Try to answer the question out loud before clicking the card to flip it.
Flashcards 🎴
What is the Binary Search Tree (BST) Invariant?
X: all keys in the left subtree of X are strictly less than X.key, and all keys in the right subtree of X are strictly greater than X.key. An in-order traversal yields keys in strictly ascending sorted order.What are the balance factors and rotation types in an AVL Tree?
BF = height(left) - height(right) ∈ {-1, 0, 1}.• Rotations: Single Left (LL), Single Right (RR), Left-Right double rotation (LR), and Right-Left double rotation (RL), restoring balance in O(1) time after insertions.
What are the 5 invariant rules of a Red-Black Tree?
2. The root is always Black.
3. All leaves (NIL null pointers) are Black.
4. If a node is Red, both its children must be Black (no consecutive red nodes).
5. For each node, all simple paths from the node to descendant leaves contain the same number of black nodes (Equal Black-Height).
How do AVL Trees and Red-Black Trees compare in practical trade-offs?
• Red-Black Trees: More relaxed balance (height ≤ 2 log N); requires fewer rotations during insertions/deletions, preferred for write/insert-heavy workloads (used in Java's TreeMap and C++ std::map).
How do Pre-order, In-order, and Post-order traversals map to expression notations?
+ * 2 3 4).• In-order (Left-Root-Right): Infix notation with parentheses (e.g.
(2 * 3) + 4).• Post-order (Left-Right-Root): Postfix / Reverse Polish notation (e.g.
2 3 * 4 +), ideal for stack evaluation.