๐Ÿงฎ
๐Ÿงฎ Data Structures & Algorithms ยท DS-101 ยท Session 5 of 6

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.

โฑ 9 min read๐Ÿ“… Updated 2026-07-27๐Ÿ“Š Tables, charts & examples

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 thingAs a tree
File systemFolders containing folders and files
An org chartA boss branching to reports
A web page (the DOM)Nested elements
A family treeAncestors 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.

๐ŸŽฏ
Graphs model connection, and unlock powerful questions. A social network (people connected to people), a map (places connected by roads), the web (pages linked to pages) - all graphs. Once data is a graph, you can ask rich questions: shortest path (how do I get from A to B fastest?), who's connected to whom, is there a cycle? These are classic, powerful algorithms - and the stack's larzgraph implements them (BFS, DFS, Dijkstra, connected components) ready to use and learn from.

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.

Session 5 of 6 complete

Next up: Algorithms & Big-O Made Simple

Continue to Session 6 โ†’

Related lessons

More in this course

Course home → · Browse all courses →