Binary Search Tree (BST)
A hierarchical data structure where each node has at most two children, maintaining the property that all values in the left subtree are less than the node's value, and all values in the right subtree are greater. This ordering property enables efficient O(log n) operations on average for search, insert, and delete. BSTs form the foundation for many advanced tree structures and are fundamental in computer science.
Visualization
Interactive visualization for Binary Search Tree (BST)
Interactive visualization with step-by-step execution
Implementation
1class BSTNode {
2 val: number;
3 left: BSTNode | null = null;
4 right: BSTNode | null = null;
5
6 constructor(val: number) {
7 this.val = val;
8 }
9}
10
11class BST {
12 root: BSTNode | null = null;
13
14 insert(val: number): void {
15 this.root = this.insertNode(this.root, val);
16 }
17
18 private insertNode(node: BSTNode | null, val: number): BSTNode {
19 if (!node) return new BSTNode(val);
20
21 if (val < node.val) node.left = this.insertNode(node.left, val);
22 else if (val > node.val) node.right = this.insertNode(node.right, val);
23
24 return node;
25 }
26
27 search(val: number): boolean {
28 return this.searchNode(this.root, val);
29 }
30
31 private searchNode(node: BSTNode | null, val: number): boolean {
32 if (!node) return false;
33 if (node.val === val) return true;
34 return val < node.val ? this.searchNode(node.left, val) : this.searchNode(node.right, val);
35 }
36}Deep Dive
Theoretical Foundation
Binary Search Tree maintains the BST property at every node: all values in left subtree < node value < all values in right subtree. This recursive property allows binary search-like behavior, dividing the search space in half at each step. The tree is built by starting with a root and recursively inserting new nodes in the correct position to maintain the BST property.
Complexity
Time
O(log n)
O(log n)
O(n)
Space
O(n)
Applications
Industry Use
Database indexing (B-trees are BST variants)
File systems (directory structures)
Compiler symbol tables
Router tables in networking
Autocomplete features
Range queries in databases
Priority queue implementations
Use Cases
Related Algorithms
Stack
LIFO (Last-In-First-Out) data structure with O(1) push/pop operations. Stack is a fundamental linear data structure where elements are added and removed from the same end (top). It's essential for function calls, expression evaluation, backtracking algorithms, and undo operations in applications.
Queue
FIFO (First-In-First-Out) data structure with O(1) enqueue/dequeue operations. Queue is a fundamental linear data structure where elements are added at one end (rear) and removed from the other end (front). Essential for breadth-first search, task scheduling, and buffering systems.
Hash Table (Hash Map)
A data structure that implements an associative array abstract data type, mapping keys to values using a hash function. Hash tables provide O(1) average-case time complexity for insertions, deletions, and lookups, making them one of the most efficient data structures for key-value storage. The hash function computes an index into an array of buckets from which the desired value can be found.
Heap (Priority Queue)
A complete binary tree data structure that satisfies the heap property: in a max heap, parent nodes are greater than or equal to children; in a min heap, parents are less than or equal to children. Heaps provide O(1) access to the maximum/minimum element and O(log n) insertion and deletion. They're typically implemented as arrays for efficiency and are the foundation of heap sort and priority queues.