DEV Community

Cover image for Understanding Binary Search Trees (BST)
Rizwanul Islam Rudra
Rizwanul Islam Rudra

Posted on

Understanding Binary Search Trees (BST)

I was solving some binary search tree-related problems and thought it could be interesting to revise my memory and share what I learned with my followers! So here we go:

What is a Binary Search Tree (BST)

A Binary Search Tree (BST) is a foundational data structure in computer science that allows for efficient searching, insertion, and deletion of data. It's a tree-based structure where every node has at most two children, and the left child is always smaller than the parent node, while the right child is larger.

Key Features of a BST

1. Efficient Searching: With a time complexity of O(log n) for balanced trees.

2. Dynamic Structure: Nodes can be added or removed dynamically.

3. Hierarchical Representation: Useful in hierarchical data representation, like a filesystem or a family tree.


Let’s dive into a practical implementation of a Binary Search Tree using TypeScript.

class Node {
    value: number;
    left: Node | null;
    right: Node | null;

    constructor(value: number) {
        this.value = value;
        this.left = null;
        this.right = null;
    }
}

class BinarySearchTree {
    root: Node | null;

    constructor() {
        this.root = null;
    }

    insert(value: number): void {
        const newNode = new Node(value);
        if (this.root === null) {
            this.root = newNode;
            return;
        }

        let currentNode = this.root;
        while (true) {
            if (value < currentNode.value) {
                if (currentNode.left === null) {
                    currentNode.left = newNode;
                    return;
                }
                currentNode = currentNode.left;
            } else {
                if (currentNode.right === null) {
                    currentNode.right = newNode;
                    return;
                }
                currentNode = currentNode.right;
            }
        }
    }

    contains(value: number): boolean {
        let currentNode = this.root;

        while (currentNode !== null) {
            if (value === currentNode.value) return true;
            currentNode = value < currentNode.value ? currentNode.left : currentNode.right;
        }

        return false;
    }

    // In-order Traversal: Left -> Root -> Right
    inOrderTraversal(node: Node | null = this.root): void {
        if (node !== null) {
            this.inOrderTraversal(node.left);
            console.log(node.value);
            this.inOrderTraversal(node.right);
        }
    }
}

// Usage
const bst = new BinarySearchTree();
bst.insert(47);
bst.insert(21);
bst.insert(76);
bst.insert(18);
bst.insert(52);
bst.insert(82);

console.log("Contains 21:", bst.contains(21)); // true
console.log("Contains 99:", bst.contains(99)); // false

console.log("In-order Traversal:");
bst.inOrderTraversal();

Enter fullscreen mode Exit fullscreen mode

Diagram Representation of the BST

Here’s what the Binary Search Tree would look like after inserting the values 47, 21, 76, 18, 52, 82:

Binary Search Tree (BST)


How it Works

  1. Insert: New values are placed based on comparisons. Smaller values go to the left, and larger values go to the right.

  2. Search (Contains): Traverse left or right depending on the value until the node is found or the traversal ends at a null node.

  3. Traversal: In-order traversal visits nodes in sorted order (Left -> Root -> Right).


Why Use Binary Search Trees?

  1. Efficient Lookups: Searching in a BST can be very efficient when the tree is balanced.

  2. Dynamic Size: You can add or remove elements without needing to resize arrays or shift elements.

  3. Sorted Data: Traversals provide data in sorted order, useful in scenarios like priority queues and in-memory databases.


Edge Cases to Keep in Mind

  1. Duplicates: Standard BSTs do not handle duplicate values by default. You may need to implement logic to allow or reject duplicates, such as storing a count in each node or skipping duplicate insertions.

  2. Unbalanced Trees: If values are inserted in sorted order (e.g., 1, 2, 3, 4, ...), the BST becomes skewed and degrades to a linked list with O(n) time complexity for operations. Using self-balancing BSTs (e.g., AVL trees, Red-Black trees) helps mitigate this issue.

  3. Empty Tree: Always check for the case where the tree is empty (i.e., this.root === null) to prevent runtime errors during operations like contains or traversal.

  4. Edge Nodes: In scenarios like removing nodes, consider edge cases such as nodes with only one child, no children, or being the root node.

  5. Performance: If your dataset is large or comes in sorted chunks, consider rebalancing or using a more appropriate data structure for efficient lookups.

To ensure efficiency, the BST should remain balanced. Unbalanced trees can degrade performance to O(n). Consider using self-balancing trees like AVL or Red-Black Trees for consistently optimized performance. I will discuss about the other trees in a post later on.


Use Cases of BSTs in Software Applications

Binary Search Trees (BSTs) are more than just a data structure you encounter in textbooks—they have tons of real-world applications! Here are some practical ways BSTs are used in computer science:

  • Databases and Indexing: Balanced BSTs (like AVL or Red-Black Trees) are often behind the scenes in database indexing. They make range queries and lookups super-efficient.

  • In-Memory Sorted Data: Need to keep data sorted while adding and searching dynamically? BSTs are your go-to. Think real-time analytics or monitoring systems.

  • Symbol Tables in Compilers: Compilers rely on BSTs to implement symbol tables for storing and quickly accessing identifiers and their attributes.

  • Autocompletion and Spell Checkers: Ever wondered how autocompletion works? BST variations, like Ternary Search Trees, are used to organize dictionaries of words efficiently.

  • Priority Scheduling: While heaps are more common, BSTs can also be used in dynamic scheduling systems where priorities are constantly changing.

  • Geographical Data (GIS): BSTs help in GIS systems by storing and retrieving spatial data—like finding nearby locations or filtering by a range.

  • Data Compression: Huffman encoding, a key part of data compression algorithms, uses a special kind of binary tree to assign variable-length codes to data symbols.

  • Gaming Systems: BSTs make dynamic leaderboards and scoreboards possible by keeping scores sorted and retrieving rankings in real-time.

  • Networking and Routing: Routing tables sometimes rely on BST-like structures to determine efficient paths for data transfer.

  • Version Control Systems: Systems like Git use tree-like structures (BST-inspired) to manage commits and versions efficiently within a Directed Acyclic Graph (DAG).

BSTs are everywhere, from powering the tools we use daily to solving complex computational problems. Pretty cool, right?

But it’s essential to be mindful of their limitations and edge cases. Understanding these nuances can help you design more efficient and reliable systems.

Have you encountered any interesting challenges or solutions while working with BSTs? Let’s discuss below! 🚀

Top comments (0)