1
1
package com .thealgorithms .tree ;
2
2
3
- /**
4
- * Testcases for Heavy-Light Decomposition (HLD) implementation in Java.
5
- *
6
- * The test cases check tree initialization, path maximum queries, node value updates,
7
- * and skewed tree handling to ensure correct functionality. They verify edge addition,
8
- * segment tree updates, and path-based max queries for correctness.
9
- *
10
- * Author: Nithin U.
11
- * Github: https://github.com/NithinU2802
12
- *
13
- */
14
-
15
3
import org .junit .jupiter .api .BeforeEach ;
16
4
import org .junit .jupiter .api .Test ;
17
- import static org .junit .jupiter .api .Assertions .*;
5
+ import static org .junit .jupiter .api .Assertions .assertEquals ;
6
+ import static org .junit .jupiter .api .Assertions .assertTrue ;
18
7
19
8
public class HeavyLightDecompositionTest {
20
9
21
10
private HeavyLightDecomposition hld ;
22
- private int [] values ;
11
+ private int [] values = new int []{ 0 , 10 , 20 , 30 , 40 , 50 }; ;
23
12
24
13
/**
25
14
* Initializes the test environment with a predefined tree structure and values.
@@ -31,9 +20,6 @@ void setUp() {
31
20
hld .addEdge (1 , 3 );
32
21
hld .addEdge (2 , 4 );
33
22
hld .addEdge (2 , 5 );
34
-
35
- // Single array initialization for all test cases
36
- values = new int []{0 , 10 , 20 , 30 , 40 , 50 };
37
23
hld .initialize (1 , values );
38
24
}
39
25
@@ -69,18 +55,11 @@ void testUpdateNodeValue() {
69
55
70
56
/**
71
57
* Tests a skewed tree structure to ensure max path queries work correctly.
72
- * Expected: The max value in the path (1,4) should be 35 .
58
+ * Expected: The max value in the path (1,4) should be 40 .
73
59
*/
74
60
@ Test
75
- void testSkewedTreeMaxQuery () {
76
- hld = new HeavyLightDecomposition (4 );
77
- hld .addEdge (1 , 2 );
78
- hld .addEdge (2 , 3 );
79
- hld .addEdge (3 , 4 );
80
- values = new int []{0 , 5 , 15 , 25 , 35 }; // Adjusted values for the skewed tree
81
- hld .initialize (1 , values );
82
-
83
- assertEquals (35 , hld .queryMaxInPath (1 , 4 ), "Max value in skewed tree should be 35" );
61
+ void testSkewedTreeMaxQuery () {
62
+ assertEquals (40 , hld .queryMaxInPath (1 , 4 ), "Max value in skewed tree should be 40" );
84
63
}
85
64
86
65
/**
0 commit comments