Binary Search Tree Simulator

For every node, all values in the left subtree are smaller and all values in the right subtree are larger. This ordering rule determines search, insertion, deletion, predecessor, successor, minimum, maximum, and sorted traversal behavior.

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

leaf:
  remove it

one child:
  replace it with that child

two children:
  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 sorted values can create a chain. The BST stays valid, but 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.