|
| 1 | +package com.thealgorithms.datastructures.trees; |
| 2 | + |
| 3 | +import java.util.ArrayList; |
| 4 | +import java.util.Collections; |
| 5 | +import java.util.List; |
| 6 | +import java.util.Set; |
| 7 | +import org.junit.jupiter.api.Assertions; |
| 8 | +import org.junit.jupiter.api.Test; |
| 9 | + |
| 10 | +class KDTreeDeletionTest { |
| 11 | + @Test |
| 12 | + void deleteLeavesOnBothSides() { |
| 13 | + for (int coordinate : new int[] {10, 30}) { |
| 14 | + List<KDTree.Point> points = pointsOf(20, 10, 30); |
| 15 | + KDTree tree = insertedTree(points); |
| 16 | + KDTree.Point deleted = pointOf(coordinate); |
| 17 | + tree.delete(deleted); |
| 18 | + points.remove(deleted); |
| 19 | + assertContents(tree, points); |
| 20 | + Assertions.assertFalse(tree.search(deleted).isPresent()); |
| 21 | + } |
| 22 | + } |
| 23 | + |
| 24 | + @Test |
| 25 | + void deleteNonRootWithDescendants() { |
| 26 | + for (int coordinate : new int[] {10, 30}) { |
| 27 | + List<KDTree.Point> points = pointsOf(20, 10, 30, 5, 15, 25, 35, 13, 27); |
| 28 | + KDTree tree = insertedTree(points); |
| 29 | + KDTree.Point deleted = pointOf(coordinate); |
| 30 | + tree.delete(deleted); |
| 31 | + points.remove(deleted); |
| 32 | + assertContents(tree, points); |
| 33 | + Assertions.assertFalse(tree.search(deleted).isPresent()); |
| 34 | + } |
| 35 | + } |
| 36 | + |
| 37 | + @Test |
| 38 | + void deleteRootWithOnlyLeftSubtree() { |
| 39 | + List<KDTree.Point> points = pointsOf(20, 10, 15, 5); |
| 40 | + KDTree tree = insertedTree(points); |
| 41 | + KDTree.Point deleted = points.removeFirst(); |
| 42 | + tree.delete(deleted); |
| 43 | + assertContents(tree, points); |
| 44 | + Assertions.assertFalse(tree.search(deleted).isPresent()); |
| 45 | + KDTree.Point inserted = pointOf(12); |
| 46 | + tree.insert(inserted); |
| 47 | + points.add(inserted); |
| 48 | + assertContents(tree, points); |
| 49 | + tree.delete(inserted); |
| 50 | + points.remove(inserted); |
| 51 | + assertContents(tree, points); |
| 52 | + } |
| 53 | + |
| 54 | + @Test |
| 55 | + void deleteRootWithNestedRightSuccessor() { |
| 56 | + List<KDTree.Point> points = pointsOf(20, 30, 25, 27, 40); |
| 57 | + KDTree tree = insertedTree(points); |
| 58 | + KDTree.Point deleted = points.removeFirst(); |
| 59 | + tree.delete(deleted); |
| 60 | + assertContents(tree, points); |
| 61 | + Assertions.assertFalse(tree.search(deleted).isPresent()); |
| 62 | + KDTree.Point successor = pointOf(25); |
| 63 | + Assertions.assertEquals(successor, tree.getRoot().getPoint()); |
| 64 | + tree.delete(successor); |
| 65 | + points.remove(successor); |
| 66 | + assertContents(tree, points); |
| 67 | + Assertions.assertFalse(tree.search(successor).isPresent()); |
| 68 | + } |
| 69 | + |
| 70 | + @Test |
| 71 | + void deletingAbsentPointPreservesContents() { |
| 72 | + List<KDTree.Point> points = pointsOf(20, 10, 30); |
| 73 | + KDTree tree = insertedTree(points); |
| 74 | + Assertions.assertThrows(IllegalArgumentException.class, () -> tree.delete(pointOf(15))); |
| 75 | + assertContents(tree, points); |
| 76 | + for (KDTree.Point point : List.copyOf(points)) { |
| 77 | + tree.delete(point); |
| 78 | + points.remove(point); |
| 79 | + assertContents(tree, points); |
| 80 | + } |
| 81 | + Assertions.assertNull(tree.getRoot()); |
| 82 | + Assertions.assertThrows(IllegalArgumentException.class, () -> tree.delete(pointOf(20))); |
| 83 | + Assertions.assertNull(tree.getRoot()); |
| 84 | + } |
| 85 | + |
| 86 | + @Test |
| 87 | + void deletionOrdersPreserveBuiltAndInsertedTrees() { |
| 88 | + for (int dimension : new int[] {1, 2, 3}) { |
| 89 | + List<KDTree.Point> initial = new ArrayList<>(); |
| 90 | + for (int index = 0; index < 15; index++) { |
| 91 | + int[] coordinates = new int[dimension]; |
| 92 | + for (int axis = 0; axis < dimension; axis++) { |
| 93 | + coordinates[axis] = index * (axis + 1) % 17 - 8; |
| 94 | + } |
| 95 | + initial.add(new KDTree.Point(coordinates)); |
| 96 | + } |
| 97 | + for (boolean bulkBuild : new boolean[] {false, true}) { |
| 98 | + for (int shift : new int[] {0, 4, 9}) { |
| 99 | + List<KDTree.Point> remaining = new ArrayList<>(initial); |
| 100 | + List<KDTree.Point> order = new ArrayList<>(initial); |
| 101 | + Collections.rotate(order, shift); |
| 102 | + KDTree tree = bulkBuild ? new KDTree(initial.toArray(KDTree.Point[] ::new)) : insertedTree(initial); |
| 103 | + assertContents(tree, remaining); |
| 104 | + for (KDTree.Point point : order) { |
| 105 | + tree.delete(point); |
| 106 | + remaining.remove(point); |
| 107 | + assertContents(tree, remaining); |
| 108 | + Assertions.assertFalse(tree.search(point).isPresent()); |
| 109 | + } |
| 110 | + Assertions.assertNull(tree.getRoot()); |
| 111 | + } |
| 112 | + } |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + private static KDTree.Point pointOf(int coordinate) { |
| 117 | + return new KDTree.Point(new int[] {coordinate, coordinate}); |
| 118 | + } |
| 119 | + |
| 120 | + private static List<KDTree.Point> pointsOf(int... coordinates) { |
| 121 | + List<KDTree.Point> points = new ArrayList<>(); |
| 122 | + for (int coordinate : coordinates) { |
| 123 | + points.add(pointOf(coordinate)); |
| 124 | + } |
| 125 | + return points; |
| 126 | + } |
| 127 | + |
| 128 | + private static KDTree insertedTree(List<KDTree.Point> points) { |
| 129 | + KDTree tree = new KDTree(points.getFirst().getDimension()); |
| 130 | + for (KDTree.Point point : points) { |
| 131 | + tree.insert(point); |
| 132 | + } |
| 133 | + return tree; |
| 134 | + } |
| 135 | + |
| 136 | + private static void assertContents(KDTree tree, List<KDTree.Point> expected) { |
| 137 | + List<KDTree.Point> actual = new ArrayList<>(); |
| 138 | + collectPoints(tree.getRoot(), actual); |
| 139 | + Assertions.assertEquals(expected.size(), actual.size()); |
| 140 | + Assertions.assertEquals(Set.copyOf(expected), Set.copyOf(actual)); |
| 141 | + for (KDTree.Point point : expected) { |
| 142 | + Assertions.assertTrue(tree.search(point).isPresent(), () -> "Surviving point is unreachable: " + point); |
| 143 | + } |
| 144 | + if (!expected.isEmpty()) { |
| 145 | + for (int axis = 0; axis < expected.getFirst().getDimension(); axis++) { |
| 146 | + int minimum = Integer.MAX_VALUE; |
| 147 | + int maximum = Integer.MIN_VALUE; |
| 148 | + for (KDTree.Point point : expected) { |
| 149 | + minimum = Math.min(minimum, point.getCoordinate(axis)); |
| 150 | + maximum = Math.max(maximum, point.getCoordinate(axis)); |
| 151 | + } |
| 152 | + Assertions.assertEquals(minimum, tree.findMin(axis).getCoordinate(axis)); |
| 153 | + Assertions.assertEquals(maximum, tree.findMax(axis).getCoordinate(axis)); |
| 154 | + } |
| 155 | + } |
| 156 | + } |
| 157 | + |
| 158 | + private static void collectPoints(KDTree.Node node, List<KDTree.Point> points) { |
| 159 | + if (node != null) { |
| 160 | + points.add(node.getPoint()); |
| 161 | + collectPoints(node.getLeft(), points); |
| 162 | + collectPoints(node.getRight(), points); |
| 163 | + } |
| 164 | + } |
| 165 | +} |
0 commit comments