Advertisement
Guest User

Untitled

a guest
Nov 14th, 2019
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.32 KB | None | 0 0
  1. package Question2;
  2.  
  3. import java.util.ArrayList;
  4.  
  5. /**
  6. * Heap class to store any type of objects and their associated priorities.
  7. */
  8. public class ArrayHeap<T> {
  9.  
  10. private ArrayList<Node> contents; ///An ArrayList to store nodes
  11.  
  12. /*
  13. * Constructor to initializes an empty ArrayHeap.
  14. */
  15. public ArrayHeap() {
  16. contents = new ArrayList<>();
  17. contents.add(null);
  18. }
  19.  
  20. /*
  21. * Returns the index of the left child node
  22. */
  23. private int getLeft(int i) {
  24. return i * 2;
  25. }
  26.  
  27. /*
  28. * Returns the index of the right clild node
  29. */
  30. private int getRight(int i) {
  31. return 2 * i + 1;
  32. }
  33.  
  34. /*
  35. * Returns the index of the parent node
  36. */
  37. private int getParent(int i) {
  38. return i / 2;
  39. }
  40.  
  41. /**
  42. * Returns the index of the node with smaller priority. Assumption: not both
  43. * nodes are null.
  44. */
  45. private int min(int index1, int index2) {
  46. Node node1 = getNode(index1);
  47. Node node2 = getNode(index2);
  48. if (node1 == null) {
  49. return index2;
  50. } else if (node2 == null) {
  51. return index1;
  52. } else if (node1.getPriority() < node2.getPriority()) {
  53. return index1;
  54. } else {
  55. return index2;
  56. }
  57. }
  58.  
  59. /*
  60. * Returns the Node with the smallest priority value without removing it
  61. */
  62. public Node peek() {
  63. return contents.get(1);
  64. }
  65.  
  66. /*
  67. * Bubbles up the node currently at the given index.
  68. */
  69. private void bubbleUp(int index) {
  70. // YOUR CODE HERE// completed
  71. while (index > 1 && min(index, getParent(index)) == index) {
  72. swap(index, getParent(index));
  73. index = getParent(index);
  74. }
  75. }
  76.  
  77. /*
  78. * Bubbles down the node currently at the given index.
  79. */
  80. private void bubbleDown(int index) {
  81. // YOUR CODE HERE// completed
  82. if (index == contents.size()) {
  83. return;
  84. }
  85. int minI = min(index, min(getLeft(index), getRight(index)));
  86. if (index != minI) {
  87. swap(index, minI);
  88. bubbleDown(minI);
  89. }
  90. }
  91.  
  92. /*
  93. * Inserts an item to the heap with a given priority value
  94. */
  95. public void insert(T item, double priority) {
  96. // YOUR CODE HERE // completed
  97. if (contents.size() == 0) {
  98. contents.add(null);
  99. } else {
  100. contents.add(new Node(item, priority));
  101. bubbleUp(contents.size() - 1);
  102. }
  103.  
  104. }
  105.  
  106. /*
  107. * Returns the Node with the smallest priority value,
  108. * and removes it from the heap.
  109. */
  110. public Node removeMin() {
  111. // YOUR CODE HERE// completed
  112. if (contents.size() < 2) {
  113. return null;
  114. } else if (contents.size() == 2) {
  115. return contents.remove(1);
  116. } else {
  117. Node minVal = contents.get(1);
  118. Node maxVal = contents.remove(contents.size() - 1);
  119. contents.set(1, maxVal);
  120. bubbleDown(1);
  121. return minVal;
  122. }
  123.  
  124. }
  125.  
  126. /*
  127. * Update a node in the heap (that matches with the given item) to have the given priority.
  128. * Assumption: we will not have two nodes with the same item.
  129. * Suggestions: Check for equality of items with .equals(), not ==
  130. */
  131. public void changePriority(T item, double priority) {
  132. // YOUR CODE HERE
  133.  
  134. for (int i = 1; i < contents.size(); i++) {
  135. if (getNode(i).getItem().equals(item)) {
  136. double old = getNode(i).getPriority();
  137. getNode(i).setPriority(priority);
  138. if (old < priority) {
  139. bubbleDown(i);
  140. } else {
  141. bubbleUp(i);
  142. }
  143. }
  144. break;
  145. }
  146.  
  147. }
  148.  
  149. /*
  150. * Returns the node at index INDEX.
  151. */
  152. private Node getNode(int index) {
  153. if (index >= contents.size()) {
  154. return null;
  155. } else {
  156. return contents.get(index);
  157. }
  158. }
  159.  
  160. /*
  161. * Swap the nodes at the two indices.
  162. */
  163. private void swap(int index1, int index2) {
  164. Node node1 = getNode(index1);
  165. Node node2 = getNode(index2);
  166. this.contents.set(index1, node2);
  167. this.contents.set(index2, node1);
  168. }
  169.  
  170. int height(int index) {
  171. int result = 0;
  172. while (index > 0) {
  173. result++;
  174. index /= 2;
  175. }
  176. return result;
  177. }
  178.  
  179.  
  180. /*
  181. * Prints out the heap sideways.
  182. * We use this method for debugging.
  183. */
  184. @Override
  185. public String toString() {
  186. return customToStringHelper();
  187. }
  188.  
  189. private String customToStringHelper() {
  190. String ret = "";
  191.  
  192. boolean[] visited = new boolean[contents.size() + 1];
  193. for (int i = 0; i <= contents.size(); i++) {
  194. visited[i] = false;
  195. }
  196.  
  197. for (int i = 1, n = contents.size(); i < n; i++) {
  198. int h = height(i);
  199. if (!visited[h]) {
  200. ret += "\n";
  201. visited[h] = true;
  202. }
  203.  
  204. if (i % 2 == 0 && i != 1) {
  205. ret += "(";
  206. }
  207. if (i % 2 == 1 && i != 1) {
  208. ret += ", ";
  209. }
  210. ret += contents.get(i);
  211. if ((i % 2 == 1 && i != 1) || i == (n - 1)) {
  212. ret += ")";
  213. }
  214. }
  215.  
  216. return ret;
  217. }
  218.  
  219. /*
  220. * Recursive helper method for toString.
  221. */
  222. private String toStringHelper(int index, String soFar) {
  223. if (getNode(index) == null) {
  224. return "";
  225. } else {
  226. String toReturn = "";
  227. int rightChild = getRight(index);
  228. toReturn += toStringHelper(rightChild, " " + soFar);
  229. if (getNode(rightChild) != null) {
  230. toReturn += soFar + " /";
  231. }
  232. toReturn += "\n" + soFar + getNode(index) + "\n";
  233. int leftChild = getLeft(index);
  234. if (getNode(leftChild) != null) {
  235. toReturn += soFar + " \\";
  236. }
  237. toReturn += toStringHelper(leftChild, " " + soFar);
  238. return toReturn;
  239. }
  240. }
  241.  
  242. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement