Algorithms · interactive · step by step
Binary Search Tree Visualizer
Insert, delete and search in a binary search tree, an AVL tree that balances itself with animated rotations, or a min-heap, and watch in-order, pre-order, post-order and level-order traversals.
The tree is empty. Insert a number to start.
Step 0Space play · ←→ step
Reference
Time complexity of tree operations
| Structure | Search | Insert | Delete | Find min |
|---|---|---|---|---|
| BST (average) | O(log n) | O(log n) | O(log n) | O(log n) |
| BST (worst, sorted input) | O(n) | O(n) | O(n) | O(n) |
| AVL tree | O(log n) | O(log n) | O(log n) | O(log n) |
| Min-heap | O(n) | O(log n) | O(log n) (extract min) | O(1) |
Questions
Binary Search Tree Visualizer FAQ
Free, private and works offline: everything runs in your browser.
What is a binary search tree?
A binary tree where every node’s left subtree holds smaller keys and its right subtree holds larger keys. Searching, inserting and deleting follow one path from the root, so they take time proportional to the tree’s height: O(log n) when it is balanced.
What is the difference between in-order, pre-order and post-order traversal?
They visit the node before (pre), between (in) or after (post) its two subtrees. For the tree built from 50, 30, 70, 20, 40, 60, 80: in-order gives 20, 30, 40, 50, 60, 70, 80 (always sorted for a BST), pre-order 50, 30, 20, 40, 70, 60, 80, post-order 20, 40, 30, 60, 80, 70, 50, and level-order 50, 30, 70, 20, 40, 60, 80.
How do you delete a node from a BST?
A leaf is simply removed. A node with one child is replaced by that child. A node with two children takes the key of its in-order successor (the smallest key in its right subtree), and then the successor, which has at most one child, is deleted instead.
What is an AVL tree and why does it rotate?
An AVL tree is a BST that keeps every node balanced: the heights of its two subtrees differ by at most 1. When an insert or delete breaks that, it rotates: a single rotation for the left-left and right-right cases, a double rotation for left-right and right-left. This keeps the height O(log n), so a sorted sequence of inserts doesn’t turn the tree into a slow chain. Press “Insert 10…70 in order” with BST and then with AVL to compare.
What is a min-heap?
A complete binary tree stored in an array where every parent is no bigger than its children, so the smallest item is always at the root (index 0). The children of index i are at 2i + 1 and 2i + 2. Insert adds at the end and sifts up; extract-min moves the last item to the top and sifts down. Both take O(log n), which makes heaps ideal for priority queues.
What is the time complexity of BST operations?
Search, insert and delete are O(h), where h is the height. For a balanced tree (like AVL) h is O(log n); for a plain BST built from sorted keys h can be n, making operations O(n). Traversals visit every node, so they are O(n).