2017-03-25 15 views
0

でアクションを実行するためにどのように私は、各ノード(GameEntryクラス)が一つの「ゲームプレイ」(名/スコアのペア)を表し、バイナリ検索ツリーを持っています。ツリーは名前(スコアではなく)で整理されています。私はツリーのトップ10のスコア(対応する名前)のリストを印刷するためのメソッドを記述しようとしています。私は木を再帰的に横断することを考えました。ノードを配列(ScoreBoardクラス)に置くのは、それが高いスコアである場合のみです。私の問題を除いて、スコアボードが再帰の途中ですべてのステップを印刷するということを除いて、動作します。再帰は - だけで終わり

public void printTopTen() 
{ 
    ScoreBoard board = new ScoreBoard(10); // new scoreboard with capacity of 10 
    printTopTenRecur(this.root, board); 
} 

// Auxillary method for printTopTen() 
private void printTopTenRecur(GameEntry node, ScoreBoard board) 
{ 
    if (node == null) 
     { 
      return; 
     } 
    printTopTenRecur(node.getLeft(), board); 
    board.add(node); // adds the node to the scoreboard if it's a high score 
    System.out.println(board); 
    printTopTenRecur(node.getRight(), board); 
} 

私は考えることができる唯一の事は、クラス(boardと呼ばれる)の属性を作成し、再帰が行われた後、属性をプリントアウトすることでした。しかし、私はコンパイル時エラーvoid cannot be converted to Stringを得ています。私はそれをどうやって行うべきか分かりません。

public String printTopTen() 
{ 
    ScoreBoard board = new ScoreBoard(10); // new scoreboard with capacity of 10 
    printTopTenRecur(this.root, board); 
    return System.out.println(this.board); 
} 

// Auxillary method for printTopTen() 
private void printTopTenRecur(GameEntry node, ScoreBoard board) 
{ 
    if (node == null) 
     { 
      return; 
     } 
    printTopTenRecur(node.getLeft(), board); 
    board.add(node); // adds the node to the score board if it's a high score 
    this.board = board; // assign local board to the board on the tree 
    printTopTenRecur(node.getRight(), board); 
} 

答えて

1

しかし、私はコンパイル時のエラー・ボイドはあなたがSystem.out.println(this.board);Stringではありませんので、そのエラーを取得しているとprintTopTenStringを返すべきであると述べている文字列

に変換することはできません取得しています。

あなたがやりたいことすべては、あなたがこれを行うことができます再帰の終了時にボードを印刷している場合:

public void printTopTen() 
{ 
    ScoreBoard board = new ScoreBoard(10); // new scoreboard with capacity of 10 
    printTopTenRecur(this.root, board); 
    System.out.println(this.board); 
} 

これは、あなたがScoreBoardクラスのtoString方法で定義されているものは何でも表示されます。

何がやりたいことは返却された場合Stringあなたはこのようにそれを行うことができます。

public String printTopTen() 
{ 
    ScoreBoard board = new ScoreBoard(10); // new scoreboard with capacity of 10 
    printTopTenRecur(this.root, board); 
    return this.board.toString(); 
} 
+0

ありがとうございます。ご提案ありがとうございます。 'println'がvoidを返すようになりました。あなたの修正後、私は 'return this.board.toString()'も試みました。それはコンパイルされますが、ランタイムエラーNoSuchMethodErrorが発生します。もう一度混乱した。 – yroc

2

私は、特にありません、それはJavaの再帰の楽しいじゃない、主な理由は、あなたがリスクということです深すぎるとスタックオーバーフローが発生します。他の言語では、テールコールを暗黙的にwhileループ(例えばスカラーのような)に変換することができます。言われていること

は、戻り値なしの再帰は、私には本当に奇妙に聞こえるmoondaisyの提案は、あなたの問題に対処しながら、私はむしろ代わりにフィールドに頼るのスコアを返します。

private ScoreBoard printTopTenRecur(GameEntry node, ScoreBoard board){ 
    if(node == null) 
    return board; 

    board.add(node); 
    ScoreBoard leftBoard = printTopTenRecur(node.getLeft(), board); 
    ScoreBoard rightBoard = printTopTenRecur(node.getRight(), leftBoard); 

    return rightBoard; 
} 

public void printTopTen(){ 
    ScoreBoard board = new ScoreBoard(10); // new scoreboard with capacity of 10 
    // No need to return anything if you want to just print the result 
    System.out.println(printTopTenRecur(this.root, board)); 
} 

サイドノートScoreBoard leftBoard = printTopTenRecur(...)はそのようなかなり無用である、ボードはそう、それは十分合格変更可能です。私は再帰は私も不変考えると思いますので、私はむしろScoreBoard newBoard = board.update(node);はこのように、更新された新しいスコアボードを返す好きだろう

ScoreBoard currentBoard = board.update(node); 
    ScoreBoard leftBoard = printTopTenRecur(node.getLeft(), currentBoard); 
    ScoreBoard rightBoard = printTopTenRecur(node.getRight(), leftBoard); 

この方法printTopTenRecurは、副作用のない関数でありますしたがって、適切な機能です。

+0

あなたの考え方を見ていただきありがとうございます。 – yroc

関連する問題