What is the difference between a tree and a graph?
Updated Apr 28, 2026
Short answer
A tree is a special type of graph that is connected and has no cycles, meaning there is exactly one path between any two nodes. A graph is a more general structure that can have cycles, multiple paths between nodes, disconnected components, and different types of relationships. Every tree is a graph, but not every graph is a tree.
Deep explanation
A graph is a data structure made up of:
- Vertices (nodes): The objects or entities being represented.
- Edges: The connections or relationships between those objects.
Graphs are very flexible. They can represent almost any network of relationships.
A tree is a restricted form of graph with additional rules:
- It must be connected: Every node must be reachable from every other node.
- It must be acyclic: It cannot contain any cycles.
- For
Nnodes, it always has exactlyN - 1edges. - There is exactly one unique path between any two nodes.
For example, this is a tree:
A / \ B C / \ D EThe paths are unique:
- A → B → D
- A → B → E
- A → C
There is no way to start at one node and return to it by following edges, so there are no cycles.
A general graph can look like this:
A / \ B---C \ DHere, A → B → C → A forms a cycle. This structure is a graph, but it is not a tree.
Key Differences
| Feature | Tree | Graph |
|---|---|---|
| Structure | Specialized graph | General relationship structure |
| Cycles | Not allowed | May exist |
| Connectivity | Always connected | Can be connected or disconnected |
| Paths | Exactly one path between two nodes | May have multiple paths |
| Number of edges | Always N - 1 | Can vary |
| Direction | Usually treated as undirected, but can be directed | Can be directed or undirected |
| Root node | Often has a root | No root required |
Why Trees Are Useful
Trees add structure and constraints, which make many operations more efficient.
For example, in a binary search tree (BST):
- Smaller values are stored on the left.
- Larger values are stored on the right.
- Searching can avoid checking every node.
A balanced BST can search in approximately O(log N) time.
Graphs are more suitable when relationships are not naturally hierarchical:
- Social networks
- Road maps
- Computer networks
- Recommendation systems
Relationship Between Trees and Graphs
The relationship can be summarized as:
Graph └── Tree └── Binary Tree └── Binary Search TreeA tree inherits graph concepts such as nodes and edges but adds restrictions that make it easier to reason about.
Common Tree Properties
For a tree with N nodes:
Number of edges = N - 1Example:
Nodes: 5
A / \ B C / \ D E
Edges:A-BA-CB-DB-E
Total edges = 4 = 5 - 1If a connected structure has more than N - 1 edges, it must contain a cycle and is therefore not a tree.
Real-world example
A company's organisational hierarchy is naturally represented as a tree.
CEO / \ Manager1 Manager2 / \ Employee1 Employee2Each employee has one direct manager, and there is exactly one path from an employee to the CEO.
A simple tree representation in code might look like:
class Employee: def __init__(self, name): self.name = name self.children = []
ceo = Employee("CEO")manager = Employee("Manager")employee = Employee("Employee")
ceo.children.append(manager)manager.children.append(employee)This works well as a tree because:
- An employee belongs to one branch of the hierarchy.
- There are no circular reporting relationships.
- The relationship flows from parent to child.
A graph would be better if employees could have multiple types of relationships, such as mentorship, project collaboration, and reporting lines all at once.
Common mistakes
- * Assuming every graph is a tree
- a graph only becomes a tree if it satisfies the tree properties.
- * Forgetting that a tree must be connected
- a collection of disconnected trees is called a forest.
- * Thinking that all trees must have a root
- only rooted trees have a designated starting node.
- * Confusing binary trees with all trees
- a binary tree is a specific type of tree where each node has at most two children.
- * Adding an extra edge to a tree without realizing it creates a cycle.
- * Assuming graphs are always more complex in every case
- trees are graphs with useful constraints that often make algorithms simpler.
Follow-up questions
- Why does a tree with N nodes always have exactly N - 1 edges?
- What is the difference between a tree and a binary tree?
- What happens if you add an extra edge to a tree?
- When would you choose a graph instead of a tree?
- What is the difference between a tree and a forest?