Skip to content
BENIBIX Learn

Tree vs Graph

A tree is a graph with rules. It has one root and no loops, so there is one path to each node. A graph may link any node to any other, loops and all.

Development🧮 Algorithms & Data Structures

Tree (Data Structure)

Nodes joined in a branching shape, with one root at the top and no loops.

A tree is a data structure made of nodes joined in a branching shape. It has one node at the top, called the root. Each node can have child nodes below it, and nodes with no children are called leaves. There are no loops: you can never walk back to where you started. Folders on a computer and the HTML of a web page are both trees.

Example
Jack's computer keeps his files in a tree. The home folder is the root, and each photo is a leaf three folders down.
Why it matters
Computers store anything with parents and children as a tree. Files, web pages and menus are all trees.
Full entry
Development🧮 Algorithms & Data Structures

Graph (Data Structure)

Nodes joined by edges, where any node can link to any other.

A graph is a set of nodes joined by lines called edges. Unlike a tree, any node can link to any other, and the links can form loops. Edges can have a direction, like a one-way street, and a weight, like a distance. Road maps, friend networks and links between web pages are all graphs. Many hard problems become easier once you see them as one.

Example
Emma's social app stores who follows whom as a graph. Finding friends of friends means walking two edges out from her.
Why it matters
Road trips, friend lists and web links are all graph problems underneath.
Full entry

In one line

Tree (Data Structure)
Nodes joined in a branching shape, with one root at the top and no loops.
Graph (Data Structure)
Nodes joined by edges, where any node can link to any other.