大家好我正在实现一个avl树并尝试自我平衡树,但根节点不平衡。它只平衡左右子树。当我输入50 40 30 10时,根音应该是40但是它仍然是50,这是错误的。请有人看看我做错了什么代码。感谢
以下是我的代码:
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);
}