A binary search tree (BST) keeps keys in order: every key in a node’s left subtree is smaller and every key in its right subtree is larger. That rule lets you find, insert or delete a key by walking down one path from the root, choosing left or right at each node. This visualizer animates that walk for insert, search and delete, and the tree rearranges itself smoothly after each change.
Deletion shows all three cases: a leaf is simply removed, a node with one child is replaced by that child, and a node with two children takes the key of its in-order successor. The traversal buttons run in-order, pre-order, post-order and level-order (breadth-first) traversals and list the keys in the order they are visited.
How to use the Binary Search Tree Visualizer tool
- Press Random tree or type keys one by one and press Insert.
- Type a key and press Search or Delete to watch the path from the root.
- Press In-order, Pre-order, Post-order or Level order to see a traversal and its output.
- Step forward and back, and follow the highlighted pseudocode line.
Why use ToolOrca Binary Search Tree Visualizer
- Insert, search and delete with animated paths
- All three deletion cases, with the in-order successor
- In-order, pre-order, post-order and level-order traversals
- Smooth layout changes after every operation
- Node count, height and nodes-visited counters
- Code in C++, Java, Python and JavaScript
How a binary search tree works
Each node has a key and up to two children. For every node, keys in the left subtree are smaller and keys in the right subtree are larger. To search, start at the root: if the key is smaller go left, if larger go right, and stop when you find it or reach an empty spot. Insert follows the same path and puts the new node in that empty spot.
Each step goes one level down, so the cost is the tree’s height. In a balanced tree with n keys the height is about log₂ n (20 levels for a million keys), giving O(log n). If keys are inserted in sorted order, though, every node goes to the right and the tree becomes a chain with height n, so operations degrade to O(n). Self-balancing trees such as AVL and red-black trees prevent this.
Deleting a node: three cases
No children: remove the leaf. One child: connect the node’s parent directly to its child. Two children: find the in-order successor, the smallest key in the right subtree (go right once, then left as far as possible). Copy its key into the node, then delete the successor, which has at most one child. Using the in-order predecessor (largest in the left subtree) works too.
Tree traversals
In-order (left, node, right) visits a BST’s keys in sorted order. Pre-order (node, left, right) is used to copy or serialize a tree. Post-order (left, right, node) visits children before parents, as needed to delete a tree or evaluate an expression tree. Level order uses a queue to visit the tree level by level, which is breadth-first search. All four take O(n) time.
Every visualizer on this site runs in your browser with the same controls: Play, Pause, Step forward, Step back and a speed slider. You can also drag the position slider to jump to any step, or focus the drawing and use the Space bar and arrow keys.
Frequently asked questions
What is a binary search tree?
A binary tree where, for every node, all keys in the left subtree are smaller and all keys in the right subtree are larger. That ordering makes search, insert and delete fast.
What is the time complexity of BST operations?
O(h), where h is the height: O(log n) for a balanced tree, but O(n) in the worst case when the tree degenerates into a chain.
Which traversal gives sorted output?
In-order traversal (left subtree, node, right subtree) visits the keys of a BST in ascending order.
What is an in-order successor?
The next larger key in the tree. For a node with a right subtree it is the leftmost node of that subtree. It is used when deleting a node with two children.
How does a BST compare to a hash table?
A hash table is faster on average (O(1)) but keeps no order. A balanced BST is O(log n) but supports sorted traversal, min, max and range queries.