|
| 1 | +package com.thealgorithms.datastructures.trees; |
| 2 | + |
| 3 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 4 | +import static org.junit.jupiter.api.Assertions.assertFalse; |
| 5 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 6 | + |
| 7 | +import java.util.List; |
| 8 | +import org.junit.jupiter.api.BeforeEach; |
| 9 | +import org.junit.jupiter.api.Test; |
| 10 | + |
| 11 | +/** |
| 12 | + * Unit tests for BSTRecursiveGeneric class. |
| 13 | + * Covers insertion, deletion, search, traversal, sorting, and display. |
| 14 | + * |
| 15 | + * Author: Udaya Krishnan M |
| 16 | + * GitHub: https://github.com/UdayaKrishnanM/ |
| 17 | + */ |
| 18 | +class BSTRecursiveGenericTest { |
| 19 | + |
| 20 | + private BSTRecursiveGeneric<Integer> intTree; |
| 21 | + private BSTRecursiveGeneric<String> stringTree; |
| 22 | + |
| 23 | + /** |
| 24 | + * Initializes test trees before each test. |
| 25 | + */ |
| 26 | + @BeforeEach |
| 27 | + void setUp() { |
| 28 | + intTree = new BSTRecursiveGeneric<>(); |
| 29 | + stringTree = new BSTRecursiveGeneric<>(); |
| 30 | + } |
| 31 | + |
| 32 | + /** |
| 33 | + * Tests insertion and search of integer values. |
| 34 | + */ |
| 35 | + @Test |
| 36 | + void testAddAndFindInteger() { |
| 37 | + intTree.add(10); |
| 38 | + intTree.add(5); |
| 39 | + intTree.add(15); |
| 40 | + assertTrue(intTree.find(10)); |
| 41 | + assertTrue(intTree.find(5)); |
| 42 | + assertTrue(intTree.find(15)); |
| 43 | + assertFalse(intTree.find(20)); |
| 44 | + } |
| 45 | + |
| 46 | + /** |
| 47 | + * Tests insertion and search of string values. |
| 48 | + */ |
| 49 | + @Test |
| 50 | + void testAddAndFindString() { |
| 51 | + stringTree.add("apple"); |
| 52 | + stringTree.add("banana"); |
| 53 | + stringTree.add("cherry"); |
| 54 | + assertTrue(stringTree.find("banana")); |
| 55 | + assertFalse(stringTree.find("date")); |
| 56 | + } |
| 57 | + |
| 58 | + /** |
| 59 | + * Tests deletion of existing and non-existing elements. |
| 60 | + */ |
| 61 | + @Test |
| 62 | + void testRemoveElements() { |
| 63 | + intTree.add(10); |
| 64 | + intTree.add(5); |
| 65 | + intTree.add(15); |
| 66 | + assertTrue(intTree.find(5)); |
| 67 | + intTree.remove(5); |
| 68 | + assertFalse(intTree.find(5)); |
| 69 | + intTree.remove(100); // non-existent |
| 70 | + assertFalse(intTree.find(100)); |
| 71 | + } |
| 72 | + |
| 73 | + /** |
| 74 | + * Tests inorder traversal output. |
| 75 | + */ |
| 76 | + @Test |
| 77 | + void testInorderTraversal() { |
| 78 | + intTree.add(20); |
| 79 | + intTree.add(10); |
| 80 | + intTree.add(30); |
| 81 | + intTree.inorder(); // visually verify output |
| 82 | + assertTrue(true); |
| 83 | + } |
| 84 | + |
| 85 | + /** |
| 86 | + * Tests preorder traversal output. |
| 87 | + */ |
| 88 | + @Test |
| 89 | + void testPreorderTraversal() { |
| 90 | + intTree.add(20); |
| 91 | + intTree.add(10); |
| 92 | + intTree.add(30); |
| 93 | + intTree.preorder(); // visually verify output |
| 94 | + assertTrue(true); |
| 95 | + } |
| 96 | + |
| 97 | + /** |
| 98 | + * Tests postorder traversal output. |
| 99 | + */ |
| 100 | + @Test |
| 101 | + void testPostorderTraversal() { |
| 102 | + intTree.add(20); |
| 103 | + intTree.add(10); |
| 104 | + intTree.add(30); |
| 105 | + intTree.postorder(); // visually verify output |
| 106 | + assertTrue(true); |
| 107 | + } |
| 108 | + |
| 109 | + /** |
| 110 | + * Tests inorderSort returns sorted list. |
| 111 | + */ |
| 112 | + @Test |
| 113 | + void testInorderSort() { |
| 114 | + intTree.add(30); |
| 115 | + intTree.add(10); |
| 116 | + intTree.add(20); |
| 117 | + List<Integer> sorted = intTree.inorderSort(); |
| 118 | + assertEquals(List.of(10, 20, 30), sorted); |
| 119 | + } |
| 120 | + |
| 121 | + /** |
| 122 | + * Tests prettyDisplay method for visual tree structure. |
| 123 | + */ |
| 124 | + @Test |
| 125 | + void testPrettyDisplay() { |
| 126 | + intTree.add(50); |
| 127 | + intTree.add(30); |
| 128 | + intTree.add(70); |
| 129 | + intTree.add(20); |
| 130 | + intTree.add(40); |
| 131 | + intTree.add(60); |
| 132 | + intTree.add(80); |
| 133 | + intTree.prettyDisplay(); // visually verify output |
| 134 | + assertTrue(true); |
| 135 | + } |
| 136 | + |
| 137 | + /** |
| 138 | + * Tests edge case: empty tree. |
| 139 | + */ |
| 140 | + @Test |
| 141 | + void testEmptyTree() { |
| 142 | + assertFalse(intTree.find(1)); |
| 143 | + List<Integer> sorted = intTree.inorderSort(); |
| 144 | + assertTrue(sorted.isEmpty()); |
| 145 | + } |
| 146 | + |
| 147 | + /** |
| 148 | + * Tests edge case: single node tree. |
| 149 | + */ |
| 150 | + @Test |
| 151 | + void testSingleNodeTree() { |
| 152 | + intTree.add(42); |
| 153 | + assertTrue(intTree.find(42)); |
| 154 | + intTree.remove(42); |
| 155 | + assertFalse(intTree.find(42)); |
| 156 | + } |
| 157 | + |
| 158 | + /** |
| 159 | + * Tests duplicate insertions. |
| 160 | + */ |
| 161 | + @Test |
| 162 | + void testDuplicateInsertions() { |
| 163 | + intTree.add(10); |
| 164 | + intTree.add(10); |
| 165 | + intTree.add(10); |
| 166 | + List<Integer> sorted = intTree.inorderSort(); |
| 167 | + assertEquals(List.of(10), sorted); // assuming duplicates are ignored |
| 168 | + } |
| 169 | +} |
0 commit comments