← Back

Binary Search Tree Simulator

What is a Binary Search Tree?

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 visualization.

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

Compare the new value with each visited node. 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 is replaced by its inorder successor.

Traversal orders

Inorder visits 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. Sorted insertion can create a skewed tree whose 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)