一个2叉树的问题 为什么运行会不对?

Peking 2002-03-20 02:01:40
import java.io.*;
class
{
public static void main(String args[])
{

int i=0;
int t=0;
int Data=-1;
node CurrentNode;
node[] tree=new node[10];

try{
System.out.println("please input:");

//in=(DataInputStream)(DataOutputStream(System.in));
Data=System.in.read();
}catch(IOException e)
{}
//Data=Integer.parseInt(r);

tree[i].setData(Data);
tree[i].leftnode=null;
tree[i].rightnode=null;

for(i=0;i<10;i++)
{
System.out.println("please input a number:");
{
while(tree[t].data!=0)
{
if(tree[t].data>=Data)
tree[t]=tree[t].leftnode;
else
tree[t]=tree[t].rightnode;
}
tree[t].setData(Data);
tree[t].setLeftnode(null);
tree[t].setRightnode(null);
}
}
t++;
}
class node //define a node class for a tree node
{
public int data; // node class's content
public node leftnode;
public node rightnode;

public node()
{
data=0;
leftnode=null;
rightnode=null;
}
public void node()
{
data=0;
leftnode=null;
rightnode=null;
}


public void setData(int t)
{
data=t;
}
public int getData(int t)
{
return data;
}
public node getLeftnode()
{
return leftnode;
}
public void setLeftnode(node b)
{
leftnode=b;
}
public node getRighttnode(node a)
{
return rightnode;
}
public void setRightnode(node b)
{
rightnode=b;
}
}
}
编译正确 运行报错
please input:
9
Exception in thread "main" java.lang.NullPointerException
at tree2.main(tree2.java:22)

希望大家帮忙


...全文
48 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
Iong 2002-03-20
  • 打赏
  • 举报
回复
不知道你想用来做什么
改了一下看看

import java.io.*;
class TreeUse
{
public static void main(String args[])
{
String theData = "-1";
Node tree;
DataInputStream di = new DataInputStream(System.in);
try{
theData=di.readLine();
}catch(IOException e)
{}

tree = new Node(new Integer(theData).intValue());

for(int i=0;i<10;i++)
{
try {

theData=di.readLine();
} catch (IOException e) {}
tree.addNode(new Integer(theData).intValue());
}
tree.display();
}
}
class Node //define a Node class for a tree Node
{
int data; // Node class's content
Node leftNode;
Node rightNode;

public Node()
{
data=0;
leftNode=null;
rightNode=null;
}

public Node(int data)
{
this.data = data;
leftNode = null;
rightNode = null;
}

public void setData(int t)
{
data=t;
}
public int getData()
{
return data;
}
public Node getLeftNode()
{
return leftNode;
}
public void setLeftNode(Node b)
{
leftNode= b;
}
public Node getRightNode()
{
return rightNode;
}
public void setRightNode(Node b)
{
rightNode= b;
}
public void addNode(int data) {
if (this.getData() < data) {
if (rightNode != null) {
rightNode.addNode(data);
} else {
rightNode = new Node(data);
}
} else if (this.getData() > data) {
if (leftNode != null) {
leftNode.addNode(data);
} else {
leftNode = new Node(data);
}
}
}
public void display() {
if (leftNode != null)
leftNode.display();
System.out.print(data + " ");
if (rightNode != null)
rightNode.display();
}
}
ChDw 2002-03-20
  • 打赏
  • 举报
回复
你把你的程序贴出来看看,标明行号
Peking 2002-03-20
  • 打赏
  • 举报
回复
还是不行。。。。
please input:
9
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException
at tree2.main(tree2.java:26)
Press any key to continue...

能告诉我为什么要这么改吗?
skyyoung 2002-03-20
  • 打赏
  • 举报
回复
static class node
{
...
}
Peking 2002-03-20
  • 打赏
  • 举报
回复
还是不行啊。。。。

D:\project\ds\tree2.java:14: non-static variable this cannot be referenced from a static context
tree[i]=new node();
^
1 error
ChDw 2002-03-20
  • 打赏
  • 举报
回复
sorry,应该是new node();
ChDw 2002-03-20
  • 打赏
  • 举报
回复
加入
for(i = 0 ; i < 10; i++)
tree[i] = new tree();

23,407

社区成员

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

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