Implementing a Binary Search Tree in JavaScript

A tree is a collection of nodes connected by edges, where each node holds data and references to its children. Binary Search Trees (BST) are a special type of binary tree that maintains a sorted order.

What is a Binary Search Tree?

A Binary Search Tree is a binary tree where nodes with lesser values are stored on the left, and nodes with higher values are stored on the right. This property makes searching, insertion, and deletion operations efficient.

25 20 36 10 22 30 40

Step 1: Creating the Node Class

Each node in a BST contains data and references to left and right children:

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

// Creating a new node
const newNode = new Node(23);
console.log(newNode);
Node { data: 23, left: null, right: null }

Step 2: Creating the Binary Search Tree Class

The BST class manages the tree structure and provides methods for operations:

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

const bst = new BinarySearchTree();
console.log("Empty BST root:", bst.root);
Empty BST root: null

Step 3: Implementing Insert Method

The insert method places new nodes according to BST rules - smaller values go left, larger values go right:

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

class BinarySearchTree {
    constructor() {
        this.root = null;
    }
    
    insert(data) {
        const newNode = new Node(data);
        if (this.root === null) {
            this.root = newNode;
        } else {
            this.insertNode(this.root, newNode);
        }
    }
    
    insertNode(node, newNode) {
        if (newNode.data < node.data) {
            if (node.left === null) {
                node.left = newNode;
            } else {
                this.insertNode(node.left, newNode);
            }
        } else {
            if (node.right === null) {
                node.right = newNode;
            } else {
                this.insertNode(node.right, newNode);
            }
        }
    }
    
    // Helper method to display tree structure
    inOrderTraversal(node = this.root, result = []) {
        if (node !== null) {
            this.inOrderTraversal(node.left, result);
            result.push(node.data);
            this.inOrderTraversal(node.right, result);
        }
        return result;
    }
}

// Example usage
const BST = new BinarySearchTree();
BST.insert(25);
BST.insert(20);
BST.insert(36);
BST.insert(10);
BST.insert(22);
BST.insert(30);
BST.insert(40);

console.log("BST in sorted order:", BST.inOrderTraversal());
console.log("Root node:", BST.root.data);
console.log("Left child of root:", BST.root.left.data);
console.log("Right child of root:", BST.root.right.data);
BST in sorted order: [ 10, 20, 22, 25, 30, 36, 40 ]
Root node: 25
Left child of root: 20
Right child of root: 36

How BST Insertion Works

The insertion process follows these steps:

  1. If the tree is empty, the new node becomes the root
  2. Compare the new value with the current node
  3. If smaller, go to the left subtree; if larger, go to the right subtree
  4. Repeat until finding an empty position
  5. Insert the new node at that position

Key Properties of BST

Binary Search Trees maintain several important properties:

  • Left Subtree: All nodes have values less than the parent
  • Right Subtree: All nodes have values greater than the parent
  • In-order Traversal: Produces a sorted sequence
  • Time Complexity: O(log n) for balanced trees, O(n) for unbalanced

Conclusion

Binary Search Trees provide an efficient way to store and retrieve sorted data. The key is maintaining the BST property where left children are smaller and right children are larger than their parent node.

Updated on: 2026-03-15T23:19:00+05:30

4K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements