java - Why pointers are shown instead of string in my inorder traversal of BST? -
here code printing inorder traversal of binary search tree: public class bstprint {
public void printinorder(bstnode root){ if (root!=null){ printinorder(root.getleftnode()); system.out.println(root.getnodevalue()); printinorder(root.getrightnode()); } } public static void main(string[] argc){ bstprint bstprint = new bstprint(); bstnode<string> root=new bstnode<string>(); root.setnodevalue("5"); bstnode<string> rootleft= new bstnode<string>(); rootleft.setnodevalue("3"); root.setleftnode(rootleft); bstnode<string> rootright= new bstnode<string>(); rootright.setnodevalue("8"); root.setrightnode(rootright); bstprint.printinorder(root); } }
here's bstnode class:
public class bstnode<e> { private e value; private bstnode<e> leftnode=null; private bstnode<e> rightnode=null; public bstnode getleftnode(){ return this.leftnode; } public void setleftnode(bstnode rootleft){ bstnode newleftnode=new bstnode(); newleftnode.leftnode=null; this.leftnode=newleftnode; newleftnode.value=rootleft; } public bstnode getrightnode(){ return this.rightnode; } public void setrightnode(bstnode rootright){ bstnode newrightnode=new bstnode(); newrightnode.rightnode=null; this.rightnode=newrightnode; newrightnode.value=rootright; } public e getnodevalue(){ return this.value; } public void setnodevalue(e value){ this.value=value; } }
why seeing result following?
bstnode@246f9f88 5 bstnode@1c52ac68
instead of
3 5 8
your setleft/right wrong:
they should be:
public void setrightnode(bstnode rootright){ this.rightnode=rootright; } public void setleftnode(bstnode rootleft){ this.leftnode=rootleft; }
you have node - need set it. there no need create node object.
hint: if @ java warnings in ide find complains should parameterize of values (always use bstnode in parts of bstnode). once add it, tell cannot conver bstnode e in set*node functions.
Comments
Post a Comment