|
2 | 2 |
|
3 | 3 | import org.junit.Test;
|
4 | 4 |
|
| 5 | +import java.util.Arrays; |
| 6 | +import java.util.List; |
| 7 | + |
5 | 8 | public class BinarySearchTreeTest {
|
6 | 9 | @Test
|
7 | 10 | public void insert_shouldCorrectlyInsertNodes() {
|
8 |
| - // Create BST instance |
9 | 11 | BinarySearchTree<Integer, String> bst = new BinarySearchTree<>();
|
10 |
| - |
11 |
| - // Insert elements |
12 | 12 | bst.insert(5, "Five");
|
13 | 13 | bst.insert(3, "Three");
|
14 | 14 | bst.insert(7, "Seven");
|
15 | 15 | bst.insert(2, "Two");
|
16 | 16 | bst.insert(4, "Four");
|
17 | 17 |
|
| 18 | + // Perform in-order traversal and get result |
| 19 | + List<String> result = bst.getInorder(); |
| 20 | + |
18 | 21 | // Expected in-order traversal result
|
19 |
| - List<Integer> expected = Arrays.asList(2, 3, 4, 5, 7); |
| 22 | + List<String> expected = Arrays.asList("Key: 2, Value: Two", "Key: 3, Value: Three", "Key: 4, Value: Four", "Key: 5, Value: Five", "Key: 7, Value: Seven"); |
| 23 | + |
| 24 | + assert result.equals(expected); |
| 25 | + } |
| 26 | + |
| 27 | + @Test |
| 28 | + public void delete_shouldCorrectlyHandleLeafNode() { |
| 29 | + BinarySearchTree<Integer, String> bst = new BinarySearchTree<>(); |
| 30 | + bst.insert(5, "Five"); |
| 31 | + bst.insert(3, "Three"); |
| 32 | + bst.insert(7, "Seven"); |
| 33 | + |
| 34 | + bst.delete(7); |
| 35 | + |
| 36 | + // Perform in-order traversal and get result |
| 37 | + List<String> result = bst.getInorder(); |
| 38 | + |
| 39 | + // Expected in-order traversal result after deletion |
| 40 | + List<String> expected = Arrays.asList("Key: 3, Value: Three", "Key: 5, Value: Five"); |
| 41 | + |
| 42 | + assert result.equals(expected); |
| 43 | + } |
| 44 | + |
| 45 | + @Test |
| 46 | + public void delete_shouldCorrectlyHandleNodeWithOneChild() { |
| 47 | + BinarySearchTree<Integer, String> bst = new BinarySearchTree<>(); |
| 48 | + bst.insert(5, "Five"); |
| 49 | + bst.insert(3, "Three"); |
| 50 | + bst.insert(6, "Six"); |
| 51 | + bst.insert(7, "Seven"); |
| 52 | + |
| 53 | + bst.delete(6); |
20 | 54 |
|
21 | 55 | // Perform in-order traversal and get result
|
22 |
| - List<Integer> result = bst.inOrderTraversal(); |
| 56 | + List<String> result = bst.getInorder(); |
| 57 | + |
| 58 | + // Expected in-order traversal result after deletion |
| 59 | + List<String> expected = Arrays.asList("Key: 3, Value: Three", "Key: 5, Value: Five", "Key: 7, Value: Seven"); |
23 | 60 |
|
24 |
| - // Assert that the result matches the expected list |
25 | 61 | assert result.equals(expected);
|
26 | 62 | }
|
27 | 63 |
|
| 64 | + @Test |
| 65 | + public void delete_shouldCorrectlyHandleNodeWithTwoChildren() { |
| 66 | + BinarySearchTree<Integer, String> bst = new BinarySearchTree<>(); |
| 67 | + bst.insert(5, "Five"); |
| 68 | + bst.insert(3, "Three"); |
| 69 | + bst.insert(7, "Seven"); |
| 70 | + bst.insert(2, "Two"); |
| 71 | + bst.insert(4, "Four"); |
| 72 | + |
| 73 | + bst.delete(3); |
| 74 | + |
| 75 | + // Perform in-order traversal and get result |
| 76 | + List<String> result = bst.getInorder(); |
| 77 | + |
| 78 | + // Expected in-order traversal result after deletion |
| 79 | + List<String> expected = Arrays.asList("Key: 2, Value: Two", "Key: 4, Value: Four", "Key: 5, Value: Five", "Key: 7, Value: Seven"); |
| 80 | + |
| 81 | + assert result.equals(expected); |
| 82 | + } |
28 | 83 | }
|
0 commit comments