View Single Post
Join Date: Nov 2007
Posts: 28
Reputation: Cleo123 is an unknown quantity at this point 
Solved Threads: 0
Cleo123 Cleo123 is offline Offline
Light Poster

Exception error for heap

 
0
  #1
Dec 1st, 2008
I'm not sure if I'm doing this correctly but up to now I get an exception error. Can you see why? :

  1.  
  2. public void countHeap(){
  3. System.out.println("input data:");
  4. for (int i = 0; i < dataSet.length; i++) {
  5. System.out.print(" "+dataSet[i]);
  6. count++;
  7. }
  8. noDataElements = count;
  9. formHeapTree();
  10. }
  11. public void formHeapTree(){
  12.  
  13. int unsortedSet[] = new int[noDataElements];
  14.  
  15. noDataElementsHalf = (noDataElements+1)/2;
  16.  
  17. System.out.println("\nheap Array");
  18. for (int j = 0; j < unsortedSet.length ; j++) {
  19. for (int i = noDataElementsHalf; i < noDataElements; i++) {
  20. unsortedSet[i] = dataSet[j];
  21. }
  22. System.out.print(" "+ unsortedSet[j]);
  23. }
  24. sortLevel();
  25.  
  26. for (int j = 0; j < unsortedSet.length ; j++) {
  27. for (int i = 0; i < noDataElementsHalf; i++) {
  28. unsortedSet[i] = dataSet[j];
  29. }
  30. System.out.print(" "+ unsortedSet[j]);
  31. }
  32. }
  33. public void sortLevel(){
  34.  
  35. System.out.print("\nheap array \n ");
  36. for (int i = unsortedSet.length; i > 0; i--) {
  37. int j = i-1;
  38. //Parent must be smaller than child
  39. if(unsortedSet[j] > unsortedSet[i]) { int temp = unsortedSet[i];
  40. unsortedSet[i] = unsortedSet[j];
  41. unsortedSet[j] = temp;
  42. }
  43. }
  44. }
Last edited by Cleo123; Dec 1st, 2008 at 2:11 am.
Reply With Quote