简体   繁体   English

为什么此方法会导致无限递归调用?

[英]Why does this method cause an Infinite Recursive call?

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. 该节点是建立在BST上的AVL树的一部分,而我选择实现该节点的方法是通过在Node类中创建方法来找到平衡因子和高度。

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)

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. 如果您的树变形,使得您在同一棵树中插入了两次Node ,则此代码将中断。

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. 补充 -您正在吃的堆栈比您需要的多-用getHeight(p.left)等替换p.left.getHeight() getHeight(p.left)等将避免每次递归一次堆栈推送。 If your issue is merely big tree then this might scrape you through but this would only postpone the problem. 如果您的问题只是一棵大树,那么这可能会刮伤您,但这只会推迟问题。

From looking at both getHeight methods, it seems like you don't have a tree but a cyclic graph. 从两个getHeight方法来看,似乎您没有树,而是有一个循环图。 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. 编辑:并且您应该使属性(至少左和右)私有。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM