三分探索木で指定された文字のリストを使用して、可能なすべての単語を見つけるプログラムを作成しました。出力は、すべての単語の並べ替えられたセットです。以下は、単語を見つけるための Python コードです。
def _find_words(self, node: Node, letters: str, buffer=''):
if node is None:
return
for c in letters:
if node.key < c:
self._find_words(node.high, letters, '')
elif node.key > c:
self._find_words(node.low, letters, '')
else:
buffer += node.key
if node.isEnd:
self.words.add(buffer)
self._find_words(node.equal,
letters.replace(node.key, '', 1), buffer)
ツリーには次の単語が含まれています: cat、acts、act、actor、at、bug、cats、up、actors、upper
文字「abugpscort」に対して次の出力が得られます。
['acts', 'cats', 'cat', 'act', 'bug', 'ors', 'up', 'or', 't']
一方、出力は次のようになります。
['actors', 'actor', 'acts', 'cats', 'cat', 'act', 'bug', 'up', 'at']
どうすればこれを修正できますか?