disjset.h
#ifndef _DisjSet_H_
#define _DisjSet_H_
#define NumSets 100
typedef int DisjSet[NumSets + 1];
typedef int SetType;
typedef int ElementType;
void Initialize(DisjSet S);
void SetUnion(DisjSet S, SetType Root1, SetType Root2);
SetType Find(ElementType X, DisjSet S);
#endif
disjset.c
#include<stdio.h>
#include<stdlib.h>
#include"disjset.h"
void Initialize(DisjSet S)
{
int i;
for (i = NumSets; i > 0; i--)
S[i] = 0;
}
void SetUnion(DisjSet S, SetType Root1, SetType Root2)
{
S[Root2] = Root1;
}
SetType Find(ElementType X, DisjSet S)
{
if (S[X] <= 0)
return X;
else
return Find(S[X], S);
}
//按高度求并
void SetUnion(DisjSet S, SetType Root1, SetType Root2)
{
if (S[Root1] < S[Root2])
S[Root1] = S[Root2];
else
{
if (S[Root1] == S[Root2])
S[Root1]--;
S[Root2] = Root1;
}
}
//路径压缩
SetType Find(ElementType X, DisjSet S)
{
if (S[X] <= 0)
return X;
else
return S[X] = Find(S[X], S);
}