Trees & Graphs: Modelling Relationships
Some data isn't a flat list โ it's connected. Trees (hierarchies) and graphs (networks) model relationships, and they power everything from file systems to social networks to maps.
Lists, sets and dicts are great for flat collections, but a lot of real data is connected: a family tree, a folder structure, a social network, a map of roads. Two structures model these relationships - trees (hierarchies, one thing branching into others) and graphs (networks, things connected in any pattern). They open the door to some of the most powerful ideas in programming.
Trees: hierarchies
A tree is data that branches out from a root - each item can have children, but only one parent. Hierarchies everywhere are trees.
| Real thing | As a tree |
|---|---|
| File system | Folders containing folders and files |
| An org chart | A boss branching to reports |
| A web page (the DOM) | Nested elements |
| A family tree | Ancestors branching to descendants |
Trees are perfect when data naturally nests. Because each node branches, you can often find things far faster than in a flat list (that's how databases and file systems stay fast).
Graphs: networks
A graph is the most general structure: nodes connected by edges in any pattern - no root, no hierarchy required. When things relate to each other freely, you have a graph.
Model connected data
- Tree - hierarchies that branch from a root (files, org charts, the DOM).
- Graph - general networks of nodes and edges (social graphs, maps, the web).
- They enable big ideas - fast search (trees), shortest paths and connections (graphs).
- larzgraph gives you the classic graph algorithms to use and study.
๐งฉ Do it with LarzOS
Turn this lesson into action with the exact free tools and AI workflows for the job:
๐ฏ Key takeaways
- A lot of real data is connected, not flat - trees and graphs model those relationships.
- A tree is a hierarchy branching from a root (files, org charts, the DOM) - and enables fast search.
- A graph is a general network of nodes and edges (social networks, maps, the web).
- Graphs unlock powerful questions - shortest path, connections, cycles - which larzgraph implements.
Frequently asked
What's the difference between a tree and a graph?
A tree is actually a special, restricted kind of graph. Both are made of nodes and connections, but a tree has a strict structure: there's a single root, every node has exactly one parent (except the root), connections flow downward in a hierarchy, and there are no cycles - think of a family tree or folder structure branching out. A graph is the general case with no such restrictions: any node can connect to any other, in any pattern, with no root and possibly cycles - think of a social network or a map where everything can link to everything. So: all trees are graphs, but most graphs aren't trees. Use a tree when data naturally nests into a hierarchy; use a general graph when relationships are free-form.
These sound advanced - do beginners really need trees and graphs?
You don't need to master graph algorithms on day one, but understanding that trees and graphs exist - and recognising when your data is actually connected rather than flat - is genuinely valuable even early on, because it changes how you model problems. You already use trees constantly (your file system, web pages) without thinking of them that way, and many real problems (relationships, navigation, dependencies) are naturally graphs. Knowing these structures exist means you'll reach for the right tool instead of forcing connected data into flat lists. Start by understanding the concepts and recognising the patterns; the specific algorithms (like shortest-path) you can learn as you need them, and libraries like larzgraph let you use and study them without implementing from scratch.
Related lessons
Stacks, Queues & Beyond
Some structures aren't about storage but about *order of access* โ stacks (last-in-first-oโฆ
Data Structures & AlgorithmsAlgorithms & Big-O Made Simple
An algorithm is just a recipe for solving a problem โ and Big-O is a simple way to talk abโฆ
Building with the Larz StackData & Storage โ Persisting Without a Server
Most apps don't need a database server on day one. This family gives you crash-safe storagโฆ