From c5518e724059d232b9b4737e8c244cec3b16f99f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mika=C3=ABl=20Capelle?= Date: Mon, 26 Mar 2018 12:27:57 +0200 Subject: [PATCH] Fix BinaryHeap/Search trees and associated tests. --- src/main/org/insa/algo/utils/BinaryHeap.java | 2 +- .../org/insa/algo/utils/BinaryHeapTest.java | 49 +++++++++++++++++-- .../insa/algo/utils/BinarySearchTreeTest.java | 49 +++++++++++++++++-- 3 files changed, 91 insertions(+), 9 deletions(-) diff --git a/src/main/org/insa/algo/utils/BinaryHeap.java b/src/main/org/insa/algo/utils/BinaryHeap.java index d3f350a..c086654 100644 --- a/src/main/org/insa/algo/utils/BinaryHeap.java +++ b/src/main/org/insa/algo/utils/BinaryHeap.java @@ -150,7 +150,7 @@ public class BinaryHeap> implements PriorityQueue { @Override public E findMin() throws EmptyPriorityQueueException { if (isEmpty()) - throw new RuntimeException("Empty binary heap."); + throw new EmptyPriorityQueueException(); return this.array.get(0); } diff --git a/src/test/org/insa/algo/utils/BinaryHeapTest.java b/src/test/org/insa/algo/utils/BinaryHeapTest.java index d9ff2a7..8f89d70 100644 --- a/src/test/org/insa/algo/utils/BinaryHeapTest.java +++ b/src/test/org/insa/algo/utils/BinaryHeapTest.java @@ -5,6 +5,7 @@ import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; import java.util.Arrays; +import java.util.HashSet; import java.util.stream.IntStream; import org.junit.Before; @@ -165,8 +166,28 @@ public class BinaryHeapTest { int size1 = heap1.size(); int[] deleteOrder1 = new int[] { 12, 17, 18, 19, 4, 5, 3, 2, 0, 9, 10, 16, 8, 14, 13, 15, 7, 6, 1, 11 }; - for (int x: deleteOrder1) { - heap1.remove(this.data1[x]); + for (int i = 0; i < deleteOrder1.length; ++i) { + // Remove from structure + heap1.remove(this.data1[deleteOrder1[i]]); + + // Copy the remaining elements + BinaryHeap copyTree = new BinaryHeap<>(heap1); + + // Retrieve all remaining elements in both structures + MutableInteger[] remains_in = new MutableInteger[deleteOrder1.length - i - 1], + remains_cp = new MutableInteger[deleteOrder1.length - i - 1]; + for (int j = 0; j < remains_cp.length; ++j) { + remains_in[j] = this.data1[deleteOrder1[i + j + 1]]; + remains_cp[j] = copyTree.deleteMin(); + } + + // Check that the copy is now empty, and that both list contains all + // elements. + assertTrue(copyTree.isEmpty()); + assertEquals(new HashSet<>(Arrays.asList(remains_in)), + new HashSet<>(Arrays.asList(remains_cp))); + + // Check that the size of the original tree is correct. assertEquals(--size1, heap1.size()); } assertTrue(heap1.isEmpty()); @@ -174,8 +195,28 @@ public class BinaryHeapTest { // heap 2 int size2 = heap2.size(); int[] deleteOrder2 = new int[] { 6, 5, 0, 1, 4, 2, 3 }; - for (int x: deleteOrder2) { - heap2.remove(this.data2[x]); + for (int i = 0; i < deleteOrder2.length; ++i) { + // Remove from structure + heap2.remove(this.data2[deleteOrder2[i]]); + + // Copy the remaining elements + BinaryHeap copyTree = new BinaryHeap<>(heap2); + + // Retrieve all remaining elements in both structures + MutableInteger[] remains_in = new MutableInteger[deleteOrder2.length - i - 1], + remains_cp = new MutableInteger[deleteOrder2.length - i - 1]; + for (int j = 0; j < remains_cp.length; ++j) { + remains_in[j] = this.data2[deleteOrder2[i + j + 1]]; + remains_cp[j] = copyTree.deleteMin(); + } + + // Check that the copy is now empty, and that both list contains all + // elements. + assertTrue(copyTree.isEmpty()); + assertEquals(new HashSet<>(Arrays.asList(remains_in)), + new HashSet<>(Arrays.asList(remains_cp))); + + // Check that the size of the original tree is correct. assertEquals(--size2, heap2.size()); } assertTrue(heap2.isEmpty()); diff --git a/src/test/org/insa/algo/utils/BinarySearchTreeTest.java b/src/test/org/insa/algo/utils/BinarySearchTreeTest.java index 5c58442..adb50c1 100644 --- a/src/test/org/insa/algo/utils/BinarySearchTreeTest.java +++ b/src/test/org/insa/algo/utils/BinarySearchTreeTest.java @@ -5,6 +5,7 @@ import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; import java.util.Arrays; +import java.util.HashSet; import java.util.stream.IntStream; import org.junit.Before; @@ -165,8 +166,28 @@ public class BinarySearchTreeTest { int size1 = searchTree1.size(); int[] deleteOrder1 = new int[] { 12, 17, 18, 19, 4, 5, 3, 2, 0, 9, 10, 16, 8, 14, 13, 15, 7, 6, 1, 11 }; - for (int x: deleteOrder1) { - searchTree1.remove(this.data1[x]); + for (int i = 0; i < deleteOrder1.length; ++i) { + // Remove from structure + searchTree1.remove(this.data1[deleteOrder1[i]]); + + // Copy the remaining elements + BinarySearchTree copyTree = new BinarySearchTree<>(searchTree1); + + // Retrieve all remaining elements in both structures + MutableInteger[] remains_in = new MutableInteger[deleteOrder1.length - i - 1], + remains_cp = new MutableInteger[deleteOrder1.length - i - 1]; + for (int j = 0; j < remains_cp.length; ++j) { + remains_in[j] = this.data1[deleteOrder1[i + j + 1]]; + remains_cp[j] = copyTree.deleteMin(); + } + + // Check that the copy is now empty, and that both list contains all + // elements. + assertTrue(copyTree.isEmpty()); + assertEquals(new HashSet<>(Arrays.asList(remains_in)), + new HashSet<>(Arrays.asList(remains_cp))); + + // Check that the size of the original tree is correct. assertEquals(--size1, searchTree1.size()); } assertTrue(searchTree1.isEmpty()); @@ -174,8 +195,28 @@ public class BinarySearchTreeTest { // searchTree 2 int size2 = searchTree2.size(); int[] deleteOrder2 = new int[] { 6, 5, 0, 1, 4, 2, 3 }; - for (int x: deleteOrder2) { - searchTree2.remove(this.data2[x]); + for (int i = 0; i < deleteOrder2.length; ++i) { + // Remove from structure + searchTree2.remove(this.data2[deleteOrder2[i]]); + + // Copy the remaining elements + BinarySearchTree copyTree = new BinarySearchTree<>(searchTree2); + + // Retrieve all remaining elements in both structures + MutableInteger[] remains_in = new MutableInteger[deleteOrder2.length - i - 1], + remains_cp = new MutableInteger[deleteOrder2.length - i - 1]; + for (int j = 0; j < remains_cp.length; ++j) { + remains_in[j] = this.data2[deleteOrder2[i + j + 1]]; + remains_cp[j] = copyTree.deleteMin(); + } + + // Check that the copy is now empty, and that both list contains all + // elements. + assertTrue(copyTree.isEmpty()); + assertEquals(new HashSet<>(Arrays.asList(remains_in)), + new HashSet<>(Arrays.asList(remains_cp))); + + // Check that the size of the original tree is correct. assertEquals(--size2, searchTree2.size()); } assertTrue(searchTree2.isEmpty());