class trie
{
private $trie;
function __construct()
{
$trie = array('children' => array(),'isword'=>false);
}
/**
* 把词加入词典
*
* @param string $key
*/
function &setword($word='')
{
$trienode = &$this->trie;
for($i = 0;$i < strlen($word);$i++)
{
$character = $word[$i];
if(!isset($trienode['children'][$character]))
{
$trienode['children'][$character] = array('isword'=>false);
}
if($i == strlen($word)-1)
{
$trienode['children'][$character] = array('isword'=>true);
}
$trienode = &$trienode['children'][$character];
}
}
/**
* 判断是否为词典词
*
* @param string $word
* @return bool true/false
*/
function & isword($word)
{
$trienode = &$this->trie;
for($i = 0;$i < strlen($word);$i++)
{
$character = $word[$i];
if(!isset($trienode['children'][$character]))
& |