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.
- 1Start at the root and compare your value with that node.
- 2Go left when the value is smaller and right when it is larger.
- 3Stop on a match, or when the next child is empty.
- 4Insert adds a new node in that empty spot and search reports not found.
- 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.
DSA practice
Problems filed under the pattern that solves them, not a sheet.
352 problemsOpenCompany sets
Practise what one company asks in the week before your round.
463 companiesOpenCoding interview
Choose a role, write in a real editor and answer while you type.
scored reportOpenMock interviews, spoken
You answer out loud and CodeStory follows up on what you said.
32 topicsOpenDatabase round
Answer DBMS the way a panel asks it, with indexes, joins and normal forms.
in Core CSOpenOOP round
Inheritance, polymorphism and the follow-up you never rehearse.
in Core CSOpenSystem design
How real systems are built, from the primitives to the AI stack.
27 chaptersOpenSQL practice
Real schemas, your query, the rows it returns and a clear verdict.
39 questionsOpenAptitude
Quant, logical, verbal, DI and technical, timed like the real test.
280 questionsOpenOnline assessment
Get a clock, marks per question and hidden tests like a company's judge.
37 papersOpenAlgorithm visualiser
Watch it play on your own input, one step at a time.
26 algorithmsOpenAI that helps, not solves
Hints one at a time and AI review before a mentor sees your work.
100 free creditsOpenResume builder
Build it inside the app, next to the work you practised.
in the appOpenProgress and readiness
One score and the axis where you are weakest.
tracked dailyOpen
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 visualiserWhat 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.
- 1CodeStory finds your starting point, from your first node to interview ready.
- 2CodeStory explains online and campus batches, with timings for your mornings or evenings.
- 3You decide. No pressure, no payment on the call.