|
| 1 | +package com.thealgorithms.tree; |
| 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 | +import org.junit.jupiter.api.BeforeEach; |
| 16 | +import org.junit.jupiter.api.Test; |
| 17 | +import static org.junit.jupiter.api.Assertions.*; |
| 18 | + |
| 19 | +public class HeavyLightDecompositionTest { |
| 20 | + |
| 21 | + private HeavyLightDecomposition hld; |
| 22 | + private int[] values; |
| 23 | + |
| 24 | + /** |
| 25 | + * Initializes the test environment with a predefined tree structure and values. |
| 26 | + */ |
| 27 | + @BeforeEach |
| 28 | + void setUp() { |
| 29 | + hld = new HeavyLightDecomposition(5); |
| 30 | + hld.addEdge(1, 2); |
| 31 | + hld.addEdge(1, 3); |
| 32 | + hld.addEdge(2, 4); |
| 33 | + hld.addEdge(2, 5); |
| 34 | + |
| 35 | + // Single array initialization for all test cases |
| 36 | + values = new int[]{0, 10, 20, 30, 40, 50}; |
| 37 | + hld.initialize(1, values); |
| 38 | + } |
| 39 | + |
| 40 | + /** |
| 41 | + * Tests the basic initialization of the tree structure. |
| 42 | + * Expected: The tree should initialize without errors. |
| 43 | + */ |
| 44 | + @Test |
| 45 | + void testBasicTreeInitialization() { |
| 46 | + assertTrue(true, "Basic tree structure initialized successfully"); |
| 47 | + } |
| 48 | + |
| 49 | + /** |
| 50 | + * Tests the maximum value query in a path between nodes. |
| 51 | + * Expected: The max value in the path (4,5) should be 50. |
| 52 | + * Expected: The max value in the path (3,2) should be 30. |
| 53 | + */ |
| 54 | + @Test |
| 55 | + void testQueryMaxInPath() { |
| 56 | + assertEquals(50, hld.queryMaxInPath(4, 5), "Max value in path should be 50"); |
| 57 | + assertEquals(30, hld.queryMaxInPath(3, 2), "Max value in path should be 30"); |
| 58 | + } |
| 59 | + |
| 60 | + /** |
| 61 | + * Tests updating a node's value and ensuring it's reflected in queries. |
| 62 | + * Expected: The updated node's value should affect query results. |
| 63 | + */ |
| 64 | + @Test |
| 65 | + void testUpdateNodeValue() { |
| 66 | + hld.updateSegmentTree(1, 0, hld.getPositionIndex() - 1, hld.getPosition(4), 100); |
| 67 | + assertEquals(100, hld.queryMaxInPath(4, 5), "Updated value should be reflected in query"); |
| 68 | + } |
| 69 | + |
| 70 | + /** |
| 71 | + * 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. |
| 73 | + */ |
| 74 | + @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"); |
| 84 | + } |
| 85 | +} |
0 commit comments