Study guide: Trees
Overview
A tree is a connected graph with no cycles. Trees describe file systems, HTML documents, organisation charts, parse trees and decision processes, and special trees such as binary search trees make searching fast.
Several different-looking conditions turn out to be equivalent ways of saying “this is a tree”, and rooted binary trees add traversal orders that show up constantly in programming.
Key ideas explained
Equivalent definitions
For a graph with n vertices, these are equivalent: connected and acyclic; connected with n − 1 edges; acyclic with n − 1 edges; exactly one simple path between any two vertices. Every tree with at least two vertices has at least two leaves.
Rooted and binary trees
A rooted tree has a root, parents, children, leaves and internal vertices. In a binary tree every node has at most two children. A full binary tree with i internal nodes has i + 1 leaves, and a binary tree of height h has at most 2^(h+1) − 1 nodes.
Traversals
Preorder visits root, left, right. Inorder visits left, root, right. Postorder visits left, right, root. Level-order visits row by row (BFS). Inorder on a binary search tree returns the keys in sorted order.
Spanning trees and search trees
A spanning tree of a connected graph keeps all vertices using n − 1 edges; Kruskal's and Prim's algorithms find one of minimum total weight. In a binary search tree, left keys are smaller and right keys larger, so searching takes time proportional to the height, between log n and n.
Worked examples
All four traversals of one tree
Problem. A is the root with children B and C. B has children D and E. C has only a right child F.
- Preorder (root first): A, B, D, E, C, F.
- Inorder: left subtree of A gives D, B, E; then A; then C's subtree gives C, F.
- Postorder (root last): D, E, B, F, C, A.
- Level-order: A | B, C | D, E, F.
Answer. Preorder ABDECF, inorder DBEACF, postorder DEBFCA, level-order ABCDEF.
Is it a tree?
Problem. Five vertices with edges AB, BC, CD, DE. Then add AE.
- Four edges on five vertices, and the graph is connected: it is a tree (a path).
- Adding AE gives five edges and creates the cycle A–B–C–D–E–A.
Answer. The first graph is a tree; the second is not, since it has a cycle.
Enter these same values in the calculator above and compare each step with the working shown here.
Common mistakes to avoid
- Thinking n − 1 edges alone proves a tree. The graph must also be connected (or acyclic).
- Mixing up traversal orders. In preorder the root is first; in postorder it is last.
- Assuming a binary search tree is balanced. Inserting sorted keys produces a chain of height n − 1.
- Mixing up height, depth and number of levels, which differ by conventions. State yours.
Where this shows up in computer science
- File systems and database indexes (B-trees) are trees.
- Compilers build abstract syntax trees, and postorder traversal evaluates expression trees.
- Decision trees, game trees and Huffman coding trees are used in machine learning, AI and compression.
Test yourself
Try each question before opening the answer.
How many edges does a tree with 12 vertices have?
11.
A full binary tree has 10 internal nodes. How many leaves and nodes in total?
11 leaves and 21 nodes.
Which traversal of a binary search tree visits keys in sorted order?
Inorder.