Binary Search Tree Simulator
Build and explore a binary search tree interactively. Insert new values, remove existing nodes, search for a value, and visualize how the tree changes. Explore in-order, pre-order, and post-order traversal sequences.
Build Your Binary Search Tree
Tree Visualization
Tree Traversal Visualizer
Select a traversal method to see the order in which nodes are visited. The sequence is displayed from left to right.
Search Path and Operation Details
When you search for a value, the simulator highlights the nodes visited while following the binary search tree rules.
| Step | Current node | Comparison | Next action |
|---|---|---|---|
| No search performed yet. | |||
How a Binary Search Tree Works
A binary search tree (BST) is a binary tree in which each node follows an ordering rule. Values smaller than a node are placed in its left subtree, while values larger than the node are placed in its right subtree.
For example, inserting 50, 30, 70, 20, 40, 60, and 80 produces this structure:
BST Operations
- Insertion: Compare the new value with each visited node and move left or right until an empty position is found.
- Search: Compare the target with the current node and follow the appropriate branch.
- Deletion: Remove a node while preserving the BST ordering property.
Three Common Traversal Methods
| Traversal | Visit order | Example result |
|---|---|---|
| In-order | Left → Root → Right | 20, 30, 40, 50, 60, 70, 80 |
| Pre-order | Root → Left → Right | 50, 30, 20, 40, 70, 60, 80 |
| Post-order | Left → Right → Root | 20, 40, 30, 60, 80, 70, 50 |
Time Complexity
The performance of a binary search tree depends on its shape. A balanced tree can provide efficient operations, while a highly skewed tree can behave similarly to a linked list.
| Operation | Average case | Worst case |
|---|---|---|
| Search | O(log n) | O(n) |
| Insertion | O(log n) | O(n) |
| Deletion | O(log n) | O(n) |
| Traversal | O(n) | O(n) |
This simulator implements a standard, unbalanced BST. It does not automatically rebalance the tree.
Frequently Asked Questions
What is a binary search tree?
A binary search tree is a data structure in which each node has at most two children. Values smaller than a node are stored to its left, and larger values are stored to its right.
How do I insert a node into the BST?
Enter an integer and click Insert Node. The simulator compares the value with existing nodes and places it in the correct empty position.
How does BST deletion work?
A node with no children can be removed directly. A node with one child is replaced by that child. A node with two children is replaced by its in-order successor, the smallest value in its right subtree.
Why does in-order traversal produce sorted values?
In-order traversal visits the left subtree, then the current node, then the right subtree. Because of the BST ordering rule, the resulting sequence is sorted in ascending order.
Does this simulator allow duplicate values?
No. Duplicate values are rejected so that each value appears only once in the tree.
Is this an AVL tree or a Red-Black tree?
No. It is a standard binary search tree. It does not automatically rotate or rebalance nodes after insertions or deletions.
What is the difference between pre-order and post-order traversal?
Pre-order visits the root before its children. Post-order visits both subtrees before visiting the root.
Related Binary and Computer Science Tools
Binary Search Simulator
Visualize binary search iterations and midpoint comparisons.
Binary String Validator
Validate binary strings and inspect their format.
Number Base Converter
Convert numbers between binary, decimal, hexadecimal, and octal.
Binary to Decimal Converter
Convert binary values into decimal numbers.
Decimal to Binary Converter
Convert decimal integers into binary representation.
Truth Table Generator
Generate truth tables for Boolean expressions.
Logic Gate Simulator
Explore digital logic gates and their outputs.