💻 Computer Science · Topic 52 of 80

Trees and Graphs

Trees and graphs model hierarchies and networks.

Interactive labFollow the idea step by step

About this topic

A tree is a hierarchical data structure of nodes connected by edges, with one root at the top and each node having children below it. Examples include family trees and folder structures. A binary tree allows at most two children per node, and a binary search tree keeps smaller values on the left and larger on the right, making searching fast. A graph is more general: a set of nodes (vertices) joined by edges, which may be directed or have weights.

Trees are traversed in orders such as pre-order, in-order and post-order, and graphs are explored using breadth-first search, which spreads outward level by level, or depth-first search, which goes as deep as possible before backtracking. A tree is a special graph with no cycles.

Social networks are graphs where people are nodes and friendships are edges, and maps are graphs with roads as weighted edges. Search in a balanced binary search tree takes O(log n), but a badly shaped one can degrade to O(n). A common misconception is that trees grow upward in diagrams; computer scientists draw the root at the top. Always check for cycles when traversing graphs.

Key ideas

  1. Root, branches, leaves
  2. Binary search trees
  3. Graphs have nodes and edges

Quick check

Use cases: where Trees and Graphs helps

Computing ideas run phones, search engines, games and the wider internet.

Frequently asked questions

What is Trees and Graphs?

Trees and graphs model hierarchies and networks.

What will I learn in Trees and Graphs?

Root, branches, leaves. Binary search trees. Graphs have nodes and edges.

Is Trees and Graphs part of Computer Science?

Yes. It is one of 80 Computer Science topics on LearnViz HighSchoolEdu, with 3 key ideas, a live animation and a quick check.

Does the Trees and Graphs lab work on a phone or tablet?

Yes. The animation resizes to fit your screen and supports touch, so you can drag, tap and slide with your finger.

How should I study Trees and Graphs?

Read the summary and key ideas first, play with the interactive lab to see the idea in motion, then answer the quick check. If you get a question wrong, go back to the key idea it tests.

Do I need an account, and is my progress saved?

No account is needed. Marking a topic as learned is saved in your own browser, so it stays on that device and is not sent to a server.

More in Computer Science