Union Find Algorithm
Step by Step
Every group is a tree named by its root. Union links two roots and compression keeps the trees flat.
Move through every union at your own pace, then share a link to the step that confused you.
What You Watch While Trees Merge
Parents, roots and tree heights change on every union, and the visualiser tracks them for you.
Following a find in your head works for three elements. By step nine, parents, roots and compressed paths blur together.
CodeStory's visualiser draws every group as a tree and shows every union and find as it happens. Code lines stay highlighted beside the trees. Step forward, step back or let it play.
Try random unions with a few different seeds. Then switch on union by size and path compression and compare how tall the trees grow.
How Union Find Merges and Finds
Steps below match the picture, so you can follow them beside the trees.
- 1Start with every element as its own group and its own root.
- 2Find an element by following parent links up to a root.
- 3Union two elements by finding both roots and linking one root under the other.
- 4With union by size, hang the smaller tree under the larger.
- 5With path compression, point every node on the find path straight at the root.
Union by Size and Path Compression
Flip the switch in the visualiser, replay the same unions and watch the trees stay flat.
- Union by size + path compressionHangs the smaller tree under the larger and points every node on a find path straight at the root. Trees stay flat, so finds take almost no steps.
Union Find Inputs Worth Trying
One preset gives you fresh unions to study.
- Random unionsChange the seed for a different set of unions.
Union Find Cost in Time and Space
Interviewers ask why Union Find is nearly constant, and the visualiser counts real operations.
- Time
- O(a(n)) with compressioncounted from real finds and unions on your input
- Space
- O(n)extra memory for one parent link per element
- Pattern
- GraphsConnected groups merge into one tree, root by root.
Put Union Find to Work
Watching builds the picture and problems test it.
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
Union Find Visualiser FAQs
Open the visualiser and watch the answers play out.
Open the visualiserWhat does the Union Find visualiser show?
CodeStory's visualiser shows every group as a tree and plays every union and find step by step on your own input. 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 Union Find?
Union Find takes O(a(n)) time with path compression, where a(n) is the inverse Ackermann function, and O(n) space. CodeStory's visualiser counts real operations as it plays, so you watch the numbers instead of trusting the formula.
What does path compression do?
Path compression points every node on a find path straight at the root, so later finds take almost no steps.
What is union by size?
Union by size hangs the smaller tree under the larger one, which keeps trees flat.
Can I use my own input?
Yes. Type your own elements and unions and the visualiser plays 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 Union Find? Get a Free Callback
Tell CodeStory where Union Find loses you, and a mentor plans your next steps.
- 1CodeStory finds your starting point, from your first group 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.