0
0
JavascriptHow-ToBeginner · 3 min read

How to Implement Binary Tree in JavaScript: Simple Guide

To implement a binary tree in JavaScript, create a Node class with value, left, and right properties, then build the tree by linking nodes. Use a BinaryTree class to manage the root node and tree operations.
📐

Syntax

A binary tree node has three parts: value to store data, left for the left child node, and right for the right child node. The BinaryTree class holds the root node and can have methods to add or traverse nodes.

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

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

Example

This example shows how to create nodes, link them to form a binary tree, and print values using in-order traversal (left, root, right).

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

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

  insert(value) {
    const newNode = new Node(value);
    if (!this.root) {
      this.root = newNode;
      return;
    }
    let current = this.root;
    while (true) {
      if (value < current.value) {
        if (!current.left) {
          current.left = newNode;
          return;
        }
        current = current.left;
      } else {
        if (!current.right) {
          current.right = newNode;
          return;
        }
        current = current.right;
      }
    }
  }

  inorderTraversal(node = this.root) {
    if (node) {
      this.inorderTraversal(node.left);
      console.log(node.value);
      this.inorderTraversal(node.right);
    }
  }
}

const tree = new BinaryTree();
tree.insert(10);
tree.insert(5);
tree.insert(15);
tree.insert(3);
tree.insert(7);
tree.inorderTraversal();
Output
3 5 7 10 15
⚠️

Common Pitfalls

Common mistakes include not initializing child nodes to null, forgetting to update the root when inserting the first node, and mixing up left and right child placement. Also, avoid infinite loops by ensuring the insert method moves correctly down the tree.

javascript
class Node {
  constructor(value) {
    this.value = value;
    // Missing initialization of left and right causes errors
    // this.left = null;
    // this.right = null;
  }
}

// Correct way:
class CorrectNode {
  constructor(value) {
    this.value = value;
    this.left = null;
    this.right = null;
  }
}
📊

Quick Reference

  • Node class: holds value, left, and right.
  • BinaryTree class: manages root and insertion.
  • Insert method: places smaller values left, larger right.
  • Traversal: in-order visits nodes in sorted order.

Key Takeaways

Create a Node class with value, left, and right properties initialized to null.
Use a BinaryTree class to manage the root node and insert new nodes correctly.
Insert smaller values to the left and larger to the right to maintain order.
Always initialize child nodes to avoid runtime errors.
Use traversal methods like in-order to visit nodes in sorted order.