How to use the Binary Search Tree Visualizer
- 1. Enter keysThey are inserted in the order you type them.
- 2. Build the treeSee the shape your insertion order creates.
- 3. Search, insert or deleteType a value and press a button to watch it happen.
The BST rule
In a binary search tree, every key in a node's left subtree is smaller than the node, and every key in its right subtree is larger. That rule lets you search like binary search: compare with the node, then go left or right.
- Search and insert follow one path from the root, so they cost O(h), where h is the height.
- Delete has three cases: a leaf is simply removed; a node with one child is replaced by that child; a node with two children takes the value of its inorder successor (the smallest key in its right subtree), which is then deleted from there.
The shape depends on insertion order. Inserting 1, 2, 3, 4, 5 creates a straight line with height 5, so searches cost O(n). That's why self-balancing trees like AVL exist.
Worked example
The sample keys 50, 30, 70, 20, 40, 60, 80 build a perfectly balanced tree of 7 nodes with height 3. Its traversals are:
- Inorder: 20, 30, 40, 50, 60, 70, 80 (always sorted for a BST).
- Preorder: 50, 30, 20, 40, 70, 60, 80.
- Postorder: 20, 40, 30, 60, 80, 70, 50.
- Level order: 50, 30, 70, 20, 40, 60, 80.
Search for 60: 60 > 50, go right; 60 < 70, go left; found after 3 comparisons, with the path 50 → 70 → 60 highlighted. Delete 50 and it is replaced by its inorder successor 60, since 50 has two children. Use Prev and Next to replay each step of a search.
Frequently asked questions
What about duplicate keys?
They are ignored, which is the simplest common rule. Some implementations count duplicates or send them to one side.
Why is my tree so tall?
Your keys were mostly in sorted order. Try shuffling them, or use the AVL Tree Visualizer, which rebalances.
Which traversal gives sorted output?
Inorder: left subtree, node, then right subtree.
Is the binary search tree visualizer free to use?
Yes, the binary search tree visualizer is free and costs nothing. Building your first tree shows a short form for your name and WhatsApp number one time; once that's done, this tool and every other Uncodemy tool open with no form.