Advertisement
Guest User

Untitled

a guest
Feb 10th, 2016
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.80 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. }
  16.  
  17. /**
  18. * Construct the binary heap.
  19. *
  20. * @param capacity the capacity of the binary heap.
  21. */
  22. // public DHeap( int capacity )
  23. // {
  24. // currentSize = 0;
  25. // array = (AnyType[]) new Comparable[ capacity + 1 ];
  26. // }
  27. public DHeap(int d) {
  28. if (d > 1) {
  29. this.children = d;
  30. } else {
  31. throw new IllegalArgumentException();
  32. }
  33. array = (AnyType[]) new Comparable[d];
  34. }
  35.  
  36. /**
  37. * Construct the binary heap given an array of items.
  38. */
  39. public DHeap(AnyType[] items) {
  40. currentSize = items.length;
  41. array = (AnyType[]) new Comparable[(currentSize + 2) * 11 / 10];
  42.  
  43. int i = 1;
  44. for (AnyType item : items) {
  45. array[ i++] = item;
  46. }
  47. buildHeap();
  48. }
  49.  
  50. /**
  51. * Insert into the priority queue, maintaining heap order. Duplicates are
  52. * allowed.
  53. *
  54. * @param x the item to insert.
  55. */
  56. //om mindre än parent perculate annars lägg till som child
  57. //lägg till children tills children= max nr av children i ett subträd
  58. public void insert(AnyType x) {
  59. if (currentSize == array.length - 1) {
  60. enlargeArray(array.length * children+1);
  61. }
  62. // Percolate up
  63. //currentSize++ OCH hole = currentSize+1
  64. int hole = ++currentSize;
  65. for (array[ 0] = x; hole > 1 && x.compareTo(array[ parentIndex(hole)]) < 0; hole = parentIndex(hole)) {
  66. //System.out.println("current hole" + hole);
  67. //System.out.println("divided by" + (parentIndex(hole)));
  68. //System.out.println("is" + (hole / parentIndex(hole)));
  69. System.out.println(x.compareTo(array[ parentIndex(hole)]));
  70.  
  71. array[ hole] = array[ parentIndex(hole)];
  72. }
  73. array[ hole] = x;
  74. }
  75.  
  76. private void enlargeArray(int newSize) {
  77. AnyType[] old = array;
  78. array = (AnyType[]) new Comparable[newSize];
  79. for (int i = 0; i < old.length; i++) {
  80. array[ i] = old[ i];
  81. }
  82. }
  83.  
  84. /**
  85. * Find the smallest item in the priority queue.
  86. *
  87. * @return the smallest item, or throw an UnderflowException if empty.
  88. */
  89. public AnyType findMin() {
  90. if (isEmpty()) {
  91. throw new UnderflowException();
  92. }
  93. return array[ 1];
  94. }
  95.  
  96. /**
  97. * Remove the smallest item from the priority queue.
  98. *
  99. * @return the smallest item, or throw an UnderflowException if empty.
  100. */
  101. public AnyType deleteMin() {
  102. if (isEmpty()) {
  103. throw new UnderflowException();
  104. }
  105.  
  106. AnyType minItem = findMin();
  107. System.out.println("nowsize" + size());
  108. array[ 1] = array[ currentSize--];
  109. System.out.println("aftersize" + size());
  110. percolateDown(1);
  111.  
  112.  
  113. return minItem;
  114. }
  115.  
  116. /**
  117. * Establish heap order property from an arbitrary arrangement of items.
  118. * Runs in linear time.
  119. */
  120. private void buildHeap() {
  121. for (int i = currentSize / 2; i > 0; i--) {
  122. percolateDown(i);
  123. }
  124. }
  125.  
  126. /**
  127. * Test if the priority queue is logically empty.
  128. *
  129. * @return true if empty, false otherwise.
  130. */
  131. public int size() {
  132. return currentSize;
  133. }
  134.  
  135. public boolean isEmpty() {
  136. return currentSize == 0;
  137. }
  138.  
  139. /**
  140. * Make the priority queue logically empty.
  141. */
  142. public void makeEmpty() {
  143. currentSize = 0;
  144. }
  145.  
  146. /**
  147. * Internal method to percolate down in the heap.
  148. *
  149. * @param hole the index at which the percolate begins.
  150. */
  151. private void percolateDown(int hole) {
  152. int child = 0;
  153. AnyType tmp = array[ hole];
  154. System.out.println("tmp"+tmp);
  155.  
  156. // firstchildindex istället för hole * children då hole * 2 är 2i, alltså formeln för vänstra barnet i en binär heap
  157. for (; firstChildIndex(hole) <= currentSize; hole = child) {
  158. System.out.println("new iter");
  159. child = firstChildIndex(hole);
  160. //eftersom det är många barn måste vi ha en nestlad for loop, i d=2 är ju ett barn alltid större eller
  161. //mindre än ett annat, i vårt fall måste vi hitta det minsta barnet (som kan vara d många)
  162. for (int i = 0; i < children; i++) {
  163. if (child != currentSize+1 && array[ child + 1].compareTo(array[ child]) < 0) {
  164. System.out.println(" value of child+1 (" + array[child + 1] + ") is less than value of child (" + array[child] + ")");
  165. child++;
  166. }
  167. }
  168. System.out.println("child after checking: "+child);
  169. System.out.println("child value after checking: "+array[child]);
  170. //problemet jag är på nu är att jag inte förstår hur det värdet vi ska ta bort "försvinner".
  171. //vi placerar ju om värdena så att det första värdet är helt borta, men när vi når det allra sista värdet
  172. //vi ändrar på måste det ju tas bort, annars får man samma värde två gånger
  173. //det finns ingen ta bort metod i arrays, och man kan inte sätta det till null och man kan heller inte
  174. //bara ändra length hos arrayen eftersom när vi insertar lägger vi till en hel rad med nya platser i arrayen
  175. //MEN det blir exakt samma grej i BinaryHeap som vi fick av boken,
  176. //så det kanske inte ska tas bort ett värde, men då undrar jag hur det blir när man sedan ska lägga till värden
  177. //när det ligger ett spökvärde där samt det stämmer inte överrens med föreläsningsbilderna när träd illustrerades
  178. //att värdet ligger kvar
  179. if (array[ child].compareTo(tmp) < 0) {
  180. System.out.println("value of child(" + array[child] + ") is less than value of temp(" + tmp + ")");
  181. array[ hole] = array[ child];
  182. System.out.println("hole:" + array[hole]);
  183. } else {
  184. break;
  185. }
  186. }
  187. array[ hole] = tmp;
  188. System.out.println("size"+array.length);
  189. System.out.println("current size value"+array[currentSize]);
  190. }
  191.  
  192. //generiska get-metoder för att hitta first child och parent
  193. public int firstChildIndex(int parent) {
  194. if (parent == 0) {
  195. throw new IllegalArgumentException();
  196. }
  197. return parent * children - (children - 2);
  198. //return 2 + (children *(parent-1));
  199. }
  200.  
  201. public int parentIndex(int child) {
  202. if (child <= 1) {
  203. throw new IllegalArgumentException();
  204. }
  205. //return (child - 2) / children + 1;
  206. return (child + children - 2) / children;
  207. }
  208.  
  209. public AnyType get(int i) {
  210. return array[i];
  211. }
  212.  
  213. // public AnyType getIndex(int i){
  214. // AnyType yeah = array[i];
  215. // return yeah;
  216. // }
  217. // Test program
  218. // public static void main( String [ ] args )
  219. // {
  220. // int numItems = 10000;
  221. // BinaryHeap<Integer> h = new BinaryHeap<>( );
  222. // int i = 37;
  223. //
  224. // for( i = 37; i != 0; i = ( i + 37 ) % numItems )
  225. // h.insert( i );
  226. // for( i = 1; i < numItems; i++ )
  227. // if( h.deleteMin( ) != i )
  228. // System.out.println( "Oops! " + i );
  229. // }
  230. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement