next up previous
Next: Up: Previous:

Union by Rank

In Union, have parent of shallower tree point to other tree.

Maintain rank(x) as an upper bound on the depth of the tree rooted at x.



Make-Set(x)
$\;\;\;\;\;$parent(x) = x
$\;\;\;\;\;$rank(x) = 0



Union(x, y)
$\;\;\;\;\;$repx = FindSet(x)
$\;\;\;\;\;$repy = FindSet(y)
$\;\;\;\;\;$if rank(repx) > rank(repy)
$\;\;\;\;\;$then parent(repy) = repx
$\;\;\;\;\;$else parent(repx) = repy
$\;\;\;\;\;$ $\;\;\;\;\;$if rank(repx) = rank(repy)
$\;\;\;\;\;$ $\;\;\;\;\;$then rank(repy) = rank(repy) + 1



Can we do even better?


next up previous
Next: Up: Previous: