0

ノード クラスとツリー クラスを作成しました。メインから、suffixTree t = new suffixTree(string); を呼び出します。while ループ上にあるため、常に変数 t になります。

問題は、入力ファイルを読み取り、文字列ごとに新しいツリーを作成したいということです。どうやら、それは新しいインスタンスを作成しません。

変数「t」は各インタラクションで同じですが、作成するたびに新しいインスタンスになる必要があります。ツリー コンストラクターには Node root = new Node(); があります。

これはコピーされたコードです。私が行った唯一のことは、入力から読み取り、ツリーをトラバースすることでした。

問題は、misissippi$ と入力してから acacdcacd$ と入力すると、同じツリーに追加され、トラバースすると間違った結果が返されることです。

前もって感謝します

4

1 に答える 1

0

新しいインスタンスを作成していますが、それらはどこにも保存されておらず、到達不能であり、ガベージ コレクションの準備ができているとマークされているため、作成直後にそれらを破棄しています。

以下をご覧ください。

// this is where the SuffixTree instances will end up
List<SuffixTree> suffixes = new ArrayList<SuffixTree>();


String[] strings = new String[3];
for (String string : strings) {
    // A new suffix tree is created here but if you don't do anything
    // with it then it is marked as garbage collectable when the closed
    // curly brace is reached
    SuffixTree t = new SuffixTree(string);


    // Now I'm pushing the object into the suffixes list: this will prevent
    // the loss of the object currently stored in the t variable
    suffixes.add(t);
}
于 2012-12-14T15:58:36.117 に答える