Trees & Forests

Tree properties, spanning trees, and traversal algorithms

Trees & Forests

A tree is one of the most important structures in computer science. It's a connected graph with no cycles — meaning there's exactly one path between any two vertices. Trees appear everywhere: file systems, HTML documents, decision trees, and more!

In this section, you'll learn to identify trees, find spanning trees in graphs, traverse trees efficiently, and explore the special case of binary trees.

What You'll Learn

🌳
Tree Properties
Connected, acyclic, |E| = |V| - 1
🔗
Spanning Trees
Prim's algorithm for MST
🔍
Tree Traversals
BFS (level-order) and DFS (pre-order)
🌲
Binary Trees
Height, leaves, full vs complete

Tree Properties Explorer

A tree is a connected graph with no cycles. It always has exactly |V| - 1 edges. Click vertices to add or remove edges and see when the graph becomes a valid tree!

Click two vertices to toggle an edge

6
Vertices |V|
5
Edges |E|

Tree Requirements

|E| = |V| - 1 (5 = 5?)
Connected (1 component)
No cycles (acyclic)
Valid Tree!

Key Facts About Trees

  • A tree with n vertices has exactly n-1 edges
  • There is a unique path between any two vertices
  • Adding any edge creates exactly one cycle
  • Removing any edge disconnects the tree
  • A forest is a graph with no cycles (multiple trees)

Spanning Tree Finder

A spanning tree connects all vertices with exactly |V| - 1 edges. Watch Prim's algorithm build a minimum spanning tree by always choosing the smallest edge that connects a new vertex!

0/6
Vertices Visited
0/5
Tree Edges
0
Total Weight

Prim's Algorithm

  1. Start from any vertex (mark it as visited)
  2. Find all edges connecting visited to unvisited vertices
  3. Pick the edge with minimum weight
  4. Add that edge to the tree, mark new vertex as visited
  5. Repeat until all vertices are visited

Tree Traversal Visualizer

Watch how BFS (Breadth-First Search) visits nodes level by level, while DFS (Depth-First Search) goes deep before backtracking.

Current
In Queue
Visited

BFS (Breadth-First)

  • Uses a queue (FIFO)
  • Visits level by level
  • Finds shortest path in unweighted graphs

DFS (Depth-First)

  • Uses a stack (LIFO)
  • Goes deep before backtracking
  • Good for exploring all paths

Binary Tree Explorer

A binary tree is a tree where each node has at most two children: a left child and a right child. Click a node to select it, then add or remove children.

Click a node to select it

6
Nodes
2
Height
3
Leaves
No
Full Binary?
Click a node to select it and modify the tree

Binary Tree Terms

Height: Longest path from root to leaf
Leaf: Node with no children
Full: Every node has 0 or 2 children
Complete: All levels full except possibly last

What We've Learned

1. Tree Properties: A tree is a connected, acyclic graph. With n vertices, it has exactly n-1 edges. There's a unique path between any two vertices. A forest is a graph with no cycles (a collection of trees).

2. Spanning Trees: A spanning tree of a graph connects all vertices with minimum edges. Prim's algorithm builds an MST by greedily adding the minimum weight edge that connects a new vertex.

3. Tree Traversals: BFS uses a queue and visits nodes level by level. DFS uses a stack (or recursion) and explores deep before backtracking. Both visit every node exactly once.

4. Binary Trees: Each node has at most 2 children. Height is the longest root-to-leaf path. A full binary tree has every node with 0 or 2 children. Binary trees are fundamental to search algorithms and data structures.

Next Up: In the next section, you'll learn about paths and cycles — including the famous Königsberg bridge problem, Eulerian paths, and Dijkstra's shortest path algorithm!