0

Basically, I am implementing an AVL tree by reading a set of integers from a text file and then populate the tree by using the add() method. Also, the program is supposed to print in order the set of integers.

As I run the program, a StackOverflowError pops up. I think that this error is being triggered due to something malfunctioning in the add() method.

I would really appreaciate if someone helps me as I am new to this type of programming.

This is part of the Main Class:

 public static void main(String[] args) throws FileNotFoundException
   {

            AVL s1 = new AVL();

            Scanner file = new Scanner(new File("C:\\Users\\Dell\\Desktop\\integers.txt"));

            while(file.hasNext())
            {
               // String str = file.next();

                //int b = Integer.parseInt(str);
                int b = file.nextInt();
                s1.add(b);

            }

            v1.PrintInOrder(v1.root); 

These are the add() and PrintInOrder() methods:

public boolean add(int key)
 {
        root = add(root, key);
        return true;
 }

private Node add(Node b1, int key)
 {

     if(b1 == null)
     {
        return new Node(key);
     }

     if(key < b1.element){
            b1.left = add(b1.left, key);
     }
     else
     {
            b1.right = add(b1.right, key);
     }

     int Left_Height = getHeight(b1.left);
     int Right_Height = getHeight(b1.right);

     // a height imbalance requires that two subtrees differ by two
     if(Math.abs(LeftHeight - RightHeight )== 2)
         return Balance(n1);
     else
     {
        n1.ResetHeight();
        return b1;
     }
 }

   public void PrintInOrder(Node b1){
      if(b1 != null){
        PrintInOrder(b1.left);
        System.out.println(b1.element);
        PrintInOrder(b1.right);
      }
 }

This is the Node class:

public class Node {

Node left;
Node right;
int element;
int height;

public Node(int keys){
    this(keys, null, null);
}

public Node(int d, Node right1, Node left1){
    element = d;
    height = 0;
    left = left1;
    right = right1;
}


// This method recalculates the height if the right or left subtrees have been altered
 public void ResetHeight(){

    int LeftHeight = AVL.getHeight(left);
    int RightHeight = AVL.getHeight(right);
    height = 1 + Math.max(LeftHeight,RightHeight);
}
femtoRgon
  • 32,893
  • 7
  • 60
  • 87
  • Please post the exact error (with all it details). – PM 77-1 Apr 12 '13 at 21:16
  • 2
    Stack overflows "pop up" when you have infinite recursion. Either trace your code on a small example by hand or get in the debugger and have the machine do it for you. Think. Getting others to fix your bugs is not a good way to learn. – Gene Apr 12 '13 at 21:18
  • 1
    I honestly don't know why because it's not worth my time. But Investigating infinite recursion can also be done by looking at the recursive condition and asking yourself under what circumstances it will keep on being called. – Dan Apr 12 '13 at 21:20
  • On a side note, I hope your getHeight() method is not actually traversing the tree to find the height. That itself would be a O(nlogn) call everytime. – Vaibhav Desai Apr 12 '13 at 21:20
  • @PM 77-1 the exact error is: Exception in thread "main" java.lang.StackOverflowError at avl.AVL.insert(AVL.java:44) – user2275998 Apr 12 '13 at 21:21
  • 1
    What line is AVL.java:44? What are the last methods at the stack trace? You should put some of it in the question. – acdcjunior Apr 12 '13 at 21:29
  • How is `root` defined and where and how is it initialized? – PM 77-1 Apr 12 '13 at 21:32
  • @ PM 77-1 - root is defined in the AVL class and it is initialized as null: public Node root = null; I debugged the program, the problem is certainly in the add() method. I think that the integers are not being read and passed properly from the file. – user2275998 Apr 12 '13 at 21:39
  • Are you instantiating **new** `Node` objects anywhere in your code? – PM 77-1 Apr 13 '13 at 05:09
  • I found out that when I input small valued numbers in the file, the program works fine but when bigger valued numbers are inserted such as 89 or 78, the StackOverflowError comes up. – user2275998 Apr 13 '13 at 07:41

1 Answers1

1

Since stack overflows commonly occur in recursion. Use your IDE and set a break at locations where you have done recusion, then debug. Step through it.

Joban
  • 1,318
  • 7
  • 19
  • I think that the problem is when I call the Height_Reset() method from the add() method because when I disable it it works fine. I don't know what is wrong with it. – user2275998 Apr 13 '13 at 11:26