Skip to content

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.

  1. 1Start with every element as its own group and its own root.
  2. 2Find an element by following parent links up to a root.
  3. 3Union two elements by finding both roots and linking one root under the other.
  4. 4With union by size, hang the smaller tree under the larger.
  5. 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.

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

Union Find Visualiser FAQs

Open the visualiser and watch the answers play out.

Open the visualiser
What 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.

  1. 1CodeStory finds your starting point, from your first group 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.