Keine Beschreibung
Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

BinaryHeapTest.java 7.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. package org.insa.algo.utils;
  2. import static org.junit.Assert.assertEquals;
  3. import static org.junit.Assert.assertFalse;
  4. import static org.junit.Assert.assertTrue;
  5. import java.util.Arrays;
  6. import java.util.HashSet;
  7. import java.util.stream.IntStream;
  8. import org.junit.Before;
  9. import org.junit.Test;
  10. public class BinaryHeapTest {
  11. class MutableInteger implements Comparable<MutableInteger> {
  12. // Actual value
  13. private int value;
  14. public MutableInteger(int value) {
  15. this.value = value;
  16. }
  17. /**
  18. * @return The integer value stored inside this MutableInteger.
  19. */
  20. public int get() {
  21. return this.value;
  22. }
  23. /**
  24. * Update the integer value stored inside this MutableInteger.
  25. *
  26. * @param value New value to set.
  27. */
  28. public void set(int value) {
  29. this.value = value;
  30. }
  31. @Override
  32. public int compareTo(MutableInteger other) {
  33. return Integer.compare(this.value, other.value);
  34. }
  35. };
  36. // Raw data arrays.
  37. private MutableInteger[] data1 = IntStream.range(0, 20).mapToObj(MutableInteger::new)
  38. .toArray(MutableInteger[]::new);
  39. private MutableInteger[] data2 = Arrays.stream(new int[] { 8, 1, 6, 3, 4, 5, 9 })
  40. .mapToObj(MutableInteger::new).toArray(MutableInteger[]::new);
  41. // Actual heap.
  42. private BinaryHeap<MutableInteger> heap1, heap2;
  43. @Before
  44. public void init() {
  45. // Create the range heap
  46. this.heap1 = new BinaryHeap<>();
  47. this.heap2 = new BinaryHeap<>();
  48. for (MutableInteger v: data1) {
  49. this.heap1.insert(v);
  50. }
  51. for (MutableInteger v: data2) {
  52. this.heap2.insert(v);
  53. }
  54. }
  55. @Test
  56. public void testIsEmpty() {
  57. BinaryHeap<MutableInteger> tree = new BinaryHeap<>();
  58. assertTrue(tree.isEmpty());
  59. assertFalse(this.heap1.isEmpty());
  60. assertFalse(this.heap2.isEmpty());
  61. }
  62. @Test
  63. public void testSize() {
  64. BinaryHeap<MutableInteger> tree = new BinaryHeap<>();
  65. assertEquals(0, tree.size());
  66. assertEquals(20, this.heap1.size());
  67. assertEquals(7, this.heap2.size());
  68. }
  69. @Test
  70. public void testInsert() {
  71. BinaryHeap<MutableInteger> heap = new BinaryHeap<>();
  72. int size = 0;
  73. for (MutableInteger x: data1) {
  74. heap.insert(x);
  75. assertEquals(++size, heap.size());
  76. }
  77. assertEquals(data1.length, heap.size());
  78. heap = new BinaryHeap<>();
  79. size = 0;
  80. for (MutableInteger x: data2) {
  81. heap.insert(x);
  82. assertEquals(++size, heap.size());
  83. }
  84. assertEquals(data2.length, heap.size());
  85. }
  86. @Test(expected = EmptyPriorityQueueException.class)
  87. public void testEmptyFindMin() {
  88. BinaryHeap<MutableInteger> heap = new BinaryHeap<>();
  89. heap.findMin();
  90. }
  91. @Test
  92. public void testFindMin() {
  93. assertEquals(0, heap1.findMin().get());
  94. assertEquals(1, heap2.findMin().get());
  95. }
  96. @Test(expected = EmptyPriorityQueueException.class)
  97. public void testEmptyDeleteMin() {
  98. BinaryHeap<MutableInteger> heap = new BinaryHeap<>();
  99. heap.deleteMin();
  100. }
  101. @Test
  102. public void testDeleteMin() {
  103. // range 1 (sorted)
  104. int size = data1.length;
  105. assertEquals(heap1.size(), size);
  106. for (MutableInteger x: data1) {
  107. assertEquals(x, heap1.deleteMin());
  108. size -= 1;
  109. assertEquals(size, heap1.size());
  110. }
  111. assertEquals(0, heap1.size());
  112. assertTrue(heap1.isEmpty());
  113. // range 2 (was not sorted)
  114. MutableInteger[] range2 = Arrays.copyOf(data2, data2.length);
  115. Arrays.sort(range2);
  116. size = range2.length;
  117. assertEquals(heap2.size(), size);
  118. for (MutableInteger x: range2) {
  119. assertEquals(x.get(), heap2.deleteMin().get());
  120. size -= 1;
  121. assertEquals(size, heap2.size());
  122. }
  123. assertEquals(0, heap2.size());
  124. assertTrue(heap2.isEmpty());
  125. }
  126. @Test(expected = ElementNotFoundException.class)
  127. public void testRemoveEmpty() {
  128. BinaryHeap<MutableInteger> heap = new BinaryHeap<>();
  129. heap.remove(new MutableInteger(0));
  130. }
  131. @Test(expected = ElementNotFoundException.class)
  132. public void testRemoveNotFound() {
  133. heap1.remove(new MutableInteger(20));
  134. }
  135. @Test
  136. public void testRemove() {
  137. // heap 1
  138. int size1 = heap1.size();
  139. int[] deleteOrder1 = new int[] { 12, 17, 18, 19, 4, 5, 3, 2, 0, 9, 10, 16, 8, 14, 13, 15, 7,
  140. 6, 1, 11 };
  141. for (int i = 0; i < deleteOrder1.length; ++i) {
  142. // Remove from structure
  143. heap1.remove(this.data1[deleteOrder1[i]]);
  144. // Copy the remaining elements
  145. BinaryHeap<MutableInteger> copyTree = new BinaryHeap<>(heap1);
  146. // Retrieve all remaining elements in both structures
  147. MutableInteger[] remains_in = new MutableInteger[deleteOrder1.length - i - 1],
  148. remains_cp = new MutableInteger[deleteOrder1.length - i - 1];
  149. for (int j = 0; j < remains_cp.length; ++j) {
  150. remains_in[j] = this.data1[deleteOrder1[i + j + 1]];
  151. remains_cp[j] = copyTree.deleteMin();
  152. }
  153. // Check that the copy is now empty, and that both list contains all
  154. // elements.
  155. assertTrue(copyTree.isEmpty());
  156. assertEquals(new HashSet<>(Arrays.asList(remains_in)),
  157. new HashSet<>(Arrays.asList(remains_cp)));
  158. // Check that the size of the original tree is correct.
  159. assertEquals(--size1, heap1.size());
  160. }
  161. assertTrue(heap1.isEmpty());
  162. // heap 2
  163. int size2 = heap2.size();
  164. int[] deleteOrder2 = new int[] { 6, 5, 0, 1, 4, 2, 3 };
  165. for (int i = 0; i < deleteOrder2.length; ++i) {
  166. // Remove from structure
  167. heap2.remove(this.data2[deleteOrder2[i]]);
  168. // Copy the remaining elements
  169. BinaryHeap<MutableInteger> copyTree = new BinaryHeap<>(heap2);
  170. // Retrieve all remaining elements in both structures
  171. MutableInteger[] remains_in = new MutableInteger[deleteOrder2.length - i - 1],
  172. remains_cp = new MutableInteger[deleteOrder2.length - i - 1];
  173. for (int j = 0; j < remains_cp.length; ++j) {
  174. remains_in[j] = this.data2[deleteOrder2[i + j + 1]];
  175. remains_cp[j] = copyTree.deleteMin();
  176. }
  177. // Check that the copy is now empty, and that both list contains all
  178. // elements.
  179. assertTrue(copyTree.isEmpty());
  180. assertEquals(new HashSet<>(Arrays.asList(remains_in)),
  181. new HashSet<>(Arrays.asList(remains_cp)));
  182. // Check that the size of the original tree is correct.
  183. assertEquals(--size2, heap2.size());
  184. }
  185. assertTrue(heap2.isEmpty());
  186. }
  187. @Test
  188. public void testRemoveThenAdd() {
  189. MutableInteger mi5 = this.data1[6];
  190. heap1.remove(mi5);
  191. assertEquals(19, heap1.size());
  192. mi5.set(-20);
  193. heap1.insert(mi5);
  194. assertEquals(20, heap1.size());
  195. assertEquals(-20, heap1.findMin().get());
  196. }
  197. }