Advertisement
Guest User

Untitled

a guest
Feb 11th, 2016
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.78 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.  
  59. public void insert(AnyType x) {
  60.  
  61. if (currentSize == array.length - 1) {
  62. enlargeArray(array.length * children+1);
  63.  
  64.  
  65. }
  66. // Percolate up
  67. //currentSize++ OCH hole = currentSize+1
  68. int hole = ++currentSize;
  69. for (array[ 0] = x; hole >= 1 && x.compareTo(array[ parentIndex(hole)]) < 0; hole = parentIndex(hole)) {
  70. //System.out.println("current hole" + hole);
  71. System.out.println("HOLE:"+hole);
  72. //System.out.println("divided by" + (parentIndex(hole)));
  73. //System.out.println("is" + (hole / parentIndex(hole)));
  74. // System.out.println(x.compareTo(array[ parentIndex(hole)]));
  75. array[ hole] = array[ parentIndex(hole)];
  76. }
  77. array[ hole] = x;
  78.  
  79. }
  80.  
  81. private void enlargeArray(int newSize) {
  82.  
  83. AnyType[] old = array;
  84. array = (AnyType[]) new Comparable[newSize];
  85. for (int i = 0; i < old.length; i++) {
  86. array[ i] = old[ i];
  87. }
  88.  
  89. }
  90. // private void decreaseArray(int newSize) {
  91. // System.out.println("h"+array.length);
  92. // System.out.println("h"+newSize);
  93. // AnyType[] old = array;
  94. // array = (AnyType[]) new Comparable[newSize];
  95. // for (int i = 0; i < old.length&&i<array.length; i++) {
  96. // array[ i] = old[ i];
  97. // }
  98. // System.out.println("h"+array.length);
  99. // }
  100.  
  101. /**
  102. * Find the smallest item in the priority queue.
  103. *
  104. * @return the smallest item, or throw an UnderflowException if empty.
  105. */
  106. public AnyType findMin() {
  107. if (isEmpty()) {
  108. throw new UnderflowException();
  109. }
  110. return array[ 1];
  111. }
  112.  
  113. /**
  114. * Remove the smallest item from the priority queue.
  115. *
  116. * @return the smallest item, or throw an UnderflowException if empty.
  117. */
  118. public AnyType deleteMin() {
  119. if (isEmpty()) {
  120. throw new UnderflowException();
  121. }
  122.  
  123. AnyType minItem = findMin();
  124. // System.out.println("nowsize" + size());
  125. array[1] = array[ currentSize];
  126. array[currentSize] = null;
  127. currentSize--;
  128. // System.out.println("aftersize" + size());
  129. percolateDown(1);
  130.  
  131.  
  132. return minItem;
  133. }
  134.  
  135. /**
  136. * Establish heap order property from an arbitrary arrangement of items.
  137. * Runs in linear time.
  138. */
  139. private void buildHeap() {
  140. for (int i = currentSize / 2; i > 0; i--) {
  141. percolateDown(i);
  142. }
  143. }
  144.  
  145. /**
  146. * Test if the priority queue is logically empty.
  147. *
  148. * @return true if empty, false otherwise.
  149. */
  150. public int size() {
  151. return currentSize;
  152. }
  153.  
  154. public boolean isEmpty() {
  155. return currentSize == 0;
  156. }
  157.  
  158. /**
  159. * Make the priority queue logically empty.
  160. */
  161. public void makeEmpty() {
  162. currentSize = 0;
  163. }
  164.  
  165. /**
  166. * Internal method to percolate down in the heap.
  167. *
  168. * @param hole the index at which the percolate begins.
  169. */
  170. private void percolateDown(int hole) {
  171. int child = 0;
  172. AnyType tmp = array[ hole];
  173. // System.out.println("tmp"+tmp);
  174.  
  175. // firstchildindex istället för hole * children då hole * 2 är 2i, alltså formeln för vänstra barnet i en binär heap
  176. for (; firstChildIndex(hole) <= currentSize; hole = child) {
  177. // System.out.println("new iter");
  178. child = firstChildIndex(hole)+1;
  179. //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
  180. //mindre än ett annat, i vårt fall måste vi hitta det minsta barnet (som kan vara d många)
  181. int smallestChild = firstChildIndex(hole);
  182. for (int i = 0; i < children-1; i++) {
  183. if (array[child]!=null&& array[smallestChild].compareTo(array[ child]) < 0) {
  184. // System.out.println(" value of child+1 (" + array[child + 1] + ") is less than value of child (" + array[child] + ")");
  185. child++;
  186. } else {
  187. smallestChild=child;
  188. }
  189. }
  190. child=smallestChild;
  191. // System.out.println("child after checking: "+child);
  192. // System.out.println("child value after checking: "+array[child]);
  193. //problemet jag är på nu är att jag inte förstår hur det värdet vi ska ta bort "försvinner".
  194. //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
  195. //vi ändrar på måste det ju tas bort, annars får man samma värde två gånger
  196. //det finns ingen ta bort metod i arrays, och man kan inte sätta det till null och man kan heller inte
  197. //bara ändra length hos arrayen eftersom när vi insertar lägger vi till en hel rad med nya platser i arrayen
  198. //MEN det blir exakt samma grej i BinaryHeap som vi fick av boken,
  199. //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
  200. //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
  201. //att värdet ligger kvar
  202. if (array[child]!=null&&array[ child].compareTo(tmp) < 0) {
  203. // System.out.println("value of child(" + array[child] + ") is less than value of temp(" + tmp + ")");
  204. array[ hole] = array[ child];
  205. // System.out.println("hole:" + array[hole]);
  206. } else {
  207. break;
  208. }
  209. }
  210. // int k = 0;
  211. // for(int i = 0; i<array.length;i++){
  212. // if(array[i]==null) {
  213. // k++;
  214. // }
  215. // }
  216. // decreaseArray(array.length-k-1);
  217. // System.out.println("hole:"+hole);
  218. array[ hole] = tmp;
  219. // System.out.println("size"+array.length);
  220. // System.out.println("current size value"+array[currentSize]);
  221. }
  222.  
  223. //generiska get-metoder för att hitta first child och parent
  224. public int firstChildIndex(int parent) {
  225. if (parent == 0) {
  226. throw new IllegalArgumentException();
  227. }
  228. return parent * children - (children - 2);
  229. //return 2 + (children *(parent-1));
  230. }
  231.  
  232. public int parentIndex(int child) {
  233. if (child < 1) {
  234. throw new IllegalArgumentException();
  235. }
  236. //return (child - 2) / children + 1;
  237. return (child + children - 2) / children;
  238. }
  239.  
  240. public AnyType get(int i) {
  241. return array[i];
  242. }
  243.  
  244. // public AnyType getIndex(int i){
  245. // AnyType yeah = array[i];
  246. // return yeah;
  247. // }
  248. // Test program
  249. // public static void main( String [ ] args )
  250. // {
  251. // int numItems = 10000;
  252. // BinaryHeap<Integer> h = new BinaryHeap<>( );
  253. // int i = 37;
  254. //
  255. // for( i = 37; i != 0; i = ( i + 37 ) % numItems )
  256. // h.insert( i );
  257. // for( i = 1; i < numItems; i++ )
  258. // if( h.deleteMin( ) != i )
  259. // System.out.println( "Oops! " + i );
  260. // }
  261. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement