Question

I'm struggling to understand why this class is not functioning. It was part of an assignment for a course on Data Structures(EDIT: The deadline for the assignment has passed, I just want to figure it out...). The node is part of an AVL tree built upon a BST and the way I chose to implement it is by creating methods within my Node class to find the Balance factor and height.

The class is structured as follows:

public class Node<T extends Comparable<? super T>> {

public T data;
public Node left;
public Node right;

public Node(T IN) {
    data = IN;
}

public Node(T IN, Node L, Node R) {
    this(IN);
    left = L;
    right = R;
}

@Override
public String toString() {
    return data.toString();
}

@Override
public Node clone() {
    return new Node(this.data) ;
}

public int getHeight() {
    return getHeight(this) ;
}

public int getBF() {

        //Calculate BF
        int balanceFactor = 0;
        if (right != null && left != null)
            balanceFactor = getHeight(right) - getHeight(left);
        else if (left != null) {
            balanceFactor = 0 - getHeight(left) ;
        }
        else if (right != null) {
            balanceFactor = getHeight(right) ;
        }
        else
            balanceFactor = 0 ;
        return balanceFactor ;
}

private int getHeight(Node p) {
    if (p.left == null && p.right == null ) {
        return 0 ;
    }
    else if (p.left != null && p.right != null) {
        return 1 + max(p.left.getHeight(), p.right.getHeight());
    }
    else if (p.left != null) {
        return 1 + p.left.getHeight() ;
    }
    else if (p.right != null) {
        return 1 + p.right.getHeight() ;
    }
    else {
        return 0;
    }
}

private int max(int x, int y) {
    if (x >= y) {
        return x;
    } else {
        return y;
    }
}

}

and the function calling the method is:

@Override
public boolean insert(T el) {
    boolean test = super.insert(el) ;
    if (test) {
        return checkBalance(root) ;
    }
    else
        return false ;
}

and the exception I recieve is a repetition of:

Exception in thread "main" java.lang.StackOverflowError
at Node.getHeight(Node.java:54)
at Node.getHeight(Node.java:33)
at Node.getHeight(Node.java:58)
Was it helpful?

Solution

I would suggest that either your tree is deformed or really big. There seems to be no problems with the code.

If your tree is deformed in such a way that you have a Node inserted twice in the same tree then this code will break.

Added - You are eating a little more stack than you need - replacing p.left.getHeight() with getHeight(p.left) etc. would avoid one stack push per recursion. If your issue is merely big tree then this might scrape you through but this would only postpone the problem.

OTHER TIPS

From looking at both getHeight methods, it seems like you don't have a tree but a cyclic graph. You should start testing with a tree consisting of only the root and then add nodes until you observe the infinite recursion. You probably have an error in the function that rebalances the tree.

EDIT: And you should make the attributes (at least left and right) private.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top