繁体   English   中英

无法平衡AVL树中的根节点

[英]Trouble balancing the root node in an 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);
}

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

rebalance(root);

在通话之前

updateHeight(root);

在方法的末尾。 然后,请确保在此之前删除重新平衡的呼叫。 因此,例如,更改

root.setLeftChild(rebalance(leftNodes));

root.setLeftChild(leftNodes);

假设您的其余代码工作正常,则应解决此问题。 然后,曾经被视为本地根的每个节点都将在其上调用平衡方法。包括树的绝对根。

暂无
暂无

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

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