11package com .thealgorithms .datastructures .disjointsetunion ;
22
3+ import static org .junit .jupiter .api .Assertions .assertEquals ;
4+ import static org .junit .jupiter .api .Assertions .assertNotEquals ;
35import static org .junit .jupiter .api .Assertions .assertNotNull ;
46
5- import org .junit .jupiter .api .Assertions ;
67import org .junit .jupiter .api .Test ;
78
89public class DisjointSetUnionTest {
@@ -12,7 +13,7 @@ public void testMakeSet() {
1213 DisjointSetUnion <Integer > dsu = new DisjointSetUnion <>();
1314 Node <Integer > node = dsu .makeSet (1 );
1415 assertNotNull (node );
15- Assertions . assertEquals (node , node .parent );
16+ assertEquals (node , node .parent );
1617 }
1718
1819 @ Test
@@ -33,19 +34,92 @@ public void testUnionFindSet() {
3334 Node <Integer > root3 = dsu .findSet (node3 );
3435 Node <Integer > root4 = dsu .findSet (node4 );
3536
36- Assertions . assertEquals (node1 , node1 .parent );
37- Assertions . assertEquals (node1 , node2 .parent );
38- Assertions . assertEquals (node1 , node3 .parent );
39- Assertions . assertEquals (node1 , node4 .parent );
37+ assertEquals (node1 , node1 .parent );
38+ assertEquals (node1 , node2 .parent );
39+ assertEquals (node1 , node3 .parent );
40+ assertEquals (node1 , node4 .parent );
4041
41- Assertions . assertEquals (node1 , root1 );
42- Assertions . assertEquals (node1 , root2 );
43- Assertions . assertEquals (node1 , root3 );
44- Assertions . assertEquals (node1 , root4 );
42+ assertEquals (node1 , root1 );
43+ assertEquals (node1 , root2 );
44+ assertEquals (node1 , root3 );
45+ assertEquals (node1 , root4 );
4546
46- Assertions .assertEquals (1 , node1 .rank );
47- Assertions .assertEquals (0 , node2 .rank );
48- Assertions .assertEquals (0 , node3 .rank );
49- Assertions .assertEquals (0 , node4 .rank );
47+ assertEquals (1 , node1 .rank );
48+ assertEquals (0 , node2 .rank );
49+ assertEquals (0 , node3 .rank );
50+ assertEquals (0 , node4 .rank );
51+ }
52+
53+ @ Test
54+ public void testFindSetOnSingleNode () {
55+ DisjointSetUnion <String > dsu = new DisjointSetUnion <>();
56+ Node <String > node = dsu .makeSet ("A" );
57+ assertEquals (node , dsu .findSet (node ));
58+ }
59+
60+ @ Test
61+ public void testUnionAlreadyConnectedNodes () {
62+ DisjointSetUnion <Integer > dsu = new DisjointSetUnion <>();
63+ Node <Integer > node1 = dsu .makeSet (1 );
64+ Node <Integer > node2 = dsu .makeSet (2 );
65+ Node <Integer > node3 = dsu .makeSet (3 );
66+
67+ dsu .unionSets (node1 , node2 );
68+ dsu .unionSets (node2 , node3 );
69+
70+ // Union nodes that are already connected
71+ dsu .unionSets (node1 , node3 );
72+
73+ // All should have the same root
74+ Node <Integer > root = dsu .findSet (node1 );
75+ assertEquals (root , dsu .findSet (node2 ));
76+ assertEquals (root , dsu .findSet (node3 ));
77+ }
78+
79+ @ Test
80+ public void testRankIncrease () {
81+ DisjointSetUnion <Integer > dsu = new DisjointSetUnion <>();
82+ Node <Integer > node1 = dsu .makeSet (1 );
83+ Node <Integer > node2 = dsu .makeSet (2 );
84+ Node <Integer > node3 = dsu .makeSet (3 );
85+ Node <Integer > node4 = dsu .makeSet (4 );
86+
87+ dsu .unionSets (node1 , node2 ); // rank of node1 should increase
88+ dsu .unionSets (node3 , node4 ); // rank of node3 should increase
89+ dsu .unionSets (node1 , node3 ); // union two trees of same rank -> rank increases
90+
91+ assertEquals (2 , dsu .findSet (node1 ).rank );
92+ }
93+
94+ @ Test
95+ public void testMultipleMakeSets () {
96+ DisjointSetUnion <Integer > dsu = new DisjointSetUnion <>();
97+ Node <Integer > node1 = dsu .makeSet (1 );
98+ Node <Integer > node2 = dsu .makeSet (2 );
99+ Node <Integer > node3 = dsu .makeSet (3 );
100+
101+ assertNotEquals (node1 , node2 );
102+ assertNotEquals (node2 , node3 );
103+ assertNotEquals (node1 , node3 );
104+
105+ assertEquals (node1 , node1 .parent );
106+ assertEquals (node2 , node2 .parent );
107+ assertEquals (node3 , node3 .parent );
108+ }
109+
110+ @ Test
111+ public void testPathCompression () {
112+ DisjointSetUnion <Integer > dsu = new DisjointSetUnion <>();
113+ Node <Integer > node1 = dsu .makeSet (1 );
114+ Node <Integer > node2 = dsu .makeSet (2 );
115+ Node <Integer > node3 = dsu .makeSet (3 );
116+
117+ dsu .unionSets (node1 , node2 );
118+ dsu .unionSets (node2 , node3 );
119+
120+ // After findSet, path compression should update parent to root directly
121+ Node <Integer > root = dsu .findSet (node3 );
122+ assertEquals (root , node1 );
123+ assertEquals (node1 , node3 .parent );
50124 }
51125}
0 commit comments