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); // 5Anatomy of a tree
A (root) → B, C (internal nodes) → D, E, F (leaves)
Core vocabulary
The topmost node — the one with no parent. Node A in the diagram. Every tree has exactly one root.
A node with no children. D, E, F are leaves. Leaves live at the bottom of the tree.
Any node that is not a leaf. It has at least one child. B and C are internal nodes.
A connection between a parent node and a child node. A tree with n nodes has exactly n-1 edges.
Distance from the root to a specific node. Root is depth 0. B and C are depth 1. D, E, F are depth 2.
The longest path from root to any leaf. The example tree has height 2. An empty tree has height -1.
Any node plus all its descendants forms a valid subtree. B, D, E form a subtree rooted at B.
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
Each node has at most 2 children (left and right). The foundation for BSTs, heaps, and expression trees.
A binary tree where left child < parent < right child. Enables O(log n) search, insert, delete when balanced.
A self-balancing BST. Guarantees O(log n) operations by rebalancing after every insert/delete via rotations.
A complete binary tree where every parent is greater (max-heap) or smaller (min-heap) than its children. Powers priority queues.
A tree where each node represents a character. Used for prefix matching, autocomplete, and IP routing tables.
A multi-way balanced tree optimized for disk reads. Every database index you'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.
Tree is empty — insert a value or click a preset above
→ Insert values to build your BST
Tree node structure
1Node:2 value <- the data stored3 left <- pointer to left child (or null)4 right <- pointer to right child (or null)5 // Optional extras:6 parent <- pointer to parent node7 height <- cached height for AVL balancing89// 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
Constant time
Logarithmic time
Linear time
🎯 Quick Check
A tree has 7 nodes arranged as a perfect binary tree. What is its height?
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.