Chapter 7 · Trees

Binary Trees

A binary tree is the simplest non-linear data structure. Each node has at most two children: left and right. This simple constraint leads to an elegant set of recursive algorithms that are the foundation of computer science.

What is a binary tree?

A binary tree is a tree where every node has at most two children, called left and right. It's the basis of binary search trees, heaps and expression trees. Its height decides performance: a balanced binary tree with n nodes has height about log₂ n, while a degenerate one (every node has a single child) is really a linked list with height n.

class Node { constructor(v) { this.value = v; this.left = null; this.right = null; } }

const height = (n) => (n ? 1 + Math.max(height(n.left), height(n.right)) : 0);
const countLeaves = (n) => (!n ? 0 : !n.left && !n.right ? 1 : countLeaves(n.left) + countLeaves(n.right));

Types of binary trees

Full binary tree

Every node has exactly 0 or 2 children. No node has just one child.

Complete binary tree

All levels are fully filled except possibly the last, which is filled left to right. Heaps use this.

Perfect binary tree

All internal nodes have 2 children and all leaves are at the same level. Has exactly 2^(h+1) - 1 nodes.

Balanced binary tree

Height is O(log n). For any node, the heights of left and right subtrees differ by at most 1. AVL trees enforce this.

Degenerate (skewed) tree

Every node has only one child. Effectively a linked list. Height = n. O(n) for all operations.

The four traversal orders

1 / 4
1

In-Order (Left → Root → Right)

Recurse into the left subtree first, then visit the current node, then recurse into the right subtree. For a BST, in-order traversal produces elements in sorted ascending order.

1 of 4

Live demo — build a BST

Insert values to build a binary search tree and observe how the BST property shapes the structure. The same shape can be traversed in multiple orders.

Binary Search Tree
preset:
normalpathfounddelete

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

→ Insert values to build your BST

Interactive sandbox — code a Binary Tree

Construct a full binary tree node architecture manually and implement pre-order, in-order, post-order, and level-order queue traversals in JavaScript. Run code execution live!

Code Editor

<pre id="out"></pre>

Preview

Traversal pseudocode

binary-tree-traversal.pseudo
1inOrder(node): // Left, Root, Right
2 if node = null: return
3 inOrder(node.left)
4 visit(node.value)
5 inOrder(node.right)
6 
7preOrder(node): // Root, Left, Right
8 if node = null: return
9 visit(node.value)
10 preOrder(node.left)
11 preOrder(node.right)
12 
13postOrder(node): // Left, Right, Root
14 if node = null: return
15 postOrder(node.left)
16 postOrder(node.right)
17 visit(node.value)
18 
19levelOrder(root): // BFS — iterative
20 queue <- [root]
21 while queue not empty:
22 node <- queue.dequeue()
23 visit(node.value)
24 if node.left != null: queue.enqueue(node.left)
25 if node.right != null: queue.enqueue(node.right)

Traversal on a concrete tree

4261357

Tree used for all four traversal examples below

In-Order1, 2, 3, 4, 5, 6, 7
Pre-Order4, 2, 1, 3, 6, 5, 7
Post-Order1, 3, 2, 5, 7, 6, 4
Level-Order4, 2, 6, 1, 3, 5, 7

Height calculation

tree-height.pseudo
1height(node): // O(n) — must visit every node
2 if node = null: return -1 // empty tree has height -1
3 leftHeight <- height(node.left)
4 rightHeight <- height(node.right)
5 return 1 + max(leftHeight, rightHeight)
6 
7// For tree with root=4 above:
8// height(4) = 1 + max(height(2), height(6))
9// height(2) = 1 + max(height(1), height(3))
10// height(1) = 1 + max(-1, -1) = 0
11// height(2) = 1 + max(0, 0) = 1
12// height(4) = 1 + max(1, 1) = 2
🔍The recursion pattern
Height is a classic example of the post-order pattern: process children first, then combine results at the parent. This bottom-up computation is fundamental to AVL tree balancing, which relies on knowing subtree heights.

Time complexity

Any traversal — must visit every node exactly onceO(n)

Linear time

Height calculation — must visit every nodeO(n)

Linear time

Search in a balanced binary treeO(log n)

Logarithmic time

Search in a degenerate (linear) treeO(n)

Linear time

🎯 Quick Check

What traversal order produces elements in sorted order for a Binary Search Tree?

💡Master one traversal, get all four free
In-order, pre-order, and post-order all follow the same recursive template — only the position of visit(node) changes. If you understand one, change two lines and you have all three. Level-order is the odd one out: it uses a queue.

Binary Trees: summary

Key points

  • Full: every node has 0 or 2 children. Complete: all levels filled except possibly the last, filled left to right. Perfect: all levels completely full.
  • Complete binary trees can be stored in a plain array (children of i are at 2i+1 and 2i+2).
  • Traversals: pre-order, in-order, post-order (depth-first) and level-order (breadth-first).

Frequently asked questions

›What is the maximum number of nodes in a binary tree of height h?

2ʰ − 1 when height counts levels, for a perfect binary tree.