Definition
A tree is a connected acyclic (no cycles) graph.
Equivalent Characterizations
For a graph with vertices, the following are equivalent:
- is a tree (connected and acyclic)
- is connected with exactly edges
- is acyclic with exactly edges
- Any two vertices are connected by a unique path
- is connected, but removing any edge disconnects it
- is acyclic, but adding any edge creates exactly one cycle
Basic Properties
Edge Count
Leaves
Every tree with vertices has at least 2 leaves (degree-1 vertices).
Proof: Sum of degrees = . Average degree , so some vertices have degree 1.
Subtrees
Removing an edge from a tree creates two smaller trees (or one tree if leaf removed).
Rooted Trees
A rooted tree designates one vertex as the root.
Terminology
| Term | Definition |
|---|---|
| Root | Designated starting vertex |
| Parent of | Unique vertex on path from to root (if is not root) |
| Child of | Vertex whose parent is |
| Ancestor | Vertex on path from to root |
| Descendant | Vertex for which is an ancestor |
| Sibling | Vertices with the same parent |
| Leaf | Vertex with no children |
| Internal vertex | Vertex with at least one child |
| Depth of | Length of path from root to |
| Height | Maximum depth of any vertex |
| Level | Set of vertices at depth |
Binary Trees
Each internal vertex has at most 2 children (left and right).
Types
Full Binary Tree: Every internal vertex has exactly 2 children.
Perfect Binary Tree: Full binary tree where all leaves are at the same depth.
Complete Binary Tree: All levels except possibly the last are full; last level filled left-to-right.
Properties
For a full binary tree with internal vertices and leaves:
- Total vertices:
- Leaves:
For a perfect binary tree of height :
- Vertices:
- Internal vertices:
- Leaves:
Spanning Trees
A spanning tree of a connected graph is a subgraph that:
- Is a tree
- Contains all vertices of
Existence
Every connected graph has at least one spanning tree.
Finding Spanning Trees
BFS: Produces spanning tree with shortest paths from root.
DFS: Produces spanning tree with deep branches.
Number of Spanning Trees
Cayley's Formula: The complete graph has labeled spanning trees.
Matrix Tree Theorem: Number of spanning trees equals any cofactor of the Laplacian matrix.
For the cycle : exactly spanning trees.
Minimum Spanning Trees
For a weighted connected graph, a minimum spanning tree (MST) has minimum total edge weight.
Kruskal's Algorithm
- Sort edges by weight
- Add edges in order, skipping those that create cycles
- Stop when edges added
Time:
Prim's Algorithm
- Start with any vertex
- Repeatedly add the minimum-weight edge connecting tree to non-tree vertex
- Stop when all vertices included
Time: with binary heap
Properties
- MST is unique if all edge weights are distinct
- Cut property: Minimum edge crossing any cut is in some MST
- Cycle property: Maximum edge in any cycle is not in some MST
Tree Traversals
Preorder
Visit root, then recursively visit children (left to right).
Inorder (Binary Trees)
Visit left subtree, root, then right subtree.
Postorder
Recursively visit children, then visit root.
Level Order (BFS)
Visit vertices level by level, left to right.
Applications
Expression Trees
Binary tree representing mathematical expressions.
- Leaves: operands
- Internal vertices: operators
- Inorder: infix notation
- Preorder: prefix notation
- Postorder: postfix notation
Huffman Trees
Binary tree for optimal prefix codes.
- More frequent symbols: shorter codes
- Less frequent: longer codes
Binary Search Trees
- Left child parent right child
- Search, insert, delete: where is height
- Balanced variants: AVL, Red-Black:
Prüfer Sequences
A Prüfer sequence is a unique -length sequence of vertex labels that encodes a labeled tree on vertices.
Tree to Prüfer Sequence
- Find the leaf with smallest label
- Add its neighbor's label to the sequence
- Remove the leaf
- Repeat until 2 vertices remain
Prüfer Sequence to Tree
Reconstructs the original tree.
Consequence
Bijection between labeled trees on vertices and sequences of length from .
Number of labeled trees: (Cayley's formula)