BST Ready
nodes 7
height 2
root 50
  • Current
  • Path
  • Visited
  • Found / new
  • Removing

Ready

Comparisons 0 Shifts 0
Ready

Keyboard: ← → step · Space play / pause · R reset · Esc exit classroom mode

How it works

Every operation, in plain steps

These are exactly the steps the visualizer animates.

What is a tree node?

Like a linked list node, but with two pointers: left and right. The top node is the root, nodes without children are leaves. In a binary SEARCH tree, everything in a node's left subtree is smaller and everything in its right subtree is larger. Height = the number of edges on the longest path from the root to a leaf.

struct Node {
    int data;
    struct Node *left;    // smaller values
    struct Node *right;   // larger values
};

struct Node *root = NULL;   // empty tree

Search

  1. Start at the root.
  2. Equal? Found it.
  3. Smaller? Go left. Larger? Go right.
  4. Reaching NULL means the value is not there.

Insert

  1. Walk down exactly like a search.
  2. When the next step is NULL, stop there.
  3. Create the node and link it in that spot.
  4. A new value always becomes a leaf.

Delete

  1. Leaf: just remove it.
  2. One child: link the parent straight to that child.
  3. Two children: copy in the inorder successor…
  4. …(smallest value on the right), then delete that successor.

Depth-first traversals

  1. Inorder: left → node → right (sorted order!).
  2. Preorder: node → left → right.
  3. Postorder: left → right → node.
  4. Recursion keeps a call stack of the nodes in progress.

Level order (BFS)

  1. Put the root in a queue.
  2. Take the front node and visit it.
  3. Add its children at the rear.
  4. Repeat until the queue is empty.

Balanced vs skewed

  1. Speed depends on the height h, not on n.
  2. Balanced: h ≈ log₂ n (15 nodes → 4 levels).
  3. Sorted input makes a skewed tree: h = n − 1.
  4. Self-balancing trees (AVL, Red-Black) keep h small.
Algorithm

Pseudocode

Language-independent steps. The visualizer shows the same logic in C, Python and Java.

1 SEARCH(node, key)
2 while node ≠ NULL
3 if key = node.data then return node
4 if key < node.data then node ← node.left
5 else node ← node.right
6 return NULL
Complexity

How fast is each operation?

n = number of nodes, h = height. A balanced tree has h ≈ log n; a skewed tree has h = n − 1. Recursive traversals use O(h) stack space.

Operation Best Average Worst Speed
Search O(1) O(log n) O(n) Grows with n
Insert O(1) O(log n) O(n) Grows with n
Delete O(1) O(log n) O(n) Grows with n
Min / Max O(1) O(log n) O(n) Grows with n
Any traversal O(n) O(n) O(n) Grows with n

The four traversals of 50, 30, 20, 40, 70, 60, 80

Traversal Order Result Used for
Inorder left → node → right 20 30 40 50 60 70 80 Values in sorted order
Preorder node → left → right 50 30 20 40 70 60 80 Copying / saving a tree
Postorder left → right → node 20 40 30 60 80 70 50 Deleting a tree, evaluating expressions
Level order level by level (queue) 50 30 70 20 40 60 80 Shortest paths, printing by level

Searching: sorted array vs BST

Sorted array BST (balanced) BST (skewed)
Search O(log n) — binary search O(log n) O(n)
Insert O(n) — shift elements O(log n) O(n)
Delete O(n) — shift elements O(log n) O(n)
Memory Values only Values + 2 pointers per node Values + 2 pointers per node

Try the "Skewed" button in the visualizer: sorted input turns the tree into a line.

Practice

Check your understanding

Answer each question — you will see the explanation straight away.

0 / 6 correct
Question 1

In a BST, where are all the values smaller than the root?

Question 2

Which traversal prints the values of a BST in sorted order?

Question 3

You insert 10, 20, 30, 40, 50 into an empty BST in that order. What does the tree look like?

Question 4

You delete a node that has two children. What replaces it?

Question 5

Which data structure does level order traversal use?

Question 6

A balanced BST has 1,000,000 nodes. About how many comparisons does a search need at most?

Assessment

Trees quiz

Test yourself: 5 questions, 70% to pass. Create a free account to take the quiz, save your progress and work towards the certificate.