0
class Fran
{
String name;
int size;
Fran(String name,int size)
{ this.name=name; this.size=size; }
String getName()
{ return this.name; }
int getSize()
{ return this.size; }
}
class Node
{
Fran fran;
Node preNode;
Node[] children=new Node[10];
int childCount=0;
Node child;
/* is there child? */
int isChild=0;
Node(Fran fran)
{
this.fran=fran;
}
Node(Node preNode,Fran fran)
{
this.fran=fran;
this.preNode=preNode;
}
void setChild(Node preNode,Node child)
{
this.preNode=preNode;
this.child=child;
this.children[childCount]=child;
this.isChild=1;
this.childCount++;
}
int getChildCount()
{ return childCount; }
Node preNode()
{ return this.preNode; }
String[] getName()
{
String[] t=new String[childCount];
if(childCount==0) {
t[0]=this.fran.getName();
return t;
}
else
{
for(int i=0;i<=childCount-1;i++)
{
t[i]=children[i].fran.getName();
}
return t;
}
}
int[] getSize()
{
int[] t=new int[childCount];
if(childCount==0) {
t[0]=this.fran.getSize();
return t;
}
else
{
for(int i=0;i<=childCount-1;i++)
{
t[i]=children[i].fran.getSize();
}
return t;
}
}
String getN()
{
return this.fran.getName();
}
}
void setup()
{
Fran aa=new Fran("apt",36);
Fran bb=new Fran("bpt",26);
Fran cc=new Fran("cpt",16);
/* Fran dd=new Fran("dpt",56); */
Node a=new Node(aa);
Node b=new Node(bb);
Node c=new Node(cc);
a.setChild(a,b);
a.setChild(a,c);
print(a.getN());
Node f=b.preNode();
print(f.getN());
}
- 我想做一个树结构。我想返回一个previouse节点。
- 节点a-> b,a-> c。并且我想让b.preNode() - > Node a。
- 但是我有NullpointerException,该如何解决这个问题?
谢谢我想制作一个树形图结构。我想找到一个previouse树
此代码错误标记。它不是java,它看起来像处理。它非常接近java。 – Dov