I'm working with a method which converts a binary tree into a string with the tree in parentheses notation. Here is what I got so far:
//both of this methods are in the tree class,
//so every other method or variable are directly visible
/*this method creates the string, and then
* calls another method to fill the string with the
* tree in pre-order, and then returns the string
already filled.*/
public String linealNotation(){
String line = new String();
linearize(line,root); //root is the Node which starts the tree.
return line;
}
//this method is the one with fills the string with an pre-order reading.
private void linearize(String line, Node n){
if(n==null)
return;
line.concat(""+n.data); //this is my cry-blood way to insert the
line.concat("("); //int stored in the node into the string
linearize(line,n.left);
line.concat(",");
linearize(line,n.right);
line.concat(")");
}
But when I print the string returned by my method, nothing appears, and String.length() returns me a zero.
Maybe the concat ways in my method are wrong, but I'm not very used in strings sciences.
linefromlinealNotationis not modified bylinearize(concactdoes not change the contents of the String), which means that wherever you use the return value fromlinealNotation, it will be an empty string.Strings are /immutable/ in Java, you can't "insert" anything into them or change their contents in any way. You can only make new ones based on the old ones' contents.