Ordered by left and right
In a binary search tree (BST), every node’s left subtree holds smaller values and its right subtree holds larger ones. Searching, inserting, and deleting all walk down one path — left or right — by comparing against the current node.
Deletion has three cases: a leaf, a node with one child, or a node with two children (replace it with its inorder successor). Traversals visit every node: inorder (left → root → right) yields sorted order; preorder and postorder change when the root is visited.
# For every node: # left < node.value # right > node.value def search(node, x): if node is None: return False if x == node.value: return True if x < node.value: return search(node.left, x) return search(node.right, x)
Each comparison discards one whole side of the tree.
Run the simulation
Switch tabs for insert, delete, and the three classic traversals.
Build and walk your own tree
Insert, search, and delete values, then run a traversal to see the visit order.
BST mental model
One comparison rule powers search, insert, delete, and sorted walks.
One path, left or right
Compare with the current node; go left if smaller, right if larger, until you match or hit None.
Three cases
Leaf, one child, or two children (swap with the inorder successor, then remove that leaf).
Three traversals
Inorder → sorted; preorder → root first; postorder → root last (children first).