楽しいという理由だけで、今日はトライを実装しました。現時点では add() と search() をサポートしており、remove() も実装する必要がありますが、それはかなり簡単だと思います。
それは完全に機能しますが、Trie にデータを入力するには少し時間がかかりすぎます。このリストをデータソースとして使用しています: http://www.isc.ro/lists/twl06.zip (SO の別の場所にあります)。読み込みには最大 11 秒かかります。私の最初の実装には約15秒かかったので、すでにパフォーマンスが向上しましたが、まだ満足していません:)
私の質問は、他に何が (実質的な) パフォーマンスの向上をもたらすでしょうか? 私はこの設計に拘束されません。完全なオーバーホールは受け入れられます。
class Trie
{
private $trie;
public function __construct(TrieNode $trie = null)
{
if($trie !== null) $this->trie = $trie;
else $this->trie = new TrieNode();
$this->counter = 0;
}
public function add($value, $val = null)
{
$str = '';
$trie_ref = $this->trie;
foreach(str_split($value) as $char)
{
$str .= $char;
$trie_ref = $trie_ref->addNode($str);
}
$trie_ref->value = $val;
return true;
}
public function search($value, $only_words = false)
{
if($value === '') return $this->trie;
$trie_ref = $this->trie;
$str = '';
foreach(str_split($value) as $char)
{
$str .= $char;
if($trie_ref = $trie_ref->getNode($str))
{
if($str === $value) return ($only_words ? $this->extractWords($trie_ref) : new self($trie_ref));
continue;
}
return false;
}
return false;
}
public function extractWords(TrieNode $trie)
{
$res = array();
foreach($trie->getChildren() as $child)
{
if($child->value !== null) $res[] = $child->value;
if($child->hasChildren()) $res = array_merge($res, $this->extractWords($child));
}
return $res;
}
}
class TrieNode
{
public $value;
protected $children = array();
public function addNode($index)
{
if(isset($this->children[$index])) return $this->children[$index];
return $this->children[$index] = new self();
}
public function getNode($index)
{
return (isset($this->children[$index]) ? $this->children[$index] : false);
}
public function getChildren()
{
return $this->children;
}
public function hasChildren()
{
return count($this->children)>0;
}
}