As far as my understanding goes in the quick-union algorithm when a pair is to be processed we first do the FIND operation and check if the roots of the trees in which these objects are present are equal or not. In the case they are not equal we perform the UNION operation by linking the 2 different roots.
In Sedgewick pg-15 property:1.2-"Suppose that the input pairs come in the order 1-2, then 2-3, then 3-4 and so forth. After N-1 such pairs, we have N objects all in the same set, and the tree that is formed by the quick-union algorithm is a straight line, with N pointing to N-1, which points to N - 2,which points to N - 3, and so forth."
According to me the tree formed has the root 1 and all other objects from 2 to N are its children-when we scan 1-2,there roots are themselves so we link them,when we scan 2-3,the root of 2 is 1 the root of 3 is 3 itself so we link 1 and 3 and not 2 and 3.
How can the tree be a straight line in this instance?
#include <stdio.h>
#define N 10000
main()
{ int i, p, q, t, id[NJ;
for (i = 0; i < N; i++) id[i] = i;
while (scanfC"%d %d\n", &p, &q)==2)
{
for(i=p;i!=id[i];i=id[i]);
for(j=q;j!=id[j];j=id[j]);
if(i==j) continue;
id[i]=j;
printf("%d%d\n",p,q);
}
}