Advertisement
Guest User

Untitled

a guest
Jan 21st, 2019
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.38 KB | None | 0 0
  1. /*
  2. * To change this license header, choose License Headers in Project Properties.
  3. * To change this template file, choose Tools | Templates
  4. * and open the template in the editor.
  5. */
  6. package Priority_queue;
  7.  
  8. import java.util.ArrayList;
  9. import java.util.Comparator;
  10.  
  11. /**
  12. * An implementation of a priority queue using an array-based heap.
  13. *
  14. * @author Michael T. Goodrich
  15. * @author Roberto Tamassia
  16. * @author Michael H. Goldwasser
  17. *
  18. * @author antoniosilva
  19. *
  20. */
  21. public class HeapPriorityQueue<K, V> extends AbstractPriorityQueue<K, V> {
  22.  
  23. /**
  24. * primary collection of priority queue entries
  25. */
  26. protected ArrayList<Entry<K, V>> heap = new ArrayList<>();
  27.  
  28. /**
  29. * Creates an empty priority queue based on the natural ordering of its
  30. * keys.
  31. */
  32. public HeapPriorityQueue() {
  33. super();
  34. }
  35.  
  36. /**
  37. * Creates an empty priority queue using the given comparator to order keys.
  38. *
  39. * @param comp comparator defining the order of keys in the priority queue
  40. */
  41. public HeapPriorityQueue(Comparator<K> comp) {
  42. super(comp);
  43. }
  44.  
  45. /**
  46. * Creates a priority queue initialized with the respective key-value pairs.
  47. * The two arrays given will be paired element-by-element. They are presumed
  48. * to have the same length. (If not, entries will be created only up to the
  49. * length of the shorter of the arrays)
  50. *
  51. * @param keys an array of the initial keys for the priority queue
  52. * @param values an array of the initial values for the priority queue
  53. */
  54. public HeapPriorityQueue(K[] keys, V[] values) {
  55. super();
  56. for (int j = 0; j < Math.min(keys.length, values.length); j++) {
  57. heap.add(new PQEntry<>(keys[j], values[j]));
  58. }
  59. buildHeap();
  60. }
  61.  
  62. // protected utilities
  63. protected int parent(int j) {
  64. return (j - 1) / 2;
  65. } // truncating division
  66.  
  67. protected int left(int j) {
  68. return 2 * j + 1;
  69. }
  70.  
  71. protected int right(int j) {
  72. return 2 * j + 2;
  73. }
  74.  
  75. protected boolean hasLeft(int j) {
  76. return left(j) < heap.size();
  77. }
  78.  
  79. protected boolean hasRight(int j) {
  80. return right(j) < heap.size();
  81. }
  82.  
  83. /**
  84. * Exchanges the entries at indices i and j of the array list.
  85. */
  86. protected void swap(int i, int j) {
  87. Entry<K, V> temp = heap.get(i);
  88. heap.set(i, heap.get(j));
  89. heap.set(j, temp);
  90. }
  91.  
  92. /**
  93. * Moves the entry at index j higher, if necessary, to restore the heap
  94. * property.
  95. */
  96. protected void percolateUp(int j) {
  97. int index = parent(j);
  98. while (index != 0 && compare(heap.get(j), heap.get(index)) < 0) {
  99. swap(j, index);
  100. j = index;
  101. index = parent(j);
  102. }
  103. }
  104.  
  105. /**
  106. * Moves the entry at index j lower, if necessary, to restore the heap
  107. * property.
  108. */
  109. protected void percolateDown(int i) {
  110. int indLeft = 2 * i + 1;
  111. int indRight = 2 * i + 2;
  112. boolean swaps = true;
  113. while (indLeft < heap.size() && swaps) {
  114. int smallindex = indLeft;
  115. if (indRight < heap.size()) {
  116. if (compare(heap.get(indRight), heap.get(indLeft)) < 0) {
  117. smallindex = indRight;
  118. }
  119. }
  120. if (compare(heap.get(i), heap.get(smallindex)) > 0) {
  121. swap(i, smallindex); //change the elem by the
  122. i = smallindex; //child with the highest
  123. indLeft = 2 * i + 1;
  124. indRight = 2 * i + 2;
  125. } else {
  126. swaps = false;
  127. }
  128. }
  129. }
  130.  
  131. /**
  132. * Performs a batch bottom-up construction of the heap in O(n) time.
  133. */
  134. protected void buildHeap() {
  135. throw new UnsupportedOperationException("Not supported yet.");
  136. }
  137.  
  138. // public methods
  139. /**
  140. * Returns the number of items in the priority queue.
  141. *
  142. * @return number of items
  143. */
  144. @Override
  145. public int size() {
  146. return heap.size();
  147. }
  148.  
  149. /**
  150. * Returns (but does not remove) an entry with minimal key.
  151. *
  152. * @return entry having a minimal key (or null if empty)
  153. */
  154. @Override
  155. public Entry<K, V> min() {
  156. return heap.get(heap.size() - 1);
  157. }
  158.  
  159. /**
  160. * Inserts a key-value pair and return the entry created.
  161. *
  162. * @param key the key of the new entry
  163. * @param value the associated value of the new entry
  164. * @return the entry storing the new key-value pair
  165. * @throws IllegalArgumentException if the key is unacceptable for this
  166. * queue
  167. */
  168. @Override
  169. public Entry<K, V> insert(K key, V value) throws IllegalArgumentException {
  170. PQEntry<K, V> temp = new PQEntry<>(key, value);
  171. heap.add(temp);
  172. percolateUp(heap.size() - 1);
  173. return temp;
  174. }
  175.  
  176. /**
  177. * Removes and returns an entry with minimal key.
  178. *
  179. * @return the removed entry (or null if empty)
  180. */
  181. @Override
  182. public Entry<K, V> removeMin() {
  183. swap(0, heap.size() - 1);
  184. Entry<K, V> e = heap.remove(heap.size() - 1);
  185. percolateDown(0);
  186. return e;
  187. }
  188.  
  189. public ArrayList<Entry<K, V>> getHeap() {
  190. return heap;
  191. }
  192.  
  193. @Override
  194. public String toString() {
  195. System.out.println("flag");
  196. for (Entry<K, V> entry : heap) {
  197. System.out.println(entry.getKey() + "entry: " + entry.getValue() + "\n");
  198. }
  199. return "";
  200. }
  201.  
  202. public void sort(ArrayList<V> vector) {
  203. int size = heap.size();
  204. for (int i = 0; i < size; i++) {
  205. vector.add(i, removeMin().getValue());
  206. }
  207. }
  208.  
  209. @Override
  210. public HeapPriorityQueue<K, V> clone() {
  211. HeapPriorityQueue<K, V> heapAux = new HeapPriorityQueue<>();
  212. for (Entry<K, V> entry : heap) {
  213. heapAux.insert(entry.getKey(), entry.getValue());
  214. }
  215. return heapAux;
  216. }
  217.  
  218. public HeapPriorityQueue<K, V> newHeap(HeapPriorityQueue<K, V> heap2, K value) {
  219. int i = 0;
  220. for (Entry<K, V> entry : this.heap) {
  221. if(!entry.getKey().equals(value)) {
  222. heap2.insert(entry.getKey(), entry.getValue());
  223. }
  224. }
  225. return heap2;
  226. }
  227.  
  228. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement