Browse Source

Fix BinaryHeap/Search trees and associated tests.

Mikaël Capelle 3 years ago
parent
commit
c5518e7240

+ 1
- 1
src/main/org/insa/algo/utils/BinaryHeap.java View File

@@ -150,7 +150,7 @@ public class BinaryHeap<E extends Comparable<E>> implements PriorityQueue<E> {
150 150
     @Override
151 151
     public E findMin() throws EmptyPriorityQueueException {
152 152
         if (isEmpty())
153
-            throw new RuntimeException("Empty binary heap.");
153
+            throw new EmptyPriorityQueueException();
154 154
         return this.array.get(0);
155 155
     }
156 156
 

+ 45
- 4
src/test/org/insa/algo/utils/BinaryHeapTest.java View File

@@ -5,6 +5,7 @@ import static org.junit.Assert.assertFalse;
5 5
 import static org.junit.Assert.assertTrue;
6 6
 
7 7
 import java.util.Arrays;
8
+import java.util.HashSet;
8 9
 import java.util.stream.IntStream;
9 10
 
10 11
 import org.junit.Before;
@@ -165,8 +166,28 @@ public class BinaryHeapTest {
165 166
         int size1 = heap1.size();
166 167
         int[] deleteOrder1 = new int[] { 12, 17, 18, 19, 4, 5, 3, 2, 0, 9, 10, 16, 8, 14, 13, 15, 7,
167 168
                 6, 1, 11 };
168
-        for (int x: deleteOrder1) {
169
-            heap1.remove(this.data1[x]);
169
+        for (int i = 0; i < deleteOrder1.length; ++i) {
170
+            // Remove from structure
171
+            heap1.remove(this.data1[deleteOrder1[i]]);
172
+
173
+            // Copy the remaining elements
174
+            BinaryHeap<MutableInteger> copyTree = new BinaryHeap<>(heap1);
175
+
176
+            // Retrieve all remaining elements in both structures
177
+            MutableInteger[] remains_in = new MutableInteger[deleteOrder1.length - i - 1],
178
+                    remains_cp = new MutableInteger[deleteOrder1.length - i - 1];
179
+            for (int j = 0; j < remains_cp.length; ++j) {
180
+                remains_in[j] = this.data1[deleteOrder1[i + j + 1]];
181
+                remains_cp[j] = copyTree.deleteMin();
182
+            }
183
+
184
+            // Check that the copy is now empty, and that both list contains all
185
+            // elements.
186
+            assertTrue(copyTree.isEmpty());
187
+            assertEquals(new HashSet<>(Arrays.asList(remains_in)),
188
+                    new HashSet<>(Arrays.asList(remains_cp)));
189
+
190
+            // Check that the size of the original tree is correct.
170 191
             assertEquals(--size1, heap1.size());
171 192
         }
172 193
         assertTrue(heap1.isEmpty());
@@ -174,8 +195,28 @@ public class BinaryHeapTest {
174 195
         // heap 2
175 196
         int size2 = heap2.size();
176 197
         int[] deleteOrder2 = new int[] { 6, 5, 0, 1, 4, 2, 3 };
177
-        for (int x: deleteOrder2) {
178
-            heap2.remove(this.data2[x]);
198
+        for (int i = 0; i < deleteOrder2.length; ++i) {
199
+            // Remove from structure
200
+            heap2.remove(this.data2[deleteOrder2[i]]);
201
+
202
+            // Copy the remaining elements
203
+            BinaryHeap<MutableInteger> copyTree = new BinaryHeap<>(heap2);
204
+
205
+            // Retrieve all remaining elements in both structures
206
+            MutableInteger[] remains_in = new MutableInteger[deleteOrder2.length - i - 1],
207
+                    remains_cp = new MutableInteger[deleteOrder2.length - i - 1];
208
+            for (int j = 0; j < remains_cp.length; ++j) {
209
+                remains_in[j] = this.data2[deleteOrder2[i + j + 1]];
210
+                remains_cp[j] = copyTree.deleteMin();
211
+            }
212
+
213
+            // Check that the copy is now empty, and that both list contains all
214
+            // elements.
215
+            assertTrue(copyTree.isEmpty());
216
+            assertEquals(new HashSet<>(Arrays.asList(remains_in)),
217
+                    new HashSet<>(Arrays.asList(remains_cp)));
218
+
219
+            // Check that the size of the original tree is correct.
179 220
             assertEquals(--size2, heap2.size());
180 221
         }
181 222
         assertTrue(heap2.isEmpty());

+ 45
- 4
src/test/org/insa/algo/utils/BinarySearchTreeTest.java View File

@@ -5,6 +5,7 @@ import static org.junit.Assert.assertFalse;
5 5
 import static org.junit.Assert.assertTrue;
6 6
 
7 7
 import java.util.Arrays;
8
+import java.util.HashSet;
8 9
 import java.util.stream.IntStream;
9 10
 
10 11
 import org.junit.Before;
@@ -165,8 +166,28 @@ public class BinarySearchTreeTest {
165 166
         int size1 = searchTree1.size();
166 167
         int[] deleteOrder1 = new int[] { 12, 17, 18, 19, 4, 5, 3, 2, 0, 9, 10, 16, 8, 14, 13, 15, 7,
167 168
                 6, 1, 11 };
168
-        for (int x: deleteOrder1) {
169
-            searchTree1.remove(this.data1[x]);
169
+        for (int i = 0; i < deleteOrder1.length; ++i) {
170
+            // Remove from structure
171
+            searchTree1.remove(this.data1[deleteOrder1[i]]);
172
+
173
+            // Copy the remaining elements
174
+            BinarySearchTree<MutableInteger> copyTree = new BinarySearchTree<>(searchTree1);
175
+
176
+            // Retrieve all remaining elements in both structures
177
+            MutableInteger[] remains_in = new MutableInteger[deleteOrder1.length - i - 1],
178
+                    remains_cp = new MutableInteger[deleteOrder1.length - i - 1];
179
+            for (int j = 0; j < remains_cp.length; ++j) {
180
+                remains_in[j] = this.data1[deleteOrder1[i + j + 1]];
181
+                remains_cp[j] = copyTree.deleteMin();
182
+            }
183
+
184
+            // Check that the copy is now empty, and that both list contains all
185
+            // elements.
186
+            assertTrue(copyTree.isEmpty());
187
+            assertEquals(new HashSet<>(Arrays.asList(remains_in)),
188
+                    new HashSet<>(Arrays.asList(remains_cp)));
189
+
190
+            // Check that the size of the original tree is correct.
170 191
             assertEquals(--size1, searchTree1.size());
171 192
         }
172 193
         assertTrue(searchTree1.isEmpty());
@@ -174,8 +195,28 @@ public class BinarySearchTreeTest {
174 195
         // searchTree 2
175 196
         int size2 = searchTree2.size();
176 197
         int[] deleteOrder2 = new int[] { 6, 5, 0, 1, 4, 2, 3 };
177
-        for (int x: deleteOrder2) {
178
-            searchTree2.remove(this.data2[x]);
198
+        for (int i = 0; i < deleteOrder2.length; ++i) {
199
+            // Remove from structure
200
+            searchTree2.remove(this.data2[deleteOrder2[i]]);
201
+
202
+            // Copy the remaining elements
203
+            BinarySearchTree<MutableInteger> copyTree = new BinarySearchTree<>(searchTree2);
204
+
205
+            // Retrieve all remaining elements in both structures
206
+            MutableInteger[] remains_in = new MutableInteger[deleteOrder2.length - i - 1],
207
+                    remains_cp = new MutableInteger[deleteOrder2.length - i - 1];
208
+            for (int j = 0; j < remains_cp.length; ++j) {
209
+                remains_in[j] = this.data2[deleteOrder2[i + j + 1]];
210
+                remains_cp[j] = copyTree.deleteMin();
211
+            }
212
+
213
+            // Check that the copy is now empty, and that both list contains all
214
+            // elements.
215
+            assertTrue(copyTree.isEmpty());
216
+            assertEquals(new HashSet<>(Arrays.asList(remains_in)),
217
+                    new HashSet<>(Arrays.asList(remains_cp)));
218
+
219
+            // Check that the size of the original tree is correct.
179 220
             assertEquals(--size2, searchTree2.size());
180 221
         }
181 222
         assertTrue(searchTree2.isEmpty());

Loading…
Cancel
Save