Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Postorder tree

807606Mar 16 2007 — edited Mar 17 2007
Hello everybody,
I've to create a postorder method for a tree (without using java classes like "tree" or similar).
The tree is made in this way:
public class MyTree
{
	private BinNode root;

	 private class BinNode{
        public BinNode left;
        public BinNode right;
        public int label;
        }
}
The real problem I have is that my method is NOT void, so I can't simply do:
if(left!=null) return left.toStringIntern();	
if(right!=null)	return right.toStringIntern();
System.out.println(label);
because, at the end of the code, I need to return a value. I think this is the problem, because if I see just the last node of the tree..probably after one System.out I return the value and so I can't see the other nodes..but how can I fix it?
I'll post you the code:
 public String toString(){
	if(root!=null){
		return root.toStringIntern();
	}
	else
		return "";
}
   public String toStringIntern(){

	if(left!=null)	       return left.toStringIntern();
	if(right!=null)      return right.toStringIntern();
				
        System.out.println(label);
	return "";  //I'll need to return a string later..but it's complicated because I've to create a string like [_,1,[[_,3,_],2,_]]
		
  }
null

Message was edited by:
Pipkin
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Apr 14 2007
Added on Mar 16 2007
21 comments
188 views