juniorTrees

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 N nodes, it always has exactly N - 1 edges.
  • There is exactly one unique path between any two nodes.

For example, this is a tree:

TypeScript
A
/ \
B C
/ \
D E

The 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:

TypeScript
A
/ \
B---C
\
D

Here, A → B → C → A forms a cycle. This structure is a graph, but it is not a tree.

Key Differences

FeatureTreeGraph
StructureSpecialized graphGeneral relationship structure
CyclesNot allowedMay exist
ConnectivityAlways connectedCan be connected or disconnected
PathsExactly one path between two nodesMay have multiple paths
Number of edgesAlways N - 1Can vary
DirectionUsually treated as undirected, but can be directedCan be directed or undirected
Root nodeOften has a rootNo 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:

TypeScript
Graph
└── Tree
└── Binary Tree
└── Binary Search Tree

A 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:

TypeScript
Number of edges = N - 1

Example:

TypeScript
Nodes: 5
A
/ \
B C
/ \
D E
Edges:
A-B
A-C
B-D
B-E
Total edges = 4 = 5 - 1

If 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.

TypeScript
CEO
/ \
Manager1 Manager2
/ \
Employee1 Employee2

Each 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:

Python
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?

More Trees interview questions

View all →