Binary Search Tree Simulator

A Binary Search Tree (BST) is a binary tree in which every value in a node's left subtree is smaller and every value in its right subtree is larger. This ordering supports efficient search, insertion, and deletion when the tree remains reasonably balanced.

Balanced searchO(log n)
Worst-case searchO(n)
Inorder traversalSorted order
Traversal costO(n)

Tree operations

400 ms
Binary search treeInteractive binary search tree.

Example tree loaded. Choose an operation.

Nodes0
Height0
Leaves0
Last path length0

Operation path

—

Traversal result

—

BST decision rule

search(node, value):
  if node is null:
    return not found
  if value == node.value:
    return node
  if value < node.value:
    return search(node.left, value)
  return search(node.right, value)

Binary search tree concepts

Insertion

Move left for a smaller value and right for a larger value until an empty child position is found.

Deletion cases

A leaf is removed directly. A node with one child is replaced by that child. A node with two children uses its inorder successor.

Traversal orders

Inorder is Left–Root–Right, preorder Root–Left–Right, postorder Left–Right–Root, and level order visits breadth by breadth.

Tree shape matters

A balanced BST has logarithmic height, while a skewed tree can make operations degrade to O(n).

Complexity summary

OperationBalancedWorst case
SearchO(log n)O(n)
InsertO(log n)O(n)
DeleteO(log n)O(n)
TraversalO(n)O(n)
SpaceO(n)O(n)