Advertisement
Guest User

Untitled

a guest
Feb 11th, 2016
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.53 KB | None | 0 0
  1. package alda.dheap;
  2.  
  3. public class DHeap<AnyType extends Comparable<? super AnyType>> {
  4.  
  5. /**
  6. * Construct the binary heap.
  7. */
  8. int children; //Number of children allowed (d)
  9. private static final int DEFAULT_CAPACITY = 10;
  10. private int currentSize; // Number of elements in heap
  11. private AnyType[] array; // The heap array
  12.  
  13. public DHeap() {
  14. this(DEFAULT_CAPACITY);
  15. this.children = 2;
  16. }
  17.  
  18. /**
  19. * Construct the binary heap.
  20. *
  21. * @param capacity the capacity of the binary heap.
  22. */
  23. // public DHeap( int capacity )
  24. // {
  25. // currentSize = 0;
  26. // array = (AnyType[]) new Comparable[ capacity + 1 ];
  27. // }
  28. public DHeap(int d) {
  29. if (d > 1) {
  30. this.children = d;
  31. } else {
  32. throw new IllegalArgumentException();
  33. }
  34. array = (AnyType[]) new Comparable[d];
  35. }
  36.  
  37. /**
  38. * Construct the binary heap given an array of items.
  39. */
  40. public DHeap(AnyType[] items) {
  41. currentSize = items.length;
  42. array = (AnyType[]) new Comparable[(currentSize + 2) * 11 / 10];
  43.  
  44. int i = 1;
  45. for (AnyType item : items) {
  46. array[ i++] = item;
  47. }
  48. buildHeap();
  49. }
  50.  
  51. /**
  52. * Insert into the priority queue, maintaining heap order. Duplicates are
  53. * allowed.
  54. *
  55. * @param x the item to insert.
  56. */
  57. //om mindre än parent perculate annars lägg till som child
  58. //lägg till children tills children= max nr av children i ett subträd
  59. public void insert(AnyType x) {
  60. if (currentSize == array.length - 1) {
  61. enlargeArray(array.length * children + 1);
  62. }
  63. // Percolate up
  64. int hole = ++currentSize;
  65. for (array[ 0] = x; hole > 1 && x.compareTo(array[ parentIndex(hole)]) < 0; hole = parentIndex(hole)) {
  66. array[ hole] = array[ parentIndex(hole)];
  67. }
  68. array[ hole] = x;
  69. }
  70.  
  71. private void enlargeArray(int newSize) {
  72. AnyType[] old = array;
  73. array = (AnyType[]) new Comparable[newSize];
  74. for (int i = 0; i < old.length; i++) {
  75. array[ i] = old[ i];
  76. }
  77.  
  78. }
  79.  
  80. /**
  81. * Find the smallest item in the priority queue.
  82. *
  83. * @return the smallest item, or throw an UnderflowException if empty.
  84. */
  85. public AnyType findMin() {
  86. if (isEmpty()) {
  87. throw new UnderflowException();
  88. }
  89. return array[ 1];
  90. }
  91.  
  92. /**
  93. * Remove the smallest item from the priority queue.
  94. *
  95. * @return the smallest item, or throw an UnderflowException if empty.
  96. */
  97. public AnyType deleteMin() {
  98. if (isEmpty()) {
  99. throw new UnderflowException();
  100. }
  101.  
  102. AnyType minItem = findMin();
  103. array[1] = array[ currentSize];
  104. array[currentSize] = null;
  105. currentSize--;
  106. percolateDown(1);
  107.  
  108. return minItem;
  109. }
  110.  
  111. /**
  112. * Establish heap order property from an arbitrary arrangement of items.
  113. * Runs in linear time.
  114. */
  115. private void buildHeap() {
  116. for (int i = currentSize / 2; i > 0; i--) {
  117. percolateDown(i);
  118. }
  119. }
  120.  
  121. /**
  122. * Test if the priority queue is logically empty.
  123. *
  124. * @return true if empty, false otherwise.
  125. */
  126. public int size() {
  127. return currentSize;
  128. }
  129.  
  130. public boolean isEmpty() {
  131. return currentSize == 0;
  132. }
  133.  
  134. /**
  135. * Make the priority queue logically empty.
  136. */
  137. public void makeEmpty() {
  138. currentSize = 0;
  139. }
  140.  
  141. /**
  142. * Internal method to percolate down in the heap.
  143. *
  144. * @param hole the index at which the percolate begins.
  145. */
  146. private void percolateDown(int hole) {
  147. int child;
  148. AnyType tmp = array[ hole];
  149.  
  150. for (; firstChildIndex(hole) <= currentSize; hole = child) {
  151. child = firstChildIndex(hole) + 1;
  152. int smallestChild = firstChildIndex(hole);
  153. int test = firstChildIndex(hole);
  154.  
  155. for (int i = 0; i < children; i++) {
  156. System.out.println("i "+i);
  157. System.out.println("child index" + child + " and child value "+array[child]);
  158. System.out.println("array length "+array.length);
  159. if (array[child] != null) {
  160. if (array[child] != null && array[smallestChild].compareTo(array[ child]) < 0) {
  161. child++;
  162. } else {
  163. smallestChild = child;
  164. child++;
  165. }
  166. } else if((child+1)>=array.length) {
  167. break;
  168.  
  169. } else {
  170. child++;
  171. }
  172. }
  173. child = smallestChild;
  174. if (array[child] != null && array[ child].compareTo(tmp) < 0) {
  175. array[ hole] = array[ child];
  176. } else {
  177. break;
  178. }
  179. }
  180.  
  181. array[ hole] = tmp;
  182. }
  183.  
  184. //generiska get-metoder för att hitta first child och parent
  185. public int firstChildIndex(int parent) {
  186. if (parent == 0) {
  187. throw new IllegalArgumentException();
  188. }
  189. return parent * children - (children - 2);
  190. //return 2 + (children *(parent-1));
  191. }
  192.  
  193. public int parentIndex(int child) {
  194. if (child < 2) {
  195. throw new IllegalArgumentException();
  196. }
  197. //return (child - 2) / children + 1;
  198. //return (int)Math.floor((child-1)/children);
  199. //return (child + children - 1) / children;
  200. //int tal = (int)Math.ceil((child - 1.0) / children);
  201. //System.out.println("tal wo ceil "+tal);
  202. //tal =Math.ceil(tal);
  203. //System.out.println("tal w ceil "+tal);
  204. return (int) Math.ceil((child - 1.0) / children);
  205.  
  206. }
  207.  
  208. public AnyType get(int i) {
  209. return array[i];
  210. }
  211.  
  212. // public AnyType getIndex(int i){
  213. // AnyType yeah = array[i];
  214. // return yeah;
  215. // }
  216. // Test program
  217. // public static void main( String [ ] args )
  218. // {
  219. // int numItems = 10000;
  220. // BinaryHeap<Integer> h = new BinaryHeap<>( );
  221. // int i = 37;
  222. //
  223. // for( i = 37; i != 0; i = ( i + 37 ) % numItems )
  224. // h.insert( i );
  225. // for( i = 1; i < numItems; i++ )
  226. // if( h.deleteMin( ) != i )
  227. // System.out.println( "Oops! " + i );
  228. // }
  229. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement