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; /* 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()); }
- i want make tree structure. , want return previouse node.
- node a->b, a->c . , want make b.prenode() -> node a.
- but have nullpointerexception, how can solve problem?
thank you
first, have decide want do. have not made clear @ all. root, , b , c children? or root, b child, , c child of b?
you need add method add child node:
node = new node (...); node b = new node(...); a.add(b); // should hook them
the add method should set parent of b a, , set child of b.
in addition, note have stylistic mistakes. should remove code setting variables , put in constructor. hard @ moment see happens when create node because have code spread on whole class.
you should consider removing fran class , put data in node. why have 2 classes? adds overhead, complexity, , not seem achieve anything. that's style, clean code lot.
Comments
Post a Comment