集合的表示
集合运算
交、并、补、差,判定一个元素是否属于某一集合
并查集
集合并、查元素属于什么集合
集合运算
查找某个元素所在的集合
int Find( SetType S[], ElementType X)
{
int i;
for(i=0;i < MaxSize && S[i].Data != X;i++);
if( i >= MxaSize)
return -1;
for(;S[i].Parent >= 0; i = S[i]Parent);
return i;
}
集合的并运算
分别找到X1和X2两个元素所在集合树的根结点
如果他们不同跟,则将其中一个根结点的父结点指针设置成另一个根结点的数组下标。
void Union( SetType S[], ElementType X1, ElementType X2)
{
int Root1, Root2;
Root1 = Find(s, X1);
Root2 = Find(s, X2);
if( Root1 != Root2)
s[Root2].Parent = Root1;
}