Your algorithm canvas is empty. Insert a value to start.

Nodes Added

0

Tree Height

0

Last Algorithm Action

Initial Grid

How a Binary Search Tree (BST) Works

A Binary Search Tree (BST) is a hierarchical node-based data structure designed for fast lookup, insertion, and deletion of ordered keys. Every node stores a value and at most two children—a left child and a right child—while maintaining the strict BST ordering invariant: all values in the left subtree are smaller than the parent node, and all values in the right subtree are greater than the parent node.

Insert, Search, and Delete Operations

When you insert or search a value in this visualizer, the traversal begins at the root node and compares the target key against the current node. If the target is smaller, traversal branches left; if larger, it branches right. Each comparison eliminates half of the remaining subtrees in a balanced tree. When deleting a node, three cases are handled automatically: removing a leaf node directly, bypassing a single-child node, or replacing a two-child node with its in-order successor (the minimum key in its right subtree) to preserve the BST property.

Time & Space Complexity Analysis

On a balanced Binary Search Tree, search, insertion, and deletion run in O(log n) average time complexity with O(n) space complexity. However, inserting already sorted numbers in ascending or descending order creates a degenerate (skewed) tree of height n, degrading operations to O(n) worst-case time. Self-balancing binary search tree variants such as AVL trees and Red-Black trees solve this by performing local tree rotations whenever subtree heights diverge. Use the Random Grid preset above to observe balanced tree depths versus sequential insertions, or click any node to inspect how subtrees reorganize after deletion.

Frequently Asked Questions

How do I visualize Binary Search Tree insertion, search, and deletion online?

Type any integer into the input box above and click Insert, Search, or Delete (or click any existing node on the tree to delete it). You can also click Random Tree to generate a balanced sample BST instantly.

Why is a Binary Search Tree O(log n) on average but O(n) in the worst case?

In a balanced BST, each comparison discards half of the remaining nodes, giving O(log n) height and lookup time. If numbers are inserted in strictly ascending or descending order, the tree becomes a skewed chain of height n, degrading search, insert, and delete to O(n).

How does BST deletion work when a node has two children?

When deleting a node with both left and right children, the visualizer finds its in-order successor (the smallest value in its right subtree), copies that value into the deleted node's position, and removes the successor node so the BST ordering rule stays valid.

Is this online BST simulator free to use?

Yes. This Binary Search Tree visualizer runs 100% in your browser using D3.js with no sign-up, downloads, or usage limits.