Trees, BSTs and Tries
A rooted tree gives each non-root node one parent. Traversal order decides when the root of each subtree is processed.
Picture the idea
In a directory tree, report a folder before its contents for preorder, or compute its size after all child sizes for postorder.
When to use it
Use a hierarchy for nested relationships, a BST for ordered keys or a trie for prefix navigation.
How it works
At root 8 with children 3 and 10, preorder visits 8, 3, 10. Inorder visits 3, 8, 10. Level order removes nodes from a queue.
- Traverse left and right subtrees.
- Carry allowable key bounds through a BST.
- Rotate a subtree while preserving inorder order.
- Follow one trie edge per symbol.
A full tree traversal costs O(n). A BST search costs O(h) for height h, which may reach n. Trie lookup costs O(L) for L symbols under constant-time child lookup.
Watch for this mistake
Checking a BST only against each immediate parent misses violations deeper in a subtree.
Explain how your height convention treats an empty tree and a leaf.