AiTechWorlds
AiTechWorlds
Insert, delete, and search in a BST with animated paths and inorder/preorder/postorder/level-order traversals.
def insert(root, val):
if root is None:
return Node(val)
if val < root.val:
root.left = insert(root.left, val)
elif val > root.val:
root.right = insert(root.right, val)
return rootA Binary Search Tree keeps smaller values in the left subtree and larger values in the right subtree of each node. This ordering gives average O(log n) search, insert, and delete, but degrades to O(n) if the tree becomes unbalanced.
| Search / Insert / Delete (avg) | O(log n) |
| Worst case | O(n) |
| Space | O(n) |
The Binary Search Tree Visualizer lets you insert, delete, and search values in a BST and watch the tree update live. A binary search tree keeps smaller values in the left subtree and larger values in the right subtree of every node, giving average O(log n) search, insert, and delete — though it degrades to O(n) if it becomes unbalanced. It also animates inorder, preorder, postorder, and level-order traversals.
Insert values
Type a number and click Insert, or click Random to build a sample tree.
Search or delete
Type a value and click Search to animate the path, or Delete to remove it.
Run a traversal
Click Inorder, Preorder, Postorder, or Level-order to animate the visiting sequence.
Read the code
See the BST insert code in C++, Java, Python, or JavaScript and copy it.
100% Private — No Server Required
All processing happens directly in your browser. No data is uploaded, stored, or transmitted to any server.
Last reviewed on June 14, 2026 by the AiTechWorlds Tools Team. All processing runs locally in your browser.