Guest User

Untitled

a guest
Apr 20th, 2018
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.99 KB | None | 0 0
  1. import java.util.*;
  2.  
  3.  
  4. public class MinHeap
  5. {
  6. int[] nulldata = {0, 0};
  7. // public MinHeap()
  8. // {
  9. // data = new ArrayList<int[]>();
  10. // data.add(null);
  11. // }
  12.  
  13. public MinHeap(int[] newconstruction)
  14. {
  15. data = new ArrayList<int[]>();
  16. data.add(nulldata);
  17. data.add(newconstruction);
  18. //System.out.println(data.size());
  19. }
  20.  
  21.  
  22. public void add(int[] newdata)
  23. {
  24. // Add a new leaf
  25. data.add(null);
  26. int index = data.size()-1;
  27.  
  28. // Demote parents that are larger than the new element
  29. while (index > 1
  30. && getParent(index) > (newdata[0]))
  31. {
  32. data.set(index, getParentdeep(index));
  33. index = getParentIndex(index);
  34. }
  35.  
  36. // Store the new element into the vacant slot
  37. data.set(index, newdata.clone());
  38. }
  39.  
  40.  
  41. public int[] peek()
  42. {
  43. return data.get(1);
  44. }
  45.  
  46.  
  47. public int[] remove()
  48. {
  49. int[] minimum;
  50.  
  51. // for (int k = 0; k < data.get(0).length; k++){
  52. //
  53. // System.out.printf("%d, ", data.get(0)[k]);
  54. //
  55. // }
  56. // System.out.println("");
  57.  
  58. // for (int k = 0; k < data.get(1).length; k++){
  59. //
  60. // System.out.printf("%d, ", data.get(1)[k]);
  61. //
  62. // }
  63. // System.out.println("");
  64.  
  65. // System.out.println(data.get(1)[data.get(1).length-1]);
  66. // System.out.println(data.size());
  67.  
  68. minimum = data.get(1);
  69.  
  70. // Remove last element
  71. int lastIndex = data.size() - 1;
  72. int[] last = data.remove(lastIndex);
  73.  
  74. if (lastIndex > 1)
  75. {
  76. data.set(1, last);
  77. fixHeap();
  78. }
  79.  
  80. return minimum;
  81. }
  82.  
  83.  
  84. private void fixHeap()
  85. {
  86. int[] root = data.get(1);
  87.  
  88. int lastIndex = data.size() - 1;
  89. // Promote children of removed root while they are larger than last
  90.  
  91. int index = 1;
  92. boolean more = true;
  93. while (more)
  94. {
  95. int childIndex = getLeftChildIndex(index);
  96. if (childIndex <= lastIndex)
  97. {
  98. // Get smaller child
  99.  
  100. // Get left child first
  101. int[] child = getLeftChilddeep(index);
  102.  
  103. // Use right child instead if it is smaller
  104. if (getRightChildIndex(index) <= lastIndex
  105. && getRightChilddeep(index)[0] < child[0])
  106. {
  107. childIndex = getRightChildIndex(index);
  108. child = getRightChilddeep(index);
  109. }
  110.  
  111. // Check if larger child is smaller than root
  112. if (child[0] < root[0])
  113. {
  114. // Promote child
  115. data.set(index, child);
  116. index = childIndex;
  117. }
  118. else
  119. {
  120. // Root is smaller than both children
  121. more = false;
  122. }
  123. }
  124. else
  125. {
  126. // No children
  127. more = false;
  128. }
  129. }
  130.  
  131. // Store root element in vacant slot
  132. data.set(index, root);
  133. }
  134.  
  135.  
  136. public int size()
  137. {
  138. return data.size() -1;
  139. }
  140.  
  141.  
  142. private static int getLeftChildIndex(int index)
  143. {
  144. return 2 * index;
  145. }
  146.  
  147.  
  148. private static int getRightChildIndex(int index)
  149. {
  150. return 2 * index + 1;
  151. }
  152.  
  153.  
  154. private static int getParentIndex(int index)
  155. {
  156. return index / 2;
  157. }
  158.  
  159.  
  160. // private int getLeftChild(int index)
  161. // {
  162. // return data.get(2 * index)[1];
  163. // }
  164. //
  165. //
  166. // private int getRightChild(int index)
  167. // {
  168. // return data.get(2 * index + 1)[1];
  169. // }
  170.  
  171. private int getParent(int index)
  172. {
  173. return data.get(index / 2)[0];
  174. }
  175.  
  176. private int[] getLeftChilddeep(int index)
  177. {
  178. return data.get(2 * index).clone();
  179. }
  180.  
  181.  
  182. private int[] getRightChilddeep(int index)
  183. {
  184. return data.get(2 * index + 1).clone();
  185. }
  186.  
  187. private int[] getParentdeep(int index)
  188. {
  189. return data.get(index / 2).clone();
  190. }
  191.  
  192. private ArrayList<int[]> data;
  193. }
Add Comment
Please, Sign In to add comment