0

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.

2
  • line from linealNotation is not modified by linearize(concact does not change the contents of the String), which means that wherever you use the return value from linealNotation, it will be an empty string. Commented Nov 6, 2016 at 5:00
  • To put a finer point on the previous comments, 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. Commented Nov 6, 2016 at 5:04

2 Answers 2

2

A String is immutable - you can't change its contents. The concat method returns a new String, rather than adding to an existing one.

What you want to do is use StringBuilder instead of String. Your code should look like this. Note

  • the use of toString in the linealNotation method, to convert the StringBuilder back into a String.
  • the use of the append method to concatenate the data together.

.

public String linealNotation(){
    StringBuffer line = new StringBuffer();
    linearize(line,root); 
    return line.toString();
}


private void linearize(StringBuilder line, Node n){
    if (n==null) {
        return;
    }
    line.append(n.data); 
    line.append("(");       
    linearize(line,n.left);
    line.append(",");
    linearize(line,n.right);
    line.append(")");
}
Sign up to request clarification or add additional context in comments.

Comments

0

You should make line variable's data type either StringBuffer or StringBuilder.

Because string is immutable in Java,so when you are trying to concat(means mutate in this context), it will not work.

Or, if you are stick to String, then you should make returned concat string refer to line again i.e.

line = line.concat("blahblah");

But it is inefficient slightly.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.