62,620
社区成员
发帖
与我相关
我的任务
分享
import java.util.Random;
public class AVLtree {
static final int NODE_NUM=10;
static final int RAND_SEED= 0x1031114;
public static void main(String[] args){
AVL_Node T_root;
int value;
Random rnd=new Random(RAND_SEED);
T_root=new AVL_Node(0);
for(int i=0; i<NODE_NUM;i++){
value=rnd.nextInt(NODE_NUM*10);
T_root=T_root.insert_v(T_root,value); //这行
}
System.out.println("树的高是:"+compute_height(T_root));
tree_shape(T_root);
}
public static int compute_height(AVL_Node p){
if(p==null){return 0;}
return p.h;
}
public static void tree_shape(AVL_Node p){
if(p==null){ return; }
System.out.print(" ( ");
if(p.left!=null){ tree_shape(p.left); }
System.out.print(p.value);
if(p.right!=null){ tree_shape(p.right); }
System.out.print(" ) ");
}
}
public class AVL_Node {
int value;
AVL_Node left;
AVL_Node right;
int h;
AVL_Node(int v){
this.value=v;
this.h=0;
this.left=null;
this. right=null;
}
public static int max(int lh,int rh){
int max=lh;
if(rh>max){
max=rh;
}
return (max);
}
public static AVL_Node insert_v(AVL_Node p, int x){
AVL_Node n=new AVL_Node(x);
if(p!=null){
return insert_n(p,n); //这行
}else{
return p;
}
}
public static AVL_Node insert_n(AVL_Node p, AVL_Node x){
int lh=-1, rh=-1;
if(x==null || x.value==p.value){
return p;
}
if(x.value<p.value){
if(p.left==null){
p.left=x;
}else {
p.left = insert_n(p.left, x);
}
}else if(p.value< x.value){
if(p.right==null){
p.right=x;
} else {
p.right = insert_n(p.right,x);
}
}
if(p.left!=null){
lh=p.left.h;
}
if(p.right!=null){
rh=p.right.h;
}
p.h=max(lh,rh)+1;
return rotate(p); //回転による根の入替に対応 //这行
}
public static AVL_Node rotate(AVL_Node p){
int lh=-1, rh=-1, llh=-1,lrh=-1, rlh=-1,rrh=-1;
if(p.left!=null){ /*左の子が空でない*/
lh=p.left.h;
if(p.left.left!=null){ llh=p.left.left.h;}
if(p.left.right!=null){lrh=p.left.right.h;}
}
if(p.right!=null){/*右の子が空でない*/
rh=p.right.h;
if(p.right.left!=null){ rlh=p.right.left.h;}
if(p.right.right!=null){ rrh=p.right.right.h;}
}
if(lh < rh){ // 右の子が高い
AVL_Node A=p,B=p.right;
AVL_Node X=A.left, Y=B.left, Z=B.right;
if(rlh<rrh){ // 右の右の孫が高い:1重回転
B.left=A; A.right=Y;
A.h=lh+1;
return B;
} else {//右の左の孫が高い:2重回転
AVL_Node C=Y, Y1=C.left, Y2=C.right;//这行
C.left=A; C.right=B; A.right=Y1; B.left=Y2;
A.h=lh+1; B.h=rrh+1; C.h =A.h+1;
return C;
}
}
if(lh < rh){ // 左の子が高い
AVL_Node A=p, B=p.left;
AVL_Node X=B.left, Y=B.right, Z=A.right;
if(llh>lrh){/* 左の左の孫が高い:1重回転*/
B.right=A; A.left=Y;
A.h=rh+1;
return B;
} else {/*左の右の孫が高い:2重回転*/
AVL_Node C=Y, Y1=C.left, Y2=C.right;
C.left=B; C.right=A; B.right= Y1; A.left=Y2;
B.h=llh+1; A.h=rh+1; C.h=B.h+1;
return C;
}
}
if(lh-rh<=1 && rh-lh<=1){ return p; }
return rotate(p);
}
}
if(lh < rh){ // 右の子が高い
AVL_Node A=p,B=p.right;
AVL_Node X=A.left, Y=B.left, Z=B.right;
if(rlh<rrh){ // 右の右の孫が高い:1重回転
B.left=A; A.right=Y;
A.h=lh+1;
return B;
} else {//右の左の孫が高い:2重回転
AVL_Node C=Y, Y1=C.left, Y2=C.right;
C.left=A; C.right=B; A.right=Y1; B.left=Y2;
A.h=lh+1; B.h=rrh+1; C.h =A.h+1;
return C;
}
}else{
AVL_Node A=p, B=p.left;
AVL_Node X=B.left, Y=B.right, Z=A.right;
if(llh>lrh){/* 左の左の孫が高い:1重回転*/
B.right=A; A.left=Y;
A.h=rh+1;
return B;
} else {/*左の右の孫が高い:2重回転*/
AVL_Node C=Y, Y1=C.left, Y2=C.right;
C.left=B; C.right=A; B.right= Y1; A.left=Y2;
B.h=llh+1; A.h=rh+1; C.h=B.h+1;
return C;
}
}
if(lh < rh){ // 右の子が高い
AVL_Node A=p,B=p.right;
AVL_Node X=A.left, Y=B.left, Z=B.right;
if(rlh<rrh){ // 右の右の孫が高い:1重回転
B.left=A; A.right=Y;
A.h=lh+1;
return B;
} else {//右の左の孫が高い:2重回転
AVL_Node C=Y, Y1=C.left, Y2=C.right;//这行
C.left=A; C.right=B; A.right=Y1; B.left=Y2;
A.h=lh+1; B.h=rrh+1; C.h =A.h+1;
return C;
}
}