Skip to content

BST Insert and Search
Step by Step

Smaller values go left and larger values go right, so every comparison rules out a subtree.

Step forward and back at your pace. Share a link to the exact step where you got stuck.

What You Watch in Every Step

Paper tracing loses the state, and the visualiser keeps it.

Every comparison in a BST sends you left or right. That rule sounds obvious and is hard to hold in your head on step nine, where most students lose the thread.

CodeStory's visualiser keeps your search path in front of you, node by node. It highlights the line of code being executed as the picture changes. Step forward, step back or let it play.

Try an empty tree, a single value and a list with duplicates after a clean example. Clean examples teach you the shape and edge cases teach you what an interviewer asks.

How Insert and Search Move Down the Tree

Follow the same order the visualiser plays, so the list and the picture match.

  1. 1Start at the root and compare your value with that node.
  2. 2Go left when the value is smaller and right when it is larger.
  3. 3Stop on a match, or when the next child is empty.
  4. 4Insert adds a new node in that empty spot and search reports not found.
  5. 5Duplicate values are ignored, and the cost is one comparison per level of the tree.

BST Inputs Worth Trying

Every preset below teaches one edge case.

  • Balanced orderRoot first, then the middles, so three levels hold seven values.
  • Sorted inputEvery value goes right and the tree becomes a linked list, so search checks all seven.
  • Target missingWatch where the search falls off the tree, because that spot is where 45 would be inserted.
  • Target is the rootOne comparison.
  • Single valueSearch stops after one check on a tree with a single node.
  • DuplicatesSecond copies of 20 and 40 are ignored.
  • Zig-zagValues alternate low and high, so the tree stays one node per level and grows tall even though the input is not sorted.

BST Cost in Time and Space

Interviewers ask for these two numbers, so the visualiser counts them for you.

Time
O(log n) avg · O(n) worstcounted as real operations while the visualiser plays, not quoted from a table
Space
O(n)what the algorithm holds besides the input itself
Pattern
TreesSubtrees follow the same rule at every level.

Once You See It Go Use It

Watching takes ten minutes. Solving problems with the pattern makes it stick.

Everything in the App in One Place

Practice, interviews and tools all start free. Swipe to see the rest.

Sign up for 100 AI credits now and get 100 more every month. Start free in the app

BST Visualiser FAQs

Open the visualiser and watch the answers play out.

Open the visualiser
What does the BST visualiser show?

CodeStory's visualiser shows every comparison on your own input, from the root down to the node or empty spot where the walk ends. It highlights the line of code being executed, and you can step forward, step back or let it play.

What is the time complexity of BST insert and search?

BST insert and search take O(log n) time on average and O(n) in the worst case, with O(n) space. CodeStory's visualiser counts real operations as it plays, so you watch the number grow instead of trusting the formula.

Why does sorted input make a BST slow?

Sorted input sends every value to the right, so the tree becomes a linked list and a search checks all of its nodes.

What happens with duplicate values?

Duplicate values are ignored. In the duplicates preset, the second 20 and the second 40 never enter the tree.

Can I use my own input?

Yes. Type your own values and the visualiser plays insert and search on them, edge cases included.

Is the visualiser free?

Yes. You start free with 100 AI credits when you sign up and 100 every month, and watching an algorithm costs no credits.

Stuck on Trees? Get a Free Callback

Tell CodeStory where trees lose you, and a mentor plans your next steps.

  1. 1CodeStory finds your starting point, from your first node to interview ready.
  2. 2CodeStory explains online and campus batches, with timings for your mornings or evenings.
  3. 3You decide. No pressure, no payment on the call.

Get a free callback

I am a
I want to learn

Your number is only used for a CodeStory callback.