How to find a value is present in binary tree or not in JavaScript ?

We are required to write a JavaScript function on the prototype object of a BinarySearchTree data type that takes in a value and finds whether or not that value is contained in the BST.

Binary Search Tree Structure

A Binary Search Tree (BST) is a tree data structure where each node has at most two children. The left child contains values smaller than the parent, and the right child contains values greater than the parent.

50 30 70 20 40

Implementation

Here's how to implement a Binary Search Tree with a search function:

// class for a single Node for BST
class Node {
    constructor(value) {
        this.value = value;
        this.left = null;
        this.right = null;
    }
}

// class for BST
// contains function to insert node and search for existing nodes
class BinarySearchTree {
    constructor() {
        this._root = null;
    }
    
    insert(value) {
        let node = this, side = '_root';
        while (node[side]) {
            node = node[side];
            if (value === node.value) {
                return; // Value already exists
            }
            side = value < node.value ? 'left' : 'right';
        }
        node[side] = new Node(value);
    }
    
    contains(value) {
        let current = this._root;
        while (current) {
            if (value === current.value) {
                return true;
            }
            current = value < current.value ? current.left : current.right;
        }
        return false;
    }
}

const tree = new BinarySearchTree();
// Insert some values
tree.insert(50);
tree.insert(30);
tree.insert(70);
tree.insert(20);
tree.insert(40);
tree.insert(60);
tree.insert(80);

console.log("Searching for 40:", tree.contains(40));
console.log("Searching for 25:", tree.contains(25));
console.log("Searching for 70:", tree.contains(70));
console.log("Searching for 100:", tree.contains(100));
Searching for 40: true
Searching for 25: false
Searching for 70: true
Searching for 100: false

How the Search Algorithm Works

The contains() method uses the BST property to efficiently search:

  1. Start at the root node
  2. Compare the target value with current node's value
  3. If equal, return true
  4. If target is smaller, move to left child
  5. If target is larger, move to right child
  6. If we reach null, the value doesn't exist

Time Complexity

Case Time Complexity Description
Best/Average O(log n) Balanced tree
Worst O(n) Skewed tree (like a linked list)

Recursive Implementation

Alternative recursive approach for the contains method:

class BinarySearchTree {
    constructor() {
        this._root = null;
    }
    
    insert(value) {
        this._root = this._insertRecursive(this._root, value);
    }
    
    _insertRecursive(node, value) {
        if (node === null) {
            return new Node(value);
        }
        
        if (value < node.value) {
            node.left = this._insertRecursive(node.left, value);
        } else if (value > node.value) {
            node.right = this._insertRecursive(node.right, value);
        }
        
        return node;
    }
    
    contains(value) {
        return this._containsRecursive(this._root, value);
    }
    
    _containsRecursive(node, value) {
        if (node === null) {
            return false;
        }
        
        if (value === node.value) {
            return true;
        }
        
        return value < node.value 
            ? this._containsRecursive(node.left, value)
            : this._containsRecursive(node.right, value);
    }
}

// Test the recursive implementation
const recursiveTree = new BinarySearchTree();
recursiveTree.insert(15);
recursiveTree.insert(10);
recursiveTree.insert(20);
recursiveTree.insert(8);

console.log("Found 10:", recursiveTree.contains(10));
console.log("Found 25:", recursiveTree.contains(25));
Found 10: true
Found 25: false

Conclusion

Binary Search Trees provide efficient searching with O(log n) average time complexity. The contains() method leverages the BST property to quickly determine if a value exists by comparing and traversing left or right based on the target value.

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

316 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements