public classAVL {private boolean taller=false;private Node root =null;private static final int EH=0;private static final int LH=1;private static final int RH=-1;private classNode{public intdata;publicNode leftChild;publicNode rightChild;public intbalanceFactor;public Node(intdata){this.data=data;
balanceFactor=0;
}
}publicNode RRotate(Node T){
Node temp=T.leftChild;
T.leftChild=temp.rightChild;
temp.rightChild=T;returntemp;
}publicNode LRotate(Node T){
Node temp=T.rightChild;
T.rightChild=temp.leftChild;
temp.leftChild=T;returntemp;
}publicNode leftBalance(Node node,Node preNode){
Node child=node.leftChild;
Node root=null;switch(child.balanceFactor){caseLH:
node.balanceFactor=child.balanceFactor=EH;
root=RRotate(node);if(preNode!=null && node.data
preNode.leftChild=root;
}if(preNode!=null && node.data>preNode.data){
preNode.rightChild=root;
}break;caseRH:
Node rchild=child.rightChild;switch(rchild.balanceFactor){caseEH:
node.balanceFactor=child.balanceFactor=EH;break;caseLH:
node.balanceFactor=RH;
child.balanceFactor=EH;break;caseRH:
node.balanceFactor=EH;
child.balanceFactor=LH;break;default:break;
}
rchild.balanceFactor=EH;
node.leftChild=LRotate(child);
root=RRotate(node);if(preNode!=null && node.data
preNode.leftChild=root;
}if(preNode!=null && node.data>preNode.data){
preNode.rightChild=root;
}break;default:break;
}returnroot;
}publicNode rightBalance(Node node,Node preNode){
Node child=node.rightChild;
Node root=null;switch(child.balanceFactor){caseRH:
node.balanceFactor=child.balanceFactor=EH;
root=LRotate(node);if(preNode!=null && node.data
preNode.leftChild=root;
}if(preNode!=null && node.data>preNode.data){
preNode.rightChild=root;
}break;caseLH:
Node lchild=child.leftChild;switch(lchild.balanceFactor){caseEH:
node.balanceFactor=child.balanceFactor=EH;break;caseRH:
node.balanceFactor=LH;
child.balanceFactor=EH;break;caseLH:
node.balanceFactor=EH;
child.balanceFactor=RH;break;default:break;
}
lchild.balanceFactor=EH;
node.rightChild=RRotate(child);
root=LRotate(node);if(preNode!=null && node.data
preNode.leftChild=root;
}if( preNode!=null && node.data>preNode.data){
preNode.rightChild=root;
}break;default:break;
}returnroot;
}public boolean insertNode(intvalue){return insertNode(root,value,null);
}public boolean insertNode(Node node, intvalue, Node preNode){if(node==null){
node=newNode(value);
node.balanceFactor=EH;
taller=true;if(preNode!=null && node.data
preNode.leftChild=node;
}if(preNode!=null && node.data>preNode.data){
preNode.rightChild=node;
}
root=node;return true;
}else{if(value==node.data){
root=node;return false;
}if (value
root=node;return false;
}if(taller){switch(node.balanceFactor){case EH:taller=true;node.balanceFactor=LH;break;case RH:taller=false;node.balanceFactor=EH;break;caseLH:
taller=false;
node=leftBalance(node,preNode);if(preNode!=null){
node=preNode;
}break;default:break;
}
}
}if (value>node.data){if (!insertNode(node.rightChild, value,node)) {
root=node;return false;
}if(taller){switch(node.balanceFactor){case EH:taller=true;node.balanceFactor=RH;break;case LH:taller=false;node.balanceFactor=EH;break;caseRH:
taller=false;
node=rightBalance(node,preNode);if(preNode!=null){
node=preNode;
}break;default:break;
}
}
}
}
root=node;return true;
}public voidinorderTraversal(){
inorderTraversal(root);
}public voidinorderTraversal(Node root){if(root!=null){
inorderTraversal(root.leftChild);
System.out.println("节点:"+root.data+" 平衡因子:"+root.balanceFactor);
inorderTraversal(root.rightChild);
}return;
}public static voidmain(String[] args) {//int[] data={8,6,4};//int[] data={8,6,9,5,7,3};//int[] data={8,6,7};//int[] data={8,5,9,4,6,7};//int[] data={8,5,9,4,7,6};
int[] data={8,5,9,7,6};
AVL avl=newAVL();for(int i=0;i
avl.insertNode(data[i]);
}
avl.inorderTraversal();
}
}