无法平衡AVL树中的根节点

时间:2015-06-12 08:32:12

标签: 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 = 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);
}

1 个答案:

答案 0 :(得分:0)

在插入方法中,您应该考虑添加

rebalance(root);

在电话会议之前

updateHeight(root);

在方法结束时。然后确保在此之前删除对重新平衡的调用。所以,例如,改变

root.setLeftChild(rebalance(leftNodes));

root.setLeftChild(leftNodes);

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