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
Every node has exactly 0 or 2 children. No node has just one child.
All levels are fully filled except possibly the last, which is filled left to right. Heaps use this.
All internal nodes have 2 children and all leaves are at the same level. Has exactly 2^(h+1) - 1 nodes.
Height is O(log n). For any node, the heights of left and right subtrees differ by at most 1. AVL trees enforce this.
Every node has only one child. Effectively a linked list. Height = n. O(n) for all operations.
The four traversal orders
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.
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.
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
1inOrder(node): // Left, Root, Right2 if node = null: return3 inOrder(node.left)4 visit(node.value)5 inOrder(node.right)67preOrder(node): // Root, Left, Right8 if node = null: return9 visit(node.value)10 preOrder(node.left)11 preOrder(node.right)1213postOrder(node): // Left, Right, Root14 if node = null: return15 postOrder(node.left)16 postOrder(node.right)17 visit(node.value)1819levelOrder(root): // BFS — iterative20 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
Tree used for all four traversal examples below
Height calculation
1height(node): // O(n) — must visit every node2 if node = null: return -1 // empty tree has height -13 leftHeight <- height(node.left)4 rightHeight <- height(node.right)5 return 1 + max(leftHeight, rightHeight)67// 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) = 011// height(2) = 1 + max(0, 0) = 112// height(4) = 1 + max(1, 1) = 2
Time complexity
Linear time
Linear time
Logarithmic time
Linear time
🎯 Quick Check
What traversal order produces elements in sorted order for a Binary Search Tree?
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.