No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

BinaryHeap.java 5.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. //
  2. // ******************PUBLIC OPERATIONS*********************
  3. // void insert( x ) --> Insert x
  4. // Comparable deleteMin( )--> Return and remove smallest item
  5. // Comparable findMin( ) --> Return smallest item
  6. // boolean isEmpty( ) --> Return true if empty; else false
  7. // ******************ERRORS********************************
  8. // Throws RuntimeException for findMin and deleteMin when empty
  9. package org.insa.algo.utils;
  10. import java.util.ArrayList;
  11. /**
  12. * Implements a binary heap. Note that all "matching" is based on the compareTo
  13. * method.
  14. *
  15. * @author Mark Allen Weiss
  16. * @author DLB
  17. */
  18. public class BinaryHeap<E extends Comparable<E>> implements PriorityQueue<E> {
  19. // Number of elements in heap.
  20. private int currentSize;
  21. // The heap array.
  22. private final ArrayList<E> array;
  23. /**
  24. * Construct a new empty binary heap.
  25. */
  26. public BinaryHeap() {
  27. this.currentSize = 0;
  28. this.array = new ArrayList<E>();
  29. }
  30. /**
  31. * Construct a copy of the given heap.
  32. *
  33. * @param heap Binary heap to copy.
  34. */
  35. public BinaryHeap(BinaryHeap<E> heap) {
  36. this.currentSize = heap.currentSize;
  37. this.array = new ArrayList<E>(heap.array);
  38. }
  39. /**
  40. * Set an element at the given index.
  41. *
  42. * @param index Index at which the element should be set.
  43. * @param value Element to set.
  44. */
  45. private void arraySet(int index, E value) {
  46. if (index == this.array.size()) {
  47. this.array.add(value);
  48. }
  49. else {
  50. this.array.set(index, value);
  51. }
  52. }
  53. /**
  54. * @return Index of the parent of the given index.
  55. */
  56. private int index_parent(int index) {
  57. return (index - 1) / 2;
  58. }
  59. /**
  60. * @return Index of the left child of the given index.
  61. */
  62. private int index_left(int index) {
  63. return index * 2 + 1;
  64. }
  65. /**
  66. * Internal method to percolate up in the heap.
  67. *
  68. * @param index Index at which the percolate begins.
  69. */
  70. private void percolateUp(int index) {
  71. E x = this.array.get(index);
  72. for (; index > 0
  73. && x.compareTo(this.array.get(index_parent(index))) < 0; index = index_parent(
  74. index)) {
  75. E moving_val = this.array.get(index_parent(index));
  76. this.arraySet(index, moving_val);
  77. }
  78. this.arraySet(index, x);
  79. }
  80. /**
  81. * Internal method to percolate down in the heap.
  82. *
  83. * @param index Index at which the percolate begins.
  84. */
  85. private void percolateDown(int index) {
  86. int ileft = index_left(index);
  87. int iright = ileft + 1;
  88. if (ileft < this.currentSize) {
  89. E current = this.array.get(index);
  90. E left = this.array.get(ileft);
  91. boolean hasRight = iright < this.currentSize;
  92. E right = (hasRight) ? this.array.get(iright) : null;
  93. if (!hasRight || left.compareTo(right) < 0) {
  94. // Left is smaller
  95. if (left.compareTo(current) < 0) {
  96. this.arraySet(index, left);
  97. this.arraySet(ileft, current);
  98. this.percolateDown(ileft);
  99. }
  100. }
  101. else {
  102. // Right is smaller
  103. if (right.compareTo(current) < 0) {
  104. this.arraySet(index, right);
  105. this.arraySet(iright, current);
  106. this.percolateDown(iright);
  107. }
  108. }
  109. }
  110. }
  111. @Override
  112. public boolean isEmpty() {
  113. return this.currentSize == 0;
  114. }
  115. @Override
  116. public int size() {
  117. return this.currentSize;
  118. }
  119. @Override
  120. public void insert(E x) {
  121. int index = this.currentSize++;
  122. this.arraySet(index, x);
  123. this.percolateUp(index);
  124. }
  125. @Override
  126. public void remove(E x) throws ElementNotFoundException {
  127. // TODO:
  128. }
  129. @Override
  130. public E findMin() throws EmptyPriorityQueueException {
  131. if (isEmpty())
  132. throw new RuntimeException("Empty binary heap.");
  133. return this.array.get(0);
  134. }
  135. @Override
  136. public E deleteMin() throws EmptyPriorityQueueException {
  137. E minItem = findMin();
  138. E lastItem = this.array.get(--this.currentSize);
  139. this.arraySet(0, lastItem);
  140. this.percolateDown(0);
  141. return minItem;
  142. }
  143. /**
  144. * Prints the heap
  145. */
  146. public void print() {
  147. System.out.println();
  148. System.out.println("======== HEAP (size = " + this.currentSize + ") ========");
  149. System.out.println();
  150. for (int i = 0; i < this.currentSize; i++) {
  151. System.out.println(this.array.get(i).toString());
  152. }
  153. System.out.println();
  154. System.out.println("-------- End of heap --------");
  155. System.out.println();
  156. }
  157. /**
  158. * Prints the elements of the heap according to their respective order.
  159. */
  160. public void printSorted() {
  161. BinaryHeap<E> copy = new BinaryHeap<E>(this);
  162. System.out.println();
  163. System.out.println("======== Sorted HEAP (size = " + this.currentSize + ") ========");
  164. System.out.println();
  165. while (!copy.isEmpty()) {
  166. System.out.println(copy.deleteMin());
  167. }
  168. System.out.println();
  169. System.out.println("-------- End of heap --------");
  170. System.out.println();
  171. }
  172. }