Fix BinaryHeap/Search trees and associated tests.

This commit is contained in:
Mikaël Capelle 2018-03-26 12:27:57 +02:00
parent aab8743d3c
commit c5518e7240
3 changed files with 91 additions and 9 deletions

View File

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

View File

@ -5,6 +5,7 @@ import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertTrue;
import java.util.Arrays; import java.util.Arrays;
import java.util.HashSet;
import java.util.stream.IntStream; import java.util.stream.IntStream;
import org.junit.Before; import org.junit.Before;
@ -165,8 +166,28 @@ public class BinaryHeapTest {
int size1 = heap1.size(); 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, int[] deleteOrder1 = new int[] { 12, 17, 18, 19, 4, 5, 3, 2, 0, 9, 10, 16, 8, 14, 13, 15, 7,
6, 1, 11 }; 6, 1, 11 };
for (int x: deleteOrder1) { for (int i = 0; i < deleteOrder1.length; ++i) {
heap1.remove(this.data1[x]); // Remove from structure
heap1.remove(this.data1[deleteOrder1[i]]);
// Copy the remaining elements
BinaryHeap<MutableInteger> 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()); assertEquals(--size1, heap1.size());
} }
assertTrue(heap1.isEmpty()); assertTrue(heap1.isEmpty());
@ -174,8 +195,28 @@ public class BinaryHeapTest {
// heap 2 // heap 2
int size2 = heap2.size(); int size2 = heap2.size();
int[] deleteOrder2 = new int[] { 6, 5, 0, 1, 4, 2, 3 }; int[] deleteOrder2 = new int[] { 6, 5, 0, 1, 4, 2, 3 };
for (int x: deleteOrder2) { for (int i = 0; i < deleteOrder2.length; ++i) {
heap2.remove(this.data2[x]); // Remove from structure
heap2.remove(this.data2[deleteOrder2[i]]);
// Copy the remaining elements
BinaryHeap<MutableInteger> 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()); assertEquals(--size2, heap2.size());
} }
assertTrue(heap2.isEmpty()); assertTrue(heap2.isEmpty());

View File

@ -5,6 +5,7 @@ import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertTrue;
import java.util.Arrays; import java.util.Arrays;
import java.util.HashSet;
import java.util.stream.IntStream; import java.util.stream.IntStream;
import org.junit.Before; import org.junit.Before;
@ -165,8 +166,28 @@ public class BinarySearchTreeTest {
int size1 = searchTree1.size(); 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, int[] deleteOrder1 = new int[] { 12, 17, 18, 19, 4, 5, 3, 2, 0, 9, 10, 16, 8, 14, 13, 15, 7,
6, 1, 11 }; 6, 1, 11 };
for (int x: deleteOrder1) { for (int i = 0; i < deleteOrder1.length; ++i) {
searchTree1.remove(this.data1[x]); // Remove from structure
searchTree1.remove(this.data1[deleteOrder1[i]]);
// Copy the remaining elements
BinarySearchTree<MutableInteger> 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()); assertEquals(--size1, searchTree1.size());
} }
assertTrue(searchTree1.isEmpty()); assertTrue(searchTree1.isEmpty());
@ -174,8 +195,28 @@ public class BinarySearchTreeTest {
// searchTree 2 // searchTree 2
int size2 = searchTree2.size(); int size2 = searchTree2.size();
int[] deleteOrder2 = new int[] { 6, 5, 0, 1, 4, 2, 3 }; int[] deleteOrder2 = new int[] { 6, 5, 0, 1, 4, 2, 3 };
for (int x: deleteOrder2) { for (int i = 0; i < deleteOrder2.length; ++i) {
searchTree2.remove(this.data2[x]); // Remove from structure
searchTree2.remove(this.data2[deleteOrder2[i]]);
// Copy the remaining elements
BinarySearchTree<MutableInteger> 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()); assertEquals(--size2, searchTree2.size());
} }
assertTrue(searchTree2.isEmpty()); assertTrue(searchTree2.isEmpty());