単語の膨大なコレクションを格納するプレフィックス ツリーがあります。現在、「a」という一般的な接頭辞を持つすべての単語を見つけたい場合は、最初に a を含む最初のノードを取得し、次に最初のノードの子ノードで深さ優先方式で徹底的に検索します。このアイデアは素朴で簡単に見えますが、共通の接頭辞を持つ単語の数が非常に多い (>20K) 場合、実際には非常に遅くなります。共通の接頭辞で始まるすべての単語を効率的に取得する他の方法はありますか? または、他のデータ構造を採用する必要がありますか? よろしくお願いします。
EDIT1 基本的に、すべてのノードにアクセスして文字を段階的に追加することにより、完全な単語を作成しています。すべての単語は、後でベクター コンテナーに格納されます。はい、再帰的な実装があります。
EDIT2
vector<int> getNonEmptyEdgeIndices(Node* parent) {
vector<int> indices;
for(int i=0; i<EDGE; i++) {
if (parent->edges[i] != NULL) {
indices.push_back(i);
}
}
return indices;
}
vector<string> getSubsequentStrings(vector<string> wordsStartingWith, Node* node, string prefix) {
vector<int> indices = getNonEmptyEdgeIndices(node);
// push the word to the container if node is a leaf
if (indices.empty()) {
wordsStartingWith.push_back(prefix);
return wordsStartingWith;
}
// if frequency is set in node, push the word but still continue recursion
if (node->frequency != 0) {
wordsStartingWith.push_back(prefix);
}
// look all the children of the node
for(unsigned int i=0; i<indices.size(); i++) {
string newPrefix = prefix + getNodeChar(indices[i]);
Node* child = node->edges[indices[i]];
// recursively get the prefix for all children
wordsStartingWith = getSubsequentStrings(wordsStartingWith, child, newPrefix);
}
return wordsStartingWith;
}
vector<string> Trie::getWordsStartingWith(string prefix) {
vector<string> wordsStartingWith;
Node* lastNode = getLastNode(prefix);
if (lastNode != NULL) {
wordsStartingWith = getSubsequentStrings(wordsStartingWith, lastNode, prefix);
}
return wordsStartingWith;
}
編集3 解決!!! 実際、私の実装には問題がありました。私はこの巨大なベクトル文字列コンテナを再帰呼び出しで渡していましたが、これが実際の問題でした。皆さん、親切なアドバイスをありがとうございます。