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.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. package org.insa.graphs.algorithm.utils;
  2. import java.util.ArrayList;
  3. /**
  4. * Implements a binary heap containing elements of type E.
  5. *
  6. * Note that all comparisons are based on the compareTo method, hence E must
  7. * implement Comparable
  8. *
  9. * @author Mark Allen Weiss
  10. * @author DLB
  11. */
  12. public class BinaryHeap<E extends Comparable<E>> implements PriorityQueue<E> {
  13. // Number of elements in heap.
  14. private int currentSize;
  15. // The heap array.
  16. protected final ArrayList<E> array;
  17. /**
  18. * Construct a new empty binary heap.
  19. */
  20. public BinaryHeap() {
  21. this.currentSize = 0;
  22. this.array = new ArrayList<E>();
  23. }
  24. /**
  25. * Construct a copy of the given heap.
  26. *
  27. * @param heap Binary heap to copy.
  28. */
  29. public BinaryHeap(BinaryHeap<E> heap) {
  30. this.currentSize = heap.currentSize;
  31. this.array = new ArrayList<E>(heap.array);
  32. }
  33. /**
  34. * Set an element at the given index.
  35. *
  36. * @param index Index at which the element should be set.
  37. * @param value Element to set.
  38. */
  39. private void arraySet(int index, E value) {
  40. if (index == this.array.size()) {
  41. this.array.add(value);
  42. }
  43. else {
  44. this.array.set(index, value);
  45. }
  46. }
  47. /**
  48. * @return Index of the parent of the given index.
  49. */
  50. protected int indexParent(int index) {
  51. return (index - 1) / 2;
  52. }
  53. /**
  54. * @return Index of the left child of the given index.
  55. */
  56. protected int indexLeft(int index) {
  57. return index * 2 + 1;
  58. }
  59. /**
  60. * Internal method to percolate up in the heap.
  61. *
  62. * @param index Index at which the percolate begins.
  63. */
  64. private void percolateUp(int index) {
  65. E x = this.array.get(index);
  66. for (; index > 0
  67. && x.compareTo(this.array.get(indexParent(index))) < 0; index = indexParent(
  68. index)) {
  69. E moving_val = this.array.get(indexParent(index));
  70. this.arraySet(index, moving_val);
  71. }
  72. this.arraySet(index, x);
  73. }
  74. /**
  75. * Internal method to percolate down in the heap.
  76. *
  77. * @param index Index at which the percolate begins.
  78. */
  79. private void percolateDown(int index) {
  80. int ileft = indexLeft(index);
  81. int iright = ileft + 1;
  82. if (ileft < this.currentSize) {
  83. E current = this.array.get(index);
  84. E left = this.array.get(ileft);
  85. boolean hasRight = iright < this.currentSize;
  86. E right = (hasRight) ? this.array.get(iright) : null;
  87. if (!hasRight || left.compareTo(right) < 0) {
  88. // Left is smaller
  89. if (left.compareTo(current) < 0) {
  90. this.arraySet(index, left);
  91. this.arraySet(ileft, current);
  92. this.percolateDown(ileft);
  93. }
  94. }
  95. else {
  96. // Right is smaller
  97. if (right.compareTo(current) < 0) {
  98. this.arraySet(index, right);
  99. this.arraySet(iright, current);
  100. this.percolateDown(iright);
  101. }
  102. }
  103. }
  104. }
  105. @Override
  106. public boolean isEmpty() {
  107. return this.currentSize == 0;
  108. }
  109. @Override
  110. public int size() {
  111. return this.currentSize;
  112. }
  113. @Override
  114. public void insert(E x) {
  115. int index = this.currentSize++;
  116. this.arraySet(index, x);
  117. this.percolateUp(index);
  118. }
  119. @Override
  120. public void remove(E x) throws ElementNotFoundException {
  121. int index=this.array.indexOf(x);
  122. if (index<0 || this.currentSize<=index)
  123. throw new ElementNotFoundException(x);
  124. this.array.set(index, this.array.get(this.currentSize-1));
  125. this.currentSize--;
  126. this.percolateDown(index);
  127. this.percolateUp(index);
  128. }
  129. @Override
  130. public E findMin() throws EmptyPriorityQueueException {
  131. if (isEmpty())
  132. throw new EmptyPriorityQueueException();
  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. * Creates a multi-lines string representing a sorted view of this binary heap.
  145. *
  146. * @return a string containing a sorted view this binary heap.
  147. */
  148. public String toStringSorted() {
  149. return BinaryHeapFormatter.toStringSorted(this, -1);
  150. }
  151. /**
  152. * Creates a multi-lines string representing a sorted view of this binary heap.
  153. *
  154. * @param maxElement Maximum number of elements to display. or {@code -1} to
  155. * display all the elements.
  156. *
  157. * @return a string containing a sorted view this binary heap.
  158. */
  159. public String toStringSorted(int maxElement) {
  160. return BinaryHeapFormatter.toStringSorted(this, maxElement);
  161. }
  162. /**
  163. * Creates a multi-lines string representing a tree view of this binary heap.
  164. *
  165. * @return a string containing a tree view of this binary heap.
  166. */
  167. public String toStringTree() {
  168. return BinaryHeapFormatter.toStringTree(this, Integer.MAX_VALUE);
  169. }
  170. /**
  171. * Creates a multi-lines string representing a tree view of this binary heap.
  172. *
  173. * @param maxDepth Maximum depth of the tree to display.
  174. *
  175. * @return a string containing a tree view of this binary heap.
  176. */
  177. public String toStringTree(int maxDepth) {
  178. return BinaryHeapFormatter.toStringTree(this, maxDepth);
  179. }
  180. @Override
  181. public String toString() {
  182. return BinaryHeapFormatter.toStringTree(this, 8);
  183. }
  184. }