<title>and check Set</title> and check Set
A simple and check-set implementation:
void Init(void) { for(int I= 0; I < 10; i++) {Par[i] = i; }}int Find(int I) {if(I! = Par[i]) {returnPar[i] = find (Par[i]); }Else{returnI }}void Union_connect(int I,int J) {int P= Find (i);int Q= Find (j);if(p = = q) {return; }Else{Par[q] = p; }}
With path compression and check set
int find0(int I) {// Recursive Implementation if(I! = Par[i]) {Par[i] = find0 (Par[i]);// the compression path when backtracking}// The nodes from the I-node search to the ancestor nodes point to the ancestor node . returnPar[i];}int Main() {Union_connect (1, 3); Union_connect (3, 4); Union_connect (4, 8); Union_connect (8, 9); Union_connect (5, 8);// here par[1] and Par[8] become 5, but 3 and 4 are unchanged . return0;}int Find1(int I) {// Iterative implementations int x= i;// Save the original node while(I! = Par[i]) {// Find and Nodei = par[i];// Find the node and use I to record}int k= x;// return to the original node while(k! = i) {// non-recursive path compression operations int J= Par[k];// The parent node of the J staging Par[k]Par[k] = i;// Par[i] Point and NodeK = J;// k Move to parent node}returnI// returns the value of the root node}
and check Set