@@ -114,33 +114,33 @@ public String toString() {
114
114
// CONSTRUCTION: with int representing initial number of sets
115
115
//
116
116
// ******************PUBLIC OPERATIONS*********************
117
- // void union(root1, root2) --> Merge two sets
118
- // int find( x ) --> Return set containing x
117
+ // void union(root1, root2) --> Merge two sets
118
+ // int find(x ) --> Return set containing x
119
119
// ******************ERRORS********************************
120
120
// No error checking is performed
121
121
// http://users.cis.fiu.edu/~weiss/dsaajava3/code/DisjSets.java
122
122
123
123
/**
124
- * Disjoint set class, using union by rank and path compression.
125
- * Elements in the set are numbered starting at 0.
124
+ * Disjoint set class, using union by rank and path compression
125
+ * Elements in the set are numbered starting at 0
126
126
* @author Mark Allen Weiss
127
127
*/
128
128
class DisjointSet {
129
- private int [] s ; //the set field
130
-
129
+ private int [] set ; //the disjoint set as an array
131
130
132
131
public int [] getSet (){ //mostly debugging method to print array
133
- return s ;
132
+ return set ;
134
133
}
135
134
136
135
/**
137
136
* Construct the disjoint sets object.
138
137
* @param numElements the initial number of disjoint sets.
139
138
*/
140
- public DisjointSet ( int numElements ) { //constructor creates singleton sets
141
- s = new int [ numElements ];
142
- for ( int i = 0 ; i < s .length ; i ++ ) //initialize to -1 so the trees have nothing in them
143
- s [ i ] = -1 ;
139
+ public DisjointSet (int numElements ) { //constructor creates singleton sets
140
+ set = new int [numElements ];
141
+ for (int i = 0 ; i < set .length ; i ++){ //initialize to -1 so the trees have nothing in them
142
+ set [i ] = -1 ;
143
+ }
144
144
}
145
145
146
146
/**
@@ -150,13 +150,15 @@ public DisjointSet( int numElements ) { //constructor creates singleton sets
150
150
* @param root1 the root of set 1.
151
151
* @param root2 the root of set 2.
152
152
*/
153
- public void union ( int root1 , int root2 ) {
154
- if ( s [ root2 ] < s [ root1 ] ) // root2 is deeper
155
- s [ root1 ] = root2 ; // Make root2 new root
153
+ public void union (int root1 , int root2 ) {
154
+ if (set [root2 ] < set [root1 ]){ // root2 is deeper
155
+ set [root1 ] = root2 ; // Make root2 new root
156
+ }
156
157
else {
157
- if ( s [ root1 ] == s [ root2 ] )
158
- s [ root1 ]--; // Update height if same
159
- s [ root2 ] = root1 ; // Make root1 new root
158
+ if (set [root1 ] == set [root2 ]){
159
+ set [root1 ]--; // Update height if same
160
+ }
161
+ set [root2 ] = root1 ; // Make root1 new root
160
162
}
161
163
}
162
164
@@ -166,10 +168,12 @@ public void union( int root1, int root2 ) {
166
168
* @param x the element being searched for.
167
169
* @return the set containing x.
168
170
*/
169
- public int find (int x ) {
170
- if (s [ x ] < 0 ) //if tree has no elements, then it is its own root
171
+ public int find (int x ) {
172
+ if (set [ x ] < 0 ){ //if tree has no elements, then it is its own root
171
173
return x ;
172
- else
173
- return s [ x ] = find ( s [ x ] );
174
+ }
175
+ else { //Recursively find the parent of
176
+ return set [x ] = find (set [x ]);
177
+ }
174
178
}
175
179
}
0 commit comments