943,546 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Marked Solved
  • Views: 634
  • Java RSS
Nov 29th, 2008
0

Why am I getting a StackOverflowError?

Expand Post »
I am trying to create a heap sort method using recursion but whenever one of my methods called maxHeapify is called, it would throw a StackOverflowError. I understand that this error will pop up whenever my application recurses too deeply but the problem is I don't see anything wrong with my maxHeapify method that would cause such an error.

Here's the code:

Java Syntax (Toggle Plain Text)
  1. public class Sorting {
  2.  
  3. public static void maxHeapify (int[] A, int i) throws OutOfRangeException{
  4.  
  5. int heapSize = A.length;
  6.  
  7. if ((i < 0) || (i >= heapSize)){
  8. throw new OutOfRangeException();
  9. }
  10.  
  11. int left = (2*i)+1;
  12. int right = (2*i)+2;
  13. int largest = i;
  14.  
  15. if ((left < heapSize ) && (A > A[i])){
  16. largest = left;
  17. }
  18.  
  19. if ((right < heapSize) && (A > A[i])){
  20. largest = right;
  21. }
  22.  
  23. if (largest != i){
  24. int tmp = A[i];
  25. A[i] = A[largest];
  26. A[largest] = tmp;
  27. }
  28.  
  29. maxHeapify(A, largest);
  30. }
  31.  
  32.  
  33. public static void buildHeap (int[] A){
  34. int n = A.length;
  35. int i = (int) Math.floor(n/2) - 1;
  36.  
  37. try{
  38. while (i > 0){
  39. maxHeapify(A, i);
  40. i --;
  41. }
  42. }
  43. catch (OutOfRangeException ex){
  44. System.out.println("Error: Out of range");
  45. }
  46. }
  47.  
  48. public static int deleteMax (int[] A) throws Exception{
  49. int heapSize = A.length;
  50.  
  51. if (heapSize > 1){
  52. int largest = A[0];
  53. A[0] = A[heapSize -1];
  54. heapSize --;
  55. maxHeapify(A, 0);
  56. return largest;
  57. }
  58. else if (heapSize == 1){
  59. heapSize = 0;
  60. return A[0];
  61. }
  62. else{
  63. throw new Exception();
  64. //throw new EmptyHeapException();
  65. }
  66. }
  67.  
  68. public static void heapSort(int [] A){
  69.  
  70. int heapSize = A.length;
  71.  
  72. if (heapSize > 1){
  73. buildHeap(A);
  74. int i = heapSize-1;
  75.  
  76. while (i > 0){
  77. try{
  78. A[i] = deleteMax(A);
  79. i--;
  80. }
  81. catch (Exception ex){
  82. System.out.println("Heap is empty, unable to sort array.");
  83. }
  84. }
  85. }
  86. }
  87. }

It'll be great if anyone can help me out.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
pocku is offline Offline
22 posts
since Nov 2007
Nov 29th, 2008
0

Re: Why am I getting a StackOverflowError?

It doesn't seem like your maxHeapify method has a base case. Meaning, even when it finds the values you are looking for, it will keep looking for those values. You need a base case so that once the method finds what its looking for, it no longer continues calling itself. Try adding this:

Java Syntax (Toggle Plain Text)
  1.  
  2. if (largest != i){
  3. int tmp = A[i];
  4. A[i] = A[largest];
  5. A[largest] = tmp;
  6. }
  7. else{ //putting return here prevents the method from calling itself again
  8. return;
  9. }
Last edited by BestJewSinceJC; Nov 29th, 2008 at 10:29 pm.
Reputation Points: 874
Solved Threads: 352
Posting Maven
BestJewSinceJC is offline Offline
2,758 posts
since Sep 2008
Nov 29th, 2008
0

Re: Why am I getting a StackOverflowError?

Ah, can't believe I missed that... Thanks for all your help. My code's running fine now.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
pocku is offline Offline
22 posts
since Nov 2007

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: Problem reading data from file
Next Thread in Java Forum Timeline: Stack class, Symbol Table help





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC