私は、バイナリツリーを偽りの表記法でツリーの文字列に変換する方法を開発中です。ここで私はこれまで得たものである:Java:バイナリ検索ツリーを文字列に変換する方法
//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 retuns the string
alredy filled.*/
public String linealNotation(){
String line = new String();
linearize(line,root); //root is the Node wich 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(")");
}
しかし、私は私のメソッドによって返された文字列を印刷するとき、何も表示されない、とString.lengthです()私にはゼロを返します。
おそらく私の方法での連結方法は間違っていますが、私は文字列科学であまり使用されていません。
'' linealNotation'からline'が* *あなたは 'linealNotation'からの戻り値を使用する場所にあることを意味' linearize'( 'concact'は、文字列の内容を変更しない)、によって変更されません、それは空の文字列になります。 String'sはJavaで/ /不変である '、以前のコメントに細かい点を入れて – Michael
、あなたは彼らに何かを「挿入」またはいずれかの方法で、その内容を変更することはできません。あなたは古いものの内容に基づいてのみ新しいものを作ることができます。 – BadZen