在Java中按级别打印AVL树

在Java中按级别打印AVL树,java,binary-tree,binary-search-tree,avl-tree,Java,Binary Tree,Binary Search Tree,Avl Tree,我正试图获取一个AVL树并逐级显示它,但不知何故我失败了,不知道在哪里。附件是显示我当前输出的图像。我实际上应该得到一个完整的二叉树,所以很明显有些地方出了问题。在所附的图像中,有一张我的“byLevel”打印功能的照片,因此您可以看到我是如何尝试打印它们的,我将附加我的insert功能,因为这是本部分中唯一重要的两个功能。我很感激任何帮助,我不明白我做错了什么,因为这是一个常用的算法 private Node insert(Node newNode, String newWord){

我正试图获取一个AVL树并逐级显示它,但不知何故我失败了,不知道在哪里。附件是显示我当前输出的图像。我实际上应该得到一个完整的二叉树,所以很明显有些地方出了问题。在所附的图像中,有一张我的“byLevel”打印功能的照片,因此您可以看到我是如何尝试打印它们的,我将附加我的insert功能,因为这是本部分中唯一重要的两个功能。我很感激任何帮助,我不明白我做错了什么,因为这是一个常用的算法

private Node insert(Node newNode, String newWord){
    if(newNode == null){
        newNode = new Node(newWord);
    }
    else if (newWord.compareToIgnoreCase(newNode.getReservedWord()) < 0){
        newNode.setlChild(insert(newNode.getlChild(), newWord));

        if(depth(newNode.getlChild()) - depth(newNode.getrChild()) == 2){
            if(newWord.compareToIgnoreCase(newNode.getlChild().getReservedWord()) < 0){
                newNode = rotateWithLeftChild(newNode);
            }
            else{
                newNode = doubleWithLeftChild(newNode);
            }
        }
    }
    else if (newWord.compareToIgnoreCase(newNode.getReservedWord()) > 0){
        newNode.setrChild(insert(newNode.getrChild(), newWord));

        if(depth(newNode.getrChild()) - depth(newNode.getlChild()) == 2){
            if(newWord.compareToIgnoreCase(newNode.getrChild().getReservedWord()) > 0){
                newNode = rotateWithRightChild(newNode);
            }
            else{
                newNode = doubleWithRightChild(newNode);
            }
        }
    }
    else;

    newNode.setDepth(max(depth(newNode.getlChild()), depth(newNode.getrChild()) + 1));
    /*if(!this.getAllNodes().contains(newNode)){
        this.getAllNodes().add(newNode);
    }*/
    return newNode;
}

private Node rotateWithLeftChild(Node nodeToRotate){
    Node newNode = nodeToRotate.getlChild();
    nodeToRotate.setlChild(newNode.getrChild());
    newNode.setrChild(nodeToRotate);
    nodeToRotate.setDepth(max(depth(nodeToRotate.getlChild()), depth(nodeToRotate.getrChild()) + 1));
    newNode.setDepth(max(depth(newNode.getlChild()), nodeToRotate.getDepth() + 1));
    return newNode;
}

private Node rotateWithRightChild(Node nodeToRotate){
    Node newNode = nodeToRotate.getrChild();
    nodeToRotate.setrChild(newNode.getlChild());
    newNode.setlChild(nodeToRotate);
    nodeToRotate.setDepth(max(depth(nodeToRotate.getlChild()), depth(nodeToRotate.getrChild()) + 1));
    newNode.setDepth(max(depth(newNode.getrChild()), nodeToRotate.getDepth() + 1));
    return newNode;
}

private Node doubleWithLeftChild(Node nodeToRotate){
    nodeToRotate.setlChild(rotateWithRightChild(nodeToRotate.getlChild()));
    return rotateWithLeftChild(nodeToRotate);
}

private Node doubleWithRightChild(Node nodeToRotate){
    nodeToRotate.setrChild(rotateWithLeftChild(nodeToRotate.getrChild()));
    return rotateWithRightChild(nodeToRotate);
}
专用节点插入(节点newNode,字符串newWord){
if(newNode==null){
newNode=新节点(newWord);
}
else if(newWord.compareToIgnoreCase(newNode.getReservedWord())<0){
setlChild(insert(newNode.getlChild(),newWord));
if(depth(newNode.getlChild())-depth(newNode.getrChild())==2){
if(newWord.compareToIgnoreCase(newNode.getlChild().getReservedWord())<0){
newNode=rotateWithLeftChild(newNode);
}
否则{
newNode=doubleWithLeftChild(newNode);
}
}
}
else if(newWord.compareToIgnoreCase(newNode.getReservedWord())>0){
setrChild(insert(newNode.getrChild(),newWord));
if(depth(newNode.getrChild())-depth(newNode.getlChild())==2){
if(newWord.compareTIgnoreCase(newNode.getrChild().getReservedWord())>0){
newNode=rotateWithChild(newNode);
}
否则{
newNode=doubleWithRightChild(newNode);
}
}
}
其他的
setDepth(最大值(深度(newNode.getlChild()),深度(newNode.getrChild())+1));
/*如果(!this.getAllNodes().包含(newNode)){
this.getAllNodes().add(newNode);
}*/
返回newNode;
}
私有节点rotateWithLeftChild(节点nodeToRotate){
Node newNode=nodeToRotate.getlChild();
nodeToRotate.setlChild(newNode.getrChild());
newNode.setrChild(nodeToRotate);
setDepth(最大值(深度(nodeToRotate.getlChild()),深度(nodeToRotate.getrChild())+1));
setDepth(最大深度(newNode.getlChild()、nodeToRotate.getDepth()+1));
返回newNode;
}
private Node RotateThrightChild(节点nodeToRotate){
Node newNode=nodeToRotate.getrChild();
nodeToRotate.setrChild(newNode.getlChild());
newNode.setlChild(nodeToRotate);
setDepth(最大值(深度(nodeToRotate.getlChild()),深度(nodeToRotate.getrChild())+1));
setDepth(最大值(深度(newNode.getrChild()),nodeToRotate.getDepth()+1));
返回newNode;
}
私有节点doubleWithLeftChild(节点旋转){
nodeToRotate.setlChild(rotateWithChild(nodeToRotate.getlChild());
返回rotateWithLeftChild(nodeToRotate);
}
私有节点doubleWithRightChild(节点旋转){
nodeToRotate.setrChild(rotateWithLeftChild(nodeToRotate.getrChild());
返回RotateThrightChild(nodeToRotate);
}

再次修复了我自己的问题,是的,提供的代码足够了。问题很简单,我在max函数的第二个部分加+1,而不是在max函数之后加+1,这是对深度的错误计算。我认为这个问题本身已经说得很清楚了,因为AVL树是常见的编码。谢谢。

目前我无法理解您到底在做什么,因为缺少很多代码,无法复制。我所能建议的只是一个在这样的案例中广泛使用的“最佳实践”:准备一个JUnit来测试所有4个相关的AVL旋转案例,并检查它们是否正常工作。通过这种方式,您还可以重新表述您的问题,解释什么不起作用,什么时候insert不能保持树的平衡,这样您就可以了解什么是错误的,以及您(或我们)可以调查的地方。附言:在你的访问中还要加上节点的深度:我认为那里有问题。