package Myjava;
public class AVLtree {
class AVLtreeNode{
private int key;
private AVLtreeNode left;
private AVLtreeNode right;
private int hight;
public AVLtreeNode(int key,AVLtreeNode left,AVLtreeNode right){
this.key=key;
this.left=left;
this.right=right;
this.hight=0;
}
}
private AVLtreeNode root;//根节点
public AVLtree(){
root=null;
}
private int getHight(AVLtreeNode tree){//返回树的高度
if(tree!=null){
return tree.hight;
}
return 0;
}
public int getHight(){
return getHight(root);
}
public int max(int a,int b){
return a>b?a:b;
}
/*
* LL:左左对应的情况(左单旋转)。
* 2
* A B
* / 1 / \
* B -->>LL--->> X A
* / 0
* X
* 返回值:旋转后的根节点
* (插入的节点是左子树的左边节点)
*/
private AVLtreeNode llRotation(AVLtreeNode a){
AVLtreeNode b=null;
b=a.left;
a.left=b.right;
b.right=a;
a.hight=max(getHight(a.left),getHight(a.right))+1;
b.hight=max(getHight(b.left),a.hight)+1;
return b;
}
/*
* RR:右右对应的情况(右单旋转)。
*
* -2 0
* A B
* \ -1 /0 \0
* B -->>RR--->> A X
* \ 0
* X
*
*(插入节点是右子树的右边节点)
* 返回值:旋转后的根节点
*/
private AVLtreeNode rrRotation(AVLtreeNode a){
AVLtreeNode b=null;
b=a.right;
a.right=b.left;
b.left=a;
a.hight=max(getHight(a.left),getHight(a.right))+1;
b.hight=max(getHight(b.right),a.hight)+1;
return b;
}
private AVLtreeNode lrROtation(AVLtreeNode a){
return this.llRotation(this.rrRotation(a.left));
}
private AVLtreeNode rlRotation(AVLtreeNode a){
return this.rrRotation(this.llRotation(a.right));
}
public void insert(int key){
root=insert(root,key);
}
private AVLtreeNode insert(AVLtreeNode tree,int key){
if(tree==null){
tree=new AVLtreeNode(key,null,null);
}else{
int cmp=key-tree.key;//新加节点的值与原节点的值进行比较
if(cmp<0){
tree.left=insert(tree.left,key);
if(getHight(tree.left)-getHight(tree.right)==2){
if(key-tree.left.key<0){//LL
tree=llRotation(tree);
}else{//LR
tree=lrROtation(tree);
}
}
}else if(cmp>0){
tree.right=insert(tree.right,key);
if(getHight(tree.right)-getHight(tree.left)==2){
if(key-tree.right.key>0){//RR
tree=rrRotation(tree);
}else{//RL
tree=rlRotation(tree);
}
}
}else {
System.out.println("新加节点与原节点相同!");
}
}
tree.hight=max(getHight(tree.left),getHight(tree.right))+1;
return tree;
}
private void preOrder(AVLtreeNode tree){
if(tree!=null){
System.out.print(tree.key+" ");
preOrder(tree.left);
preOrder(tree.right);
}
}
public void preOrder(){//先序遍历
preOrder(root);
}
private void inOrder(AVLtreeNode tree){
if(tree!=null){
inOrder(tree.left);
System.out.print(tree.key+" ");
inOrder(tree.right);
}
}
public void inOrder(){//中序遍历
inOrder(root);
}
private void postOrder(AVLtreeNode tree){
if(tree!=null){
postOrder(tree.left);
postOrder(tree.right);
System.out.print(tree.key+" ");
}
}
public void postOrder(){//后序遍历
postOrder(root);
}
private void print(AVLtreeNode tree, int key, int direction) {
if(tree != null) {
if(direction==0) // tree是根节点
System.out.printf("%2d is root\n", tree.key, key);
else // tree是分支节点
System.out.printf("%2d is %2d's %6s child\n", tree.key, key, direction==1?"right" : "left");
print(tree.left, tree.key, -1);
print(tree.right,tree.key, 1);
}
}
public void print() {
if (root != null)
print(root, root.key, 0);
}
public static void main(String[] args) {
int i;
AVLtree tree = new AVLtree();
int arr[]= {3,2,1,4,5,6,0,10,9,8};
System.out.printf("== 依次添加: ");
for(i=0; i<arr.length; i++) {
tree.insert(arr[i]);
System.out.printf("%d ", arr[i]);
}
System.out.printf("\n== 前序遍历: ");
tree.preOrder();
System.out.println();
System.out.printf("\n== 中序遍历: ");
tree.inOrder();
System.out.println();
System.out.printf("\n== 后序遍历: ");
tree.postOrder();
System.out.println();
tree.print();
System.out.printf("高度: %d\n", tree.getHight());
}
}
平衡二叉树
于 2018-10-04 10:45:04 首次发布