Kazi Rahamatullah
Kazi Rahamatullah
AboutProjectsBlogContact
UsesBooks
ResumeView CV
Kazi Rahamatullah

© Copyright 2026 Kazi Rahamatullah

AboutProjectsBlogBooksUses
Twitter/XGitHubProduct HuntCodeSandbox
Back to Blog
DSATreesBinary Trees

DSA Binary Trees — Left & Right Visualized

Learn binary trees — at most two children per node, complete/full/perfect types, depth and height, interactive visualizer.

Jul 4, 20263 min read

Introduction

Binary trees restrict each node to at most two children — left and right. This constraint enables BSTs, heaps, and AVL trees.

Quick index

#Section
1Binary tree rules
2Interactive visualizer
3Full implementation
4Tree types
5Real-life examples

1. Binary tree rules

plaintext
       4
      / \
     2   6
    / \ / \
   1  3 5  7

Each node: left pointer + right pointer (or null).

Back to index


2. Interactive visualizer

Binary Trees

At most two children per node — left and right — enabling search trees and heaps.

4
2
1
3
6
5
7

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

Back to index


3. Full implementation

js
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

js
// ── Demo calls — output shown on the right ──
const root = buildCompleteTree()
 
height(root) // → 2
countNodes(root) // → 7
root.left.value // → 2
root.right.right.value // → 7

Back to index


4. Tree types

TypeDefinition
FullEvery node has 0 or 2 children
CompleteAll levels filled left-to-right
PerfectAll leaves at same depth, full
BalancedHeight ≈ log(n)

Back to index


5. Real-life examples

ScenarioBinary tree role
ML decision treesYes/no splits → left/right branches
HeapComplete binary tree in array
Expression treesOperators with two operands
Huffman codingVariable-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.

Back to index


Summary

Binary trees cap branching at two — the foundation for search trees, heaps, and traversals.

Next reads: Pre-order Traversal · Binary Search Trees

Share this article

XLinkedInFacebook
Kazi Rahamatullah

Written by

Kazi Rahamatullah

FullStack Developer

X / TwitterGitHubLinkedIn

Subscribe to my newsletter

Stay up to date and get notified when I share new contents.

No spam ever, unsubscribe anytime