Java 在AVL树中平衡根节点时出现问题

Java 在AVL树中平衡根节点时出现问题,java,avl-tree,Java,Avl Tree,大家好,我似乎在重新平衡AVL树时遇到了麻烦。它平衡除根节点之外的所有其他节点。我不知道为什么它不能平衡根节点。当我传递诸如50、40、30、20和10之类的值时。我得到50,30,20,10和40。50不应为根,正确的根值应为40。 下面是我的代码: public void insert(T data){ AVLNode<T> newNode = new AVLNode<T>(data); if(isEmpty()){ root = n

大家好,我似乎在重新平衡AVL树时遇到了麻烦。它平衡除根节点之外的所有其他节点。我不知道为什么它不能平衡根节点。当我传递诸如50、40、30、20和10之类的值时。我得到50,30,20,10和40。50不应为根,正确的根值应为40。 下面是我的代码:

public void insert(T data){

    AVLNode<T> newNode = new AVLNode<T>(data);
    if(isEmpty()){
        root = newNode;
    }
    else{
        insert(root, newNode);
    }

}

private void insert(AVLNode<T> root, AVLNode<T> newNode){

 if(newNode.getData().compareTo(root.getData())<0){
        if(root.getLeftChild()!=null){
        AVLNode<T> leftNodes = root.getLeftChild();
        insert(leftNodes, newNode);
        root.setLeftChild(rebalance(leftNodes));
        }
        else{
            root.setLeftChild(newNode);
        }
    }
    else if(newNode.getData().compareTo(root.getData())>0){
        if(root.getRightChild()!=null){
        AVLNode<T> rightNodes = root.getRightChild();
        insert(rightNodes, newNode);
        root.setRightChild(rebalance(rightNodes));
    }
        else{
            root.setRightChild(newNode);
        }
    }
    else{
          root.setData(newNode.getData());
    }
    updateHeight(root); 
}

//re-balances the tree.
private AVLNode<T> rebalance(AVLNode<T> root){
    int difference = balance(root);
    if (difference > 1){
        if(balance(root.getLeftChild())>0){
            root = rotateRight(root);
        }
        else{
            root = rotateLeftRight(root);
        }
    }
    else if(difference < -1){
        if(balance(root.getRightChild())<0){
            root = rotateLeft(root);
        }
        else{
            root = rotateRightLeft(root);
        }
    }
    return root;
}

//updates the height of the tree.
public void updateHeight(AVLNode<T> root){
    if((root.getLeftChild()==null) && (root.getRightChild()!=null)){
        root.setHeight(root.getRightChild().getHeight()+1);
    }
    else if((root.getLeftChild() !=null)&&(root.getRightChild()==null)){
        root.setHeight(root.getLeftChild().getHeight()+1);
    }
    else
        root.setHeight(Math.max(getHeight(root.getLeftChild()), getHeight(root.getRightChild()))+1);
}

private int balance(AVLNode<T> root) {
    return getHeight(root.getLeftChild())-getHeight(root.getRightChild());
}

//single left left rotation of the tree
private AVLNode<T> rotateLeft(AVLNode<T> root){
    AVLNode<T> NodeA = root.getRightChild();
    root.setRightChild(NodeA.getLeftChild());
    NodeA.setLeftChild(root);
    root.setHeight(Math.max(getHeight(root.getLeftChild()), getHeight(root.getRightChild()))+1); //updates the height
    NodeA.setHeight(Math.max(getHeight(NodeA.getLeftChild()), getHeight(NodeA.getRightChild()))+1);
    return NodeA;
}
//single right right rotation of the tree
private AVLNode<T> rotateRight(AVLNode<T> root){
    AVLNode<T> NodeA = root.getLeftChild();
    root.setLeftChild(NodeA.getRightChild());
    NodeA.setRightChild(root);
    root.setHeight(Math.max(getHeight(root.getLeftChild()), getHeight(root.getRightChild()))+1); //updates the height of the AVL tree
    NodeA.setHeight(Math.max(getHeight(NodeA.getLeftChild()), getHeight(NodeA.getRightChild()))+1);
    return NodeA;
}
//a double rotation. Left right rotation
private AVLNode<T> rotateLeftRight(AVLNode<T> root){
    AVLNode<T> nodeA = root.getLeftChild();
    root.setLeftChild(rotateLeft(nodeA));
    return rotateRight(root);
}
//a right left rotation
private AVLNode<T> rotateRightLeft(AVLNode<T> root){
    AVLNode<T> nodeA = root.getRightChild();
    root.setRightChild(rotateRight(nodeA));
    return rotateLeft(root);
}
公共作废插入(T数据){
AVLNode newNode=新的AVLNode(数据);
if(isEmpty()){
根=新节点;
}
否则{
插入(根,新节点);
}
}
专用void插入(AVLNode root,AVLNode newNode){
if(newNode.getData().compareTo(root.getData())0){
if(root.getRightChild()!=null){
AVLNode rightNodes=root.getRightChild();
插入(rightNodes、newNode);
setRightChild(重新平衡(rightNodes));
}
否则{
setRightChild(newNode);
}
}
否则{
setData(newNode.getData());
}
更新高度(根);
}
//重新平衡树。
专用AVLNode重新平衡(AVLNode根){
内部差异=平衡(根);
如果(差异>1){
if(balance(root.getLeftChild())>0){
根=旋转右(根);
}
否则{
根=右旋转(根);
}
}
否则如果(差异<-1){

如果在INSERT方法中使用(Boo.GruttHealthCar)(p>),则应考虑添加

rebalance(root);
就在打电话之前

updateHeight(root);
然后确保在此之前删除重新平衡调用。例如,更改

root.setLeftChild(rebalance(leftNodes));

假设您的其余代码工作正常,这应该是修复方法。然后,每个被视为本地根的节点都将调用平衡方法。包括树的绝对根

root.setLeftChild(leftNodes);