DEV Community

Cover image for 🌳 Binary Trees Demystified: A Beginner’s Guide Using JavaScript
Rakesh Reddy Peddamallu
Rakesh Reddy Peddamallu

Posted on

🌳 Binary Trees Demystified: A Beginner’s Guide Using JavaScript

If you’ve ever scratched your head at the mention of binary trees or felt overwhelmed by tree data structures in general—don’t worry, you’re not alone. Binary trees are one of the most fundamental and powerful data structures in computer science, and once you understand how they work, a whole new world of algorithmic problem-solving opens up.

In this guide, we’ll break binary trees down into digestible chunks and build one from scratch using JavaScript. By the end, you’ll understand what binary trees are, why they matter, and how to implement them in code.


🧠 What is a Binary Tree?

A binary tree is a hierarchical data structure in which each node has at most two children—commonly referred to as the left and right child.

Here’s a visual example:

       10
      /  \
     5    15
    / \
   3   7
Enter fullscreen mode Exit fullscreen mode

In the tree above:

  • 10 is the root node.
  • 5 and 15 are children of 10.
  • 3 and 7 are children of 5.

šŸ“¦ Each node in a binary tree contains:

  • A value
  • A pointer to a left child node (if any)
  • A pointer to a right child node (if any)

🧱 Real-World Analogies

Binary trees might sound abstract, but they show up everywhere:

  • File systems: Folders containing files and subfolders
  • Game decision trees: AI choosing optimal moves
  • Expression trees: Used in compilers and calculators
  • Hierarchical UI components: Think collapsible menus

šŸ›  Implementing a Binary Tree in JavaScript

Let’s roll up our sleeves and build a basic binary tree from scratch.

šŸ”¹ Step 1: Create a Node class

Each node will hold a value and pointers to its left and right children.

class Node {
  constructor(value) {
    this.value = value;
    this.left = null;
    this.right = null;
  }
}
Enter fullscreen mode Exit fullscreen mode

šŸ”ø Step 2: Create the BinaryTree class

This class manages the root and provides methods to work with the tree.

class BinaryTree {
  constructor() {
    this.root = null;
  }

  insert(value) {
    const newNode = new Node(value);

    if (!this.root) {
      this.root = newNode;
      return;
    }

    // Level-order insertion using a queue
    const queue = [this.root];
    while (queue.length) {
      const current = queue.shift();

      if (!current.left) {
        current.left = newNode;
        break;
      } else {
        queue.push(current.left);
      }

      if (!current.right) {
        current.right = newNode;
        break;
      } else {
        queue.push(current.right);
      }
    }
  }
}
Enter fullscreen mode Exit fullscreen mode

šŸ’” How This Insert Works

We’re using level-order insertion, which means we’re filling the tree from top to bottom and left to right. This is not a Binary Search Tree yet—that comes later in this series.

šŸ“„ Example Usage:

const tree = new BinaryTree();
tree.insert(10);
tree.insert(5);
tree.insert(15);
Enter fullscreen mode Exit fullscreen mode

After inserting:

       10
      /  \
     5    15
Enter fullscreen mode Exit fullscreen mode

šŸ¤” Why Are Binary Trees Useful?

Here’s why you should care about binary trees:

  • They form the foundation for more advanced trees: BST, AVL, Heaps, and Tries
  • They're used in search and sort algorithms
  • Great for storing hierarchical or recursive data
  • A must-know for coding interviews and system design

🧠 Common Terms

Term Meaning
Root The top node of the tree
Leaf A node with no children
Subtree A smaller tree within a tree
Depth Levels from the root to the current node
Height Number of levels from node to deepest leaf

šŸ Wrapping Up

In this post, you learned:

  • What a binary tree is and why it’s useful
  • How to model a node and a binary tree in JavaScript
  • How to insert elements using level-order logic

Top comments (0)