000gjk 2022-12-07 21:55 采纳率: 83.3%
浏览 23
已结题

java二叉排序树(数字加字符串的插入输出)

狠狠地报错了 不知道是为什么😭
package search;

public class BTS {

public static void main(String[] args) {
    // TODO Auto-generated method stub
    BTS bts = new BTS();
    Node r;
    char[][] arr = null;
    bts.create(arr);
    bts.insertBTS(1002, '王', '男', 2021053, 12345678);
    bts.show1(bts);
}

class Node{
    int key;
    Node lchild;
    Node rchild;
    Node(){
        lchild=rchild=null;
    }
}

public Node r;  //根节点
public Node f;  //存放待删除结点的双亲
public BTS() {
    r=null;
}

public void insertBTS(int id,char name,char sex,int classroom,int phone) {
    insert(r, id, name, sex, classroom, phone);
}
private Node insert(Node p,int id,char a1,char a12,int classroom,int phone) {
    if(p==null) {
        p=new Node();
        p.key=id;
    }
    else if(id<p.key)
        p.lchild=insert(p.lchild, id, a1, a12, classroom, phone);
    else if(id>p.key)
        p.rchild=insert(p.rchild, id, a1, a12, classroom, phone);
    return p;
}

public void create(char[][] a1) {
    r=new Node();
    r.key=a1[0][4];
    for(int i=0;i<a1.length;i++) {
        insert(r, a1[i][0], a1[i][1], a1[i][2], a1[i][3], a1[i][4]);
    }
}

public void show1(BTS x) {
    show(x.r);
}
public void show(Node p) {
    if(p!=null) {
        show(p.lchild);
        System.out.println(p.key+" ");
        show(p.rchild);
    }
}

}
package search;

public class BTS {

public static void main(String[] args) {
    // TODO Auto-generated method stub
    BTS bts = new BTS();
    Node r;
    char[][] arr = null;
    bts.create(arr);
    bts.insertBTS(1002, '王', '男', 2021053, 12345678);
    bts.show1(bts);
}

class Node{
    int key;
    Node lchild;
    Node rchild;
    Node(){
        lchild=rchild=null;
    }
}

public Node r;  //根节点
public Node f;  //存放待删除结点的双亲
public BTS() {
    r=null;
}

public void insertBTS(int id,char name,char sex,int classroom,int phone) {
    insert(r, id, name, sex, classroom, phone);
}
private Node insert(Node p,int id,char a1,char a12,int classroom,int phone) {
    if(p==null) {
        p=new Node();
        p.key=id;
    }
    else if(id<p.key)
        p.lchild=insert(p.lchild, id, a1, a12, classroom, phone);
    else if(id>p.key)
        p.rchild=insert(p.rchild, id, a1, a12, classroom, phone);
    return p;
}

public void create(char[][] a1) {
    r=new Node();
    r.key=a1[0][4];
    for(int i=0;i<a1.length;i++) {
        insert(r, a1[i][0], a1[i][1], a1[i][2], a1[i][3], a1[i][4]);
    }
}

public void show1(BTS x) {
    show(x.r);
}
public void show(Node p) {
    if(p!=null) {
        show(p.lchild);
        System.out.println(p.key+" ");
        show(p.rchild);
    }
}

}

  • 写回答

1条回答 默认 最新

  • pzzhao 2022-12-07 22:02
    关注

    你的 arr是个null, 然后你调用它的 a[0][4]报空指针了

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 系统已结题 12月15日
  • 已采纳回答 12月7日
  • 创建了问题 12月7日

悬赏问题

  • ¥15 远程访问linux主机超时
  • ¥15 odoo17存货管理优势于中国国内该行业传统ERP或MES的详细解读和举例
  • ¥15 CPU卡指令整合指令数据都在图片上
  • ¥15 火车票关联12306问题
  • ¥15 odoo17处理受托加工产品
  • ¥15 如何用MATLAB编码图三的积分
  • ¥15 圆孔衍射光强随孔径变化
  • ¥15 MacBook pro m3max上用vscode运行c语言没有反应
  • ¥15 ESP-PROG配置错误,ALL ONES
  • ¥15 结构功能耦合指标计算