Every data structure covered so far in this course — arrays, linked lists, stacks, queues — is linear: each element has at most one "next" element. A tree breaks that pattern. It's a hierarchical structure, where a single element can branch out into multiple children, which makes it the natural fit for modeling anything with a nested, parent-child relationship.

Think of a company's org chart, a computer's file system (folders containing folders containing files), or the structure of this very website (a course containing modules containing lessons) — all of these are trees in disguise.

Core Terminology

A tree is built from nodes connected by edges. The vocabulary that describes a tree's shape:

                 A           <- root (the one node with no parent)
               /   \
              B     C         <- children of A; B and C are siblings
             / \     \
            D   E     F       <- D, E, F are leaves (no children)

Depth of D  = 2   (number of edges from the root down to D)
Height of A = 2   (number of edges on the longest path down to a leaf)
  • Root — the single top-level node with no parent (A above).
  • Parent / Child — a direct connection one level apart (A is B's parent; B is A's child).
  • Siblings — nodes sharing the same parent (B and C).
  • Leaf — a node with no children (D, E, F).
  • Depth — how many edges separate a node from the root.
  • Height — the longest path from a node down to any leaf beneath it; the height of the whole tree is the height of its root.

A Minimal Tree Node in Code

Unlike a linked list node, which points to one "next" node, a general tree node points to a collection of children:

class TreeNode:
    def __init__(self, value):
        self.value = value
        self.children = []

    def add_child(self, child_node):
        self.children.append(child_node)

root = TreeNode("CEO")
vp_eng = TreeNode("VP Engineering")
vp_sales = TreeNode("VP Sales")
root.add_child(vp_eng)
root.add_child(vp_sales)
vp_eng.add_child(TreeNode("Engineer A"))
vp_eng.add_child(TreeNode("Engineer B"))

print(root.value, "has", len(root.children), "direct reports")
print(vp_eng.value, "has", len(vp_eng.children), "direct reports")

Output:

CEO has 2 direct reports
VP Engineering has 2 direct reports

Why Trees Are Worth Learning Deeply

Trees underpin an enormous amount of real-world software: file systems, the DOM tree a browser builds from HTML, database indexes, autocomplete systems, and decision-making AI all rely on tree structures. The rest of this course builds directly on the vocabulary introduced here — starting with the most common specialization, the binary tree, where every node is restricted to at most two children.