Binary Search Tree Visualizer
The left subtree contains smaller values, and the right subtree contains greater values than the root. Insert, delete, edit and search with step-by-step animation.
How to use the Binary Search Tree Visualizer on KickSolve
Three quick steps, no account needed.
Why choose KickSolve
Built for learning, speed and privacy.
What you can do with the Binary Search Tree Visualizer
Everything this tool offers.
About the Binary Search Tree Visualizer on KickSolve
What it does and how to get the best results.
What is a binary search tree?
A binary search tree (BST) is a binary tree in which every node follows one ordering rule: all values in its left subtree are smaller, and all values in its right subtree are greater. This rule lets a search skip half of the remaining tree at every step.
Key properties
- Left subtree holds smaller values
- Right subtree holds greater values
- The rule holds for every node, not just the root
- Inorder traversal gives sorted output
Operations and time complexity
Search, insert and delete take O(log n) on average and O(n) in the worst case, when sorted input turns the tree into a chain.
Real-world uses
- Fast lookup and sorted storage
- Ordered sets and maps
- Range queries
- Base for AVL and red-black trees
Comparing tree shapes? Try the Binary Tree, Balanced Binary Tree or Skewed Tree visualizer.
Worked example: building a small binary search tree
The same steps the visualizer animates for any input.
Insert 50: it becomes the root. Insert 30: smaller than 50, so it goes left. Insert 70: greater than 50, so it goes right. Insert 20, 40, 60, 80: each walks left or right from the root until it finds an empty spot. Inorder: 20 30 40 50 60 70 80 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 Delete 30, which has two children: 40, the smallest value in its right subtree, takes its place. Inorder: 20 40 50 60 70 80
Insert 1, 2, 3, 4 in sorted order instead and the tree becomes a skewed chain.
Binary Search Tree traversals compared
When each node is visited relative to its children.
| Traversal | Order | Typical use |
|---|---|---|
| Preorder | Root, left, right | Copying a tree, prefix expressions |
| Inorder | Left, root, right | Sorted output from a binary search tree |
| Postorder | Left, right, root | Deleting a tree, postfix expressions |
| Level order | Level by level, left to right | Shortest path, printing by depth |
BST operations and time complexity
Cost depends on the shape of the tree.
| Operation | Average (balanced) | Worst case (skewed) |
|---|---|---|
| Search | O(log n) | O(n) |
| Insert | O(log n) | O(n) |
| Delete | O(log n) | O(n) |
| Minimum or maximum | O(log n) | O(n) |
| Inorder traversal | O(n) | O(n) |
BST height: best vs worst case
A balanced BST stays short, a skewed one does not.
| Nodes | Best height (balanced) | Worst height (skewed) |
|---|---|---|
| 1 | 1 | 1 |
| 3 | 2 | 3 |
| 7 | 3 | 7 |
| 15 | 4 | 15 |
| 31 | 5 | 31 |
| 63 | 6 | 63 |
| 127 | 7 | 127 |
| 255 | 8 | 255 |
| 511 | 9 | 511 |
| 1023 | 10 | 1023 |
Binary Search Tree FAQ
Quick answers.
What is a binary search tree?
A binary tree in which, for every node, all values in the left subtree are smaller and all values in the right subtree are greater than the node's value.
Why does inorder traversal give sorted output?
Inorder visits the left subtree, then the node, then the right subtree. Because smaller values are always on the left, the values come out in ascending order.
What is the time complexity of a binary search tree?
Search, insert and delete take O(log n) on average when the tree is balanced and O(n) in the worst case, when it is skewed into a chain.
How is a node deleted 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 smallest value from its right subtree, and that smaller node is then removed.
Does a binary search tree allow duplicate values?
Definitions vary. This visualizer rejects duplicates so the left and right rules stay strict.
What is the difference between a binary tree and a binary search tree?
A binary tree only limits each node to two children. A binary search tree adds the ordering rule, so a search can ignore half of the remaining tree at each step.
When does a BST become skewed?
When values arrive in sorted order. Inserting 1, 2, 3, 4 gives a right skewed chain with height 4, and searching it costs O(n).
How does the visualizer search and insert?
It starts at the root and compares the value with each node. Smaller goes left and larger goes right, until the value is found or an empty spot is reached. The path is highlighted step by step.
How do I edit a node in the visualizer?
Type the existing node in Target and the new value in Value, then press Edit. The old value is deleted and the new one is inserted, so the tree stays ordered.
What is the difference between a BST and an AVL tree?
An AVL tree is a BST that rotates nodes after every insert and delete to keep the height small. A plain BST has no rebalancing, so it can degrade into a chain.
Can I undo a change?
Yes. Press Undo to step back through your last 40 changes. Clear and Random can also be undone.
Is the visualizer free and private?
Yes. It is free with no signup, and everything runs in your browser. Nothing you type is sent to a server.
More free tools from KickSolve
Other tree visualizers.