帮忙改下错误-AVL树

psp_66 2012-06-02 02:54:21

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);
}

}

2个class
错误提示
Exception in thread "main" java.lang.NullPointerException
at AVL_Node.rotate(AVL_Node.java:80)
at AVL_Node.insert_n(AVL_Node.java:55)
at AVL_Node.insert_v(AVL_Node.java:25)
at AVLtree.main(AVLtree.java:14)
对应的行数 //这行
谢谢
...全文
144 11 打赏 收藏 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
psp_66 2012-06-04
  • 打赏
  • 举报
回复
继续求支援。
mkworld 2012-06-04
  • 打赏
  • 举报
回复

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;

}
}


这样就好
古布 2012-06-03
  • 打赏
  • 举报
回复
自己DEBUG吧,很快就会发现
psp_66 2012-06-03
  • 打赏
  • 举报
回复
还不会请求支援! 感谢
psp_66 2012-06-03
  • 打赏
  • 举报
回复
但是不会改。。。 劳驾帮改下

求AVL树的高。
aaa2550 2012-06-03
  • 打赏
  • 举报
回复
不知道你要实现什么功能
aaa2550 2012-06-03
  • 打赏
  • 举报
回复
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;
}

}

好好看看你这段代码,其实你的p.right里面的right和left都是空的,
所以b的right和left都是空的,
所以Y是空的
所以C是空的
所以 Y1=C.left, Y2=C.right当然会抛空指针
psp_66 2012-06-03
  • 打赏
  • 举报
回复
Debug后还是改不对。。。继续求帮忙
psp_66 2012-06-02
  • 打赏
  • 举报
回复
能否给个正确的,感谢
  • 打赏
  • 举报
回复
你对应行号,看看那里的对象引用是否 为null.有则改之 无则加勉。
jlu_lamp_lamp 2012-06-02
  • 打赏
  • 举报
回复
空指针异常
说明你的对象还没有初始化或者没有值的时候就引用了

62,620

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧