Chapter 6 · Trees

Trees

Arrays and linked lists are linear — one element after another. Trees break free from the line. A tree is a hierarchical structure where each node can have multiple children, branching out like real trees but drawn upside down.

What is a tree data structure?

A tree is a hierarchy of nodes connected by edges, with one root at the top. Each node has zero or more children, and nodes without children are leaves. There are no cycles. Trees model anything hierarchical: the DOM, file systems, org charts, JSON, and the indexes that make databases fast.

const tree = {
  value: "html",
  children: [
    { value: "head", children: [{ value: "title", children: [] }] },
    { value: "body", children: [{ value: "h1", children: [] }] },
  ],
};
const count = (n) => 1 + n.children.reduce((s, c) => s + count(c), 0);   // 5

Anatomy of a tree

ABCDEF

A (root) → B, C (internal nodes) → D, E, F (leaves)

Core vocabulary

Root

The topmost node — the one with no parent. Node A in the diagram. Every tree has exactly one root.

Leaf

A node with no children. D, E, F are leaves. Leaves live at the bottom of the tree.

Internal node

Any node that is not a leaf. It has at least one child. B and C are internal nodes.

Edge

A connection between a parent node and a child node. A tree with n nodes has exactly n-1 edges.

Depth

Distance from the root to a specific node. Root is depth 0. B and C are depth 1. D, E, F are depth 2.

Height

The longest path from root to any leaf. The example tree has height 2. An empty tree has height -1.

Subtree

Any node plus all its descendants forms a valid subtree. B, D, E form a subtree rooted at B.

Degree

The number of children a node has. A has degree 2. B has degree 2. F has degree 0.

Why not use a linked list?

SLOW

Linked List: Find item in sorted data

500,000 comparisons

A million-element linked list requires scanning up to 1,000,000 nodes to find one element. O(n).

FAST

Binary Search Tree: Find same item

~20 comparisons

A balanced BST of 1,000,000 elements has height log2(1M) ≈ 20. Each step halves the search space. O(log n).

Types of trees

Binary Tree

Each node has at most 2 children (left and right). The foundation for BSTs, heaps, and expression trees.

Binary Search Tree (BST)

A binary tree where left child < parent < right child. Enables O(log n) search, insert, delete when balanced.

AVL Tree

A self-balancing BST. Guarantees O(log n) operations by rebalancing after every insert/delete via rotations.

Heap

A complete binary tree where every parent is greater (max-heap) or smaller (min-heap) than its children. Powers priority queues.

Trie

A tree where each node represents a character. Used for prefix matching, autocomplete, and IP routing tables.

B-Tree

A multi-way balanced tree optimized for disk reads. Every database index you&apos;ve ever used is likely a B-Tree or B+ Tree.

Live demo — build a BST

Insert numbers below and watch the binary search tree form. Smaller values branch left, larger values branch right — see the hierarchical structure build itself.

Binary Search Tree
preset:
normalpathfounddelete

Tree is empty — insert a value or click a preset above

→ Insert values to build your BST

Tree node structure

tree-node.pseudo
1Node:
2 value <- the data stored
3 left <- pointer to left child (or null)
4 right <- pointer to right child (or null)
5 // Optional extras:
6 parent <- pointer to parent node
7 height <- cached height for AVL balancing
8 
9// A tree IS just its root node.
10// Access the whole tree via the root reference.
11Tree:
12 root <- Node | null

Height vs depth at a glance

Access root — always directO(1)

Constant time

Search in a balanced BST (height = log n)O(log n)

Logarithmic time

Search in an unbalanced/degenerate tree (height = n)O(n)

Linear time

🎯 Quick Check

A tree has 7 nodes arranged as a perfect binary tree. What is its height?

🧠Trees are recursive by nature
Every subtree is itself a valid tree. This means almost every tree algorithm can be expressed recursively: solve the problem for the left subtree, solve it for the right subtree, combine. This recursive structure is the key to understanding all tree traversal algorithms.

Trees: summary

Key points

  • Terms: root, parent, child, sibling, leaf, depth (distance from the root) and height (longest path down).
  • A tree with n nodes has exactly n − 1 edges.
  • Most tree algorithms are naturally recursive: solve for the children, then combine.

Frequently asked questions

›What is the difference between a tree and a graph?

A tree is a special graph: connected, with no cycles, and (usually) a single root. General graphs can have cycles and any connections.