DSA Binary Trees — Left & Right Visualized
Learn binary trees — at most two children per node, complete/full/perfect types, depth and height, interactive visualizer.
Introduction
Binary trees restrict each node to at most two children — left and right. This constraint enables BSTs, heaps, and AVL trees.
Quick index
1. Binary tree rules
4
/ \
2 6
/ \ / \
1 3 5 7Each node: left pointer + right pointer (or null).
2. Interactive visualizer
Binary Trees
At most two children per node — left and right — enabling search trees and heaps.
Each node has at most two children — left and right.
Binary tree node
class BinaryNode {
constructor(value) {
this.value = value
this.left = null
this.right = null
}
}Binary trees restrict branching to two — enables efficient search variants.
Max children
2
Nodes
7
Height
2
3. Full implementation
class BinaryNode {
constructor(value) {
this.value = value
this.left = null
this.right = null
}
}
function height(node) {
if (!node) return -1
return 1 + Math.max(height(node.left), height(node.right))
}
function countNodes(node) {
if (!node) return 0
return 1 + countNodes(node.left) + countNodes(node.right)
}
function buildCompleteTree() {
const root = new BinaryNode(4)
root.left = new BinaryNode(2)
root.right = new BinaryNode(6)
root.left.left = new BinaryNode(1)
root.left.right = new BinaryNode(3)
root.right.left = new BinaryNode(5)
root.right.right = new BinaryNode(7)
return root
}Function calls with output
// ── Demo calls — output shown on the right ──
const root = buildCompleteTree()
height(root) // → 2
countNodes(root) // → 7
root.left.value // → 2
root.right.right.value // → 74. Tree types
| Type | Definition |
|---|---|
| Full | Every node has 0 or 2 children |
| Complete | All levels filled left-to-right |
| Perfect | All leaves at same depth, full |
| Balanced | Height ≈ log(n) |
5. Real-life examples
| Scenario | Binary tree role |
|---|---|
| ML decision trees | Yes/no splits → left/right branches |
| Heap | Complete binary tree in array |
| Expression trees | Operators with two operands |
| Huffman coding | Variable-length prefix codes |
Decision tree in ML
Each node is a yes/no split — left/right branches based on feature threshold.
age <= 30?
├── Yes → buy (60%)
└── No → skip (70%)
Binary split at root — two branches only.
Real-world implementation
class DecisionNode {
constructor(feature, threshold, left, right) {
this.feature = feature; this.threshold = threshold
this.left = left; this.right = right
}
predict(sample) {
return sample[this.feature] <= this.threshold
? this.left.predict(sample)
: this.right.predict(sample)
}
}Full working code for the scenario above.
Function calls with output
// ── Function calls with output ──
root = new DecisionNode("age", 30, left, right)Side output shows return value after each call — step through to trace execution.
Summary
Binary trees cap branching at two — the foundation for search trees, heaps, and traversals.
Next reads: Pre-order Traversal · Binary Search Trees
Subscribe to my newsletter
Stay up to date and get notified when I share new contents.
No spam ever, unsubscribe anytime