Exception in thread "main" java.lang.StackOverflowError

Cat

Error received:

Exception in thread "main" java.lang.StackOverflowError
    at AVL.insert(AVL.java:45)

I am not familiar with the error I was given, but I do know that it only happens when the array being used to build the AVL tree is a vary large size and is occurring during insert when moving to the right side of the tree. I am not sure why this is happening though (in other words I do not know exactly what StackOverflowError is and why it is happening).

AVL class:

//AVL.java
import java.util.*;
import java.io.*;

public class AVL{
    AvlNode root;

   public void tree(int[] list){
      for(int i=0; i<list.length; i++){
         insertPrep(list[i]);
      }
   }

   public void insertPrep(int data){
      if (root==null){root = new AvlNode(data);}
        else {
         AvlNode newNode = new AvlNode(data);
         root = insert(root, newNode);
         root = rebalance(root);
         adjustHeight(root);
      }
    }

   public AvlNode insert(AvlNode node, AvlNode newNode){
      if (node.key > newNode.key){
         if(node.left!=null){node.left=insert(node.left , newNode);}
         else{node.left=newNode;}
      }
      else if (node.key < newNode.key){
         if(node.right!=null){node.right=insert(node.right, newNode);}
         else{node.right=newNode;}
      }
      AvlNode result = rebalance(node); 
      adjustHeight(result);
      return result;
   }

   public int height (AvlNode node ){
      if (node == null){return 0;}
      else {return node.height;}
   }

   public void adjustHeight (AvlNode node){
      if (root != null){ root.height = 1+ Math.max(height(root.left),height(root.right));}
   }

   public AvlNode rebalance (AvlNode node){
      AvlNode newAvlNode = node;

      if (node.left != null && node.right != null){
        if (node.left.height-node.right.height==2){
            if (node.left.left.height>node.left.right.height){
                AvlNode n2 = node.left;
                AvlNode n3 = node;
                n3.left = n2.right;
                n2.right = n3;

                adjustHeight(n3);
                adjustHeight(n2);
                newAvlNode = n2;
            } else {
                AvlNode n1 = node.left;
                AvlNode n2 = node.left.right;
                AvlNode n3 = node;
                n1.right = n2.left;
                n2.left = n1;
                n3.left = n2.right;
                n2.right = n3;

                adjustHeight(n1);
                adjustHeight(n3);
                adjustHeight(n2);
                newAvlNode = n2;
            }
        } else if (node.right.height-node.left.height==2){
            if (node.right.right.height>node.right.left.height){
                AvlNode n1 = node;
                AvlNode n2 = node.right;
                n1.right = n2.left;
                n2.left = n1;

                adjustHeight(n1);
                adjustHeight(n2);
                newAvlNode = n2;
            } else {
                AvlNode n1 = node;
                AvlNode n2 = node.right.left;
                AvlNode n3 = node.right;
                n1.right = n2.left;
                n2.left = n1;
                n3.left = n2.right;
                n2.right = n3;

                adjustHeight(n1);
                adjustHeight(n3);
                adjustHeight(n2);
                newAvlNode = n2;
            }
        }
      }
    return newAvlNode;
   }

   class AvlNode{
    int key, height; //data for input numbers and height for height of nodes to keep balance
    AvlNode left, right; //left for left side of tree and right for right side of tree

      AvlNode(int data){
         key = data;
      }

   }
}

Class using AVL:

//Tree.java
import java.io.*;
import java.util.*;

public class Tree{

   public static void main(String[] args){

      int n = 30000; //numbers to be in arrays
      int a[] = new int[n]; //first array

      for (int i=0; i<n; i++){
         a[i] = i+1; //insert #'s 1-n; smallest to largest
      }

      //send arrays to be put in AVL trees
      AVL avl = new AVL();
      double timeSoFar = (double)System.nanoTime();
      avl.tree(a);
      double treeTime = (double)System.nanoTime() - timeSoFar;
      printTime('a',treeTime, "AVL");

   }

   public static void printTime(char l, double treeTime, String tree){
      double treeTimeMin = treeTime/600000;
      treeTimeMin/=100000;
      System.out.println("Elapsed time for building " + tree + " " + "Tree for array '" + l + "': " + treeTime + " nanoseconds, or: " + treeTimeMin + " minutes.");
   }

}
Bruno Grieder

Since your array is sorted from smallest to largest, when you try to insert the, say, 15000th node using insertPrep (see the loop in tree()), you are going to recursively call insert(AvlNode node, AvlNode newNode) 15000 times.

This is due to the test in insert

  if (node.key > newNode.key){
     if(node.left!=null){node.left=insert(node.left , newNode);}
     else{node.left=newNode;}
  }

The recursions are too deep

Recursion is probably not your best choice to find the location in the tree and you should resort to a loop which will be more efficient anyway because you do not have to accumulate the frames between the calls.

Alternatively, use a language such as Scala that knows about tail recursion and automatically unfolds tail recursions to loops at compile time.

EDIT The explanation for the Overflow is probably over simplistic. See comments below

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

This in Java - Exception in thread "main" java.lang.StackOverflowError

From Dev

Java - Recursion - Exception in thread "main" java.lang.StackOverflowError

From Dev

Getting exception in thread "main" java.lang.StackOverflowError

From Dev

Recursion - Exception in thread "main" java.lang.StackOverflowError

From Dev

3 Way Quicksort: Exception in thread "main" java.lang.StackOverflowError

From Dev

Exception in thread "main" java.lang.StackOverflowError issue

From Dev

QuickSort giving exception in thread "main" java.lang.StackOverflowError

From Dev

java.lang.StackOverflowError in Thread main

From Dev

Why Am I getting "Exception in thread "main" java.lang.StackOverflowError" in recursive java method?

From Dev

“Exception in thread ”main“ java.lang.StackOverflowError” in Quick sort for sorted, reversed and Identical data of size greater than 10000

From Dev

Exception in thread "main" java.lang.IllegalMonitorStateException

From Dev

Exception in thread "main" java.lang.UnsupportedClassVersionError:

From Dev

Exception in thread "main" java.lang.IndexOutOfBoundsException:

From Dev

Exception in thread "main" java.lang.NoClassDefFoundError: Main$1 java?

From Dev

PDFbox Exception - Exception in thread "main" java.lang.VerifyError

From Dev

Java: (Error) Exception in thread "main" java.lang.NullPointerException at Main.main(Main.java:14)

From Dev

java Runnable - Exception in thread "main" java.lang.NoClassDefFoundError

From Dev

java error: Exception in thread "main" java.lang.NoClassDefFoundError

From Dev

"Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0" java error

From Dev

Exception in thread "main" java.lang.NumberFormatException: For input string: "" in Java

From Dev

Exception in thread "main" java.lang.IncompatibleClassChangeError: Implementing class

From Dev

Exception in thread "main" java.lang.NumberFormatException: For input string: "1"

From Dev

Got Exception in thread "main" java.lang.IndexOutOfBoundsException: No group 1

From Dev

"Exception in thread "main" java.lang.NoClassDefFoundError: org/hamcrest/SelfDescribing"

From Dev

Exception in thread "main" java.lang.ClassNotFoundException: WordCount

From Dev

"Exception in thread "main" java.lang.NullPointerException" - CSV to Arff

From Dev

Exception in thread "main" java.lang.NoClassDefFoundError: junit/textui/ResultPrinter

From Dev

Exception in thread "main" java.lang.ExceptionInInitializerError (Clojure)

From Dev

Getting Exception in thread "main" java.lang.NullPointerException

Related Related

  1. 1

    This in Java - Exception in thread "main" java.lang.StackOverflowError

  2. 2

    Java - Recursion - Exception in thread "main" java.lang.StackOverflowError

  3. 3

    Getting exception in thread "main" java.lang.StackOverflowError

  4. 4

    Recursion - Exception in thread "main" java.lang.StackOverflowError

  5. 5

    3 Way Quicksort: Exception in thread "main" java.lang.StackOverflowError

  6. 6

    Exception in thread "main" java.lang.StackOverflowError issue

  7. 7

    QuickSort giving exception in thread "main" java.lang.StackOverflowError

  8. 8

    java.lang.StackOverflowError in Thread main

  9. 9

    Why Am I getting "Exception in thread "main" java.lang.StackOverflowError" in recursive java method?

  10. 10

    “Exception in thread ”main“ java.lang.StackOverflowError” in Quick sort for sorted, reversed and Identical data of size greater than 10000

  11. 11

    Exception in thread "main" java.lang.IllegalMonitorStateException

  12. 12

    Exception in thread "main" java.lang.UnsupportedClassVersionError:

  13. 13

    Exception in thread "main" java.lang.IndexOutOfBoundsException:

  14. 14

    Exception in thread "main" java.lang.NoClassDefFoundError: Main$1 java?

  15. 15

    PDFbox Exception - Exception in thread "main" java.lang.VerifyError

  16. 16

    Java: (Error) Exception in thread "main" java.lang.NullPointerException at Main.main(Main.java:14)

  17. 17

    java Runnable - Exception in thread "main" java.lang.NoClassDefFoundError

  18. 18

    java error: Exception in thread "main" java.lang.NoClassDefFoundError

  19. 19

    "Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0" java error

  20. 20

    Exception in thread "main" java.lang.NumberFormatException: For input string: "" in Java

  21. 21

    Exception in thread "main" java.lang.IncompatibleClassChangeError: Implementing class

  22. 22

    Exception in thread "main" java.lang.NumberFormatException: For input string: "1"

  23. 23

    Got Exception in thread "main" java.lang.IndexOutOfBoundsException: No group 1

  24. 24

    "Exception in thread "main" java.lang.NoClassDefFoundError: org/hamcrest/SelfDescribing"

  25. 25

    Exception in thread "main" java.lang.ClassNotFoundException: WordCount

  26. 26

    "Exception in thread "main" java.lang.NullPointerException" - CSV to Arff

  27. 27

    Exception in thread "main" java.lang.NoClassDefFoundError: junit/textui/ResultPrinter

  28. 28

    Exception in thread "main" java.lang.ExceptionInInitializerError (Clojure)

  29. 29

    Getting Exception in thread "main" java.lang.NullPointerException

HotTag

Archive