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 

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.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 

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

307 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements