1
1
package src .dataStructures .disjointSet .quickFind .generalised ;
2
+
2
3
import java .util .ArrayList ;
3
4
import java .util .List ;
4
5
5
6
/**
6
- * Implementation of quick-find algorithm ; Turns a list of objects into a data structure that supports union operations
7
+ * Implementation of quick-find structure ; Turns a list of objects into a data structure that supports union operations
7
8
* @param <T> generic type of object to be stored
8
9
*/
9
10
public class QuickFind <T > {
@@ -28,8 +29,8 @@ public QuickFind(List<T> input) {
28
29
* @param item to be added
29
30
*/
30
31
public void add (T item ) {
31
- this . objects .add (item );
32
- this . identity .add (this . identity .size ()); // identity of the new item
32
+ objects .add (item );
33
+ identity .add (identity .size ()); // identity of the new item
33
34
}
34
35
35
36
/**
@@ -38,19 +39,19 @@ public void add(T item) {
38
39
* @param objTwo object in another component
39
40
*/
40
41
public void union (T objOne , T objTwo ) {
41
- if (!this . objects .contains (objOne ) || !this . objects .contains (objTwo )) {
42
+ if (!objects .contains (objOne ) || !objects .contains (objTwo )) {
42
43
System .out .println ("One or more of the objects do not exist!" );
43
44
return ;
44
45
}
45
- int idxOne = this . objects .indexOf (objOne );
46
- int compOne = this . identity .get (idxOne );
47
- int idxTwo = this . objects .indexOf (objTwo );
48
- int compTwo = this . identity .get (idxTwo );
46
+ int idxOne = objects .indexOf (objOne );
47
+ int compOne = identity .get (idxOne );
48
+ int idxTwo = objects .indexOf (objTwo );
49
+ int compTwo = identity .get (idxTwo );
49
50
50
51
int size = this .objects .size ();
51
52
for (int i = 0 ; i < size ; i ++) {
52
- if (this . identity .get (i ) == compOne ) {
53
- this . identity .set (i , compTwo );
53
+ if (identity .get (i ) == compOne ) {
54
+ identity .set (i , compTwo );
54
55
}
55
56
}
56
57
}
@@ -61,13 +62,12 @@ public void union(T objOne, T objTwo) {
61
62
* @return list of elements.
62
63
*/
63
64
public List <T > retrieveComponent (T objOne ) {
64
- int idx = this .objects .indexOf (objOne );
65
- int comp = this .identity .get (idx );
66
- int size = this .objects .size ();
65
+ int idx = objects .indexOf (objOne );
66
+ int comp = identity .get (idx );
67
67
List <T > ret = new ArrayList <>();
68
- for (int i = 0 ; i < size ; i ++) {
69
- if (this . identity .get (i ) == comp ) {
70
- ret .add (this . objects .get (i ));
68
+ for (int i = 0 ; i < objects . size () ; i ++) {
69
+ if (identity .get (i ) == comp ) {
70
+ ret .add (objects .get (i ));
71
71
}
72
72
}
73
73
return ret ;
0 commit comments