01 / 22

Tree Properties &
Traversal Concepts

Master the fundamental structure of trees in discrete mathematics: properties, key theorems, and the three classical traversal orders that unlock efficient data processing.

Discrete Math

What Is a Tree?

A tree is a connected, acyclic graph. It provides a hierarchical structure where every pair of vertices is connected by exactly one simple path: no loops, no redundant edges.

A B C D E F G 7 vertices, 6 edges: connected, acyclic

Key Terminology

Root
The designated top-level vertex with no parent. Every rooted tree has exactly one.
Leaf
A vertex with no children (degree 1 in a rooted tree, or degree 1 in an unrooted tree).
Depth
The number of edges on the path from the root to a given vertex.
Height
The maximum depth among all vertices: the longest root-to-leaf path length.

Why Trees Matter

Trees are everywhere in computer science and mathematics. They model hierarchies (file systems, organizational charts), enable efficient search (binary search trees), and underpin algorithms (spanning trees, expression parsing).

Data Structures
Binary trees, heaps, tries: all rely on tree properties for \( O(\log n) \) performance.
Networks
Spanning trees connect all nodes with minimum edges: crucial in network design.
Parsing
Expression trees represent arithmetic formulas, enabling systematic evaluation.

The Tree Characterization Theorem

1
\( G \) is a tree: connected and acyclic.
2
\( G \) is connected and has exactly \( n - 1 \) edges.
3
\( G \) is acyclic and has exactly \( n - 1 \) edges.
4
There is exactly one simple path between every pair of vertices.
5
\( G \) is connected, but removing any edge disconnects it.

The \( n - 1 \) Edge Rule

\( n = 1 \)
\( e = 0 \)
\( n = 2 \)
\( e = 1 \)
\( n = 4 \)
\( e = 3 \)
\( n = 6 \)
\( e = 5 \)
Every tree: \( |E| = |V| - 1 \)
Adding one edge always creates exactly one cycle

Binary vs General Trees

Binary Tree General Tree
Max children per node 2 (left, right) Unlimited
Max nodes at depth \( d \) \( 2^d \) No fixed limit
Max total nodes (height \( h \)) \( 2^{h+1} - 1 \) No fixed limit
Traversal patterns In/Pre/Post/Level Pre/Post/Level
Common use Search, heaps, expressions File systems, XML/HTML DOM

Three Classical Traversals

A tree traversal is a systematic way to visit every node exactly once. The three depth-first traversals differ only in when you process the root relative to its subtrees:

Pre-order
Root first, then left, then right. Visits parents before children.
In-order
Left first, then root, then right. Produces sorted output for BSTs.
Post-order
Left first, then right, then root last. Visits children before parents.

How to Traverse a Binary Tree

1
Identify the root of the tree (or subtree) you are currently processing.
2
Choose your traversal order: decide when to "record" the root: before (pre), between (in), or after (post) recursing into subtrees.
3
Recurse left: apply the same traversal to the left subtree.
4
Recurse right: apply the same traversal to the right subtree.
5
Base case: if the current node is null (empty subtree), return immediately: nothing to visit.

Example: Pre-order Traversal

F B G A D I 1 2 3 4 5 6

Example: In-order Traversal

F B G A D I 4 2 1 3 5 6

Example: Post-order Traversal

F B G A D I 6 3 1 2 5 4

Level-order Traversal (BFS)

Unlike depth-first traversals, level-order visits nodes layer by layer, from left to right at each depth. It uses a queue instead of recursion.

depth 0 depth 1 depth 2 F B G A D I Level-order: F, B, G, A, D, I

Spanning Trees

A spanning tree of a connected graph \( G \) is a subgraph that includes all vertices of \( G \) and is itself a tree. It keeps the graph connected using the minimum number of edges.

Original Graph 1 2 3 4 5 Spanning Tree 1 2 3 4 5
\( n = 5 \) vertices, \( e = 4 \) edges

Expression Trees

An expression tree represents an arithmetic expression. Internal nodes hold operators; leaves hold operands. The three traversals produce different notations:

+ * 5 3 4
Pre-order (Prefix)
\( + \; * \; 3 \; 4 \; 5 \)
In-order (Infix)
\( 3 * 4 + 5 \)
Post-order (Postfix)
\( 3 \; 4 \; * \; 5 \; + \)

Common Mistakes

✕ Confusing Pre and Post
Pre-order visits the root first; post-order visits it last. Mixing them up produces a completely different, incorrect sequence.
✕ Wrong Edge Count
A tree with \( n \) vertices has \( n - 1 \) edges, not \( n \). Off-by-one errors are the most common tree mistake.
✕ Applying In-order to General Trees
In-order traversal only makes sense for binary trees. General trees (3+ children) have no natural "middle" position.

Traversal Summary

Pre-order In-order Post-order Level-order
Visit root Before subtrees Between subtrees After subtrees By depth layer
Strategy DFS (stack) DFS (stack) DFS (stack) BFS (queue)
Notation Prefix (Polish) Infix (standard) Postfix (RPN) N/A
Tree type Any Binary only Any Any

Question 1 of 5

A tree has 12 vertices. How many edges does it have?

Question 2 of 5

Given the binary tree below, what is the in-order traversal?

M H R C K

Question 3 of 5

Which statement is always true about a tree?

Question 4 of 5

The post-order traversal of an expression tree produces which notation?

Question 5 of 5

A connected graph with 8 vertices and 7 edges is guaranteed to be: