GATE Computer ScienceModerateGATE CS (PYQ pattern)

Union by rank without path compression is performed (ties: the root of the second argument is attached below the root of the first) on elements 0–9 in this order: union(0,1), union(2,3), union(0,2), union(4,5), union(6,7), union(4,6), union(0,4), union(8,9), union(8,0). What is the largest depth (distance to the root) of any element?

  1. A. 2
  2. B. 4
  3. C. 5
  4. D. 3

Correct answer: D. 3

The two 8-element trees built from {0..3} and {4..7} have rank 2; joining them gives a rank-3 tree rooted at 0 in which element 7 has depth 3. The final union attaches the rank-1 tree {8,9} below the root, so no node gets deeper than 3. Rank bounds the height by log₂ n.

More Algorithms questions

Exams that ask this topic