← Back

Binary Search Tree Simulator

Binary Search Tree Ordering

For every node, all values in the left subtree are smaller and all values in the right subtree are larger. This rule determines every search, insertion, deletion, predecessor, and successor path.

Balanced operationsO(log n)
Worst-case operationsO(n)
Inorder traversalSorted values
Duplicate policyRejected

BST operations

400 ms

Example tree loaded. Choose an operation.

Nodes0
Height0
Leaves0
Comparisons0

Search / operation path

Traversal result

Deletion cases

if node is a leaf:
    remove it

else if node has one child:
    replace it with that child

else:
    copy the inorder successor
    delete that successor
    from the right subtree

BST concepts

Search decisions

At each node, equality finishes the search. A smaller target moves left; a larger target moves right.

Predecessor and successor

The predecessor is the greatest smaller key. The successor is the smallest larger key. They may be found in a subtree or among ancestors.

Deletion

Deleting a node with two children uses the minimum node in its right subtree, called the inorder successor.

Degeneration

Inserting already sorted values can create a chain. The BST remains valid, but its height and operation time become linear.

Traversal orders

TraversalOrderTypical use
InorderLeft, Root, RightRead BST values in sorted order.
PreorderRoot, Left, RightSerialize or copy tree structure.
PostorderLeft, Right, RootDelete or evaluate subtrees first.
Level orderBreadth by breadthInspect the tree one level at a time.