I'm trying to do this exercise in Introduction to Algorithms by Cormen et al that has to do with the Disjoin Set data structure:
Suppose that we wish to add the operation
PRINT-SET(x)
, which is given a nodex
and prints all the members ofx
's set, in any order. Show how we can add just a single attribute to each node in a disjoint-set forest so thatPRINT-SET(x)
takes time linear in the number of members ofx
's set, and the asymptotic running times of the other operations are unchanged. Assume that we can print each member of the set in O(1) time.
Now, I'm quite sure that the attribute needed is a tail pointer, so it can keep track of the children.
Since the disjoint set structure already has a parent attribute, find-set(x)
can easily print out nodes going in one direction. But now, having a tail pointer, let's us go the other direction as well.
However, I'm not sure how I would write the algorithm to do this. If anyone could help me out in pseudocode, that would be much appreciated.