3

楽しいという理由だけで、今日はトライを実装しました。現時点では 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;
    }
}
4

2 に答える 2

3

PHPはわかりませんが、

次の方法で:

   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; 
    } 

なぜ必要なのですか$str .= $char(私は追加だと思います)?これ自体が、O(n) 時間の追加/検索を O(n) ではなく Omega(n^2) (n は の長さ) に変更$valueします。

トライでは、通常、文字列をウォークしながらトライをウォークします。つまり、現在のプレフィックスではなく、現在の文字に基づいて次のノードを見つけます。

于 2010-07-20T18:05:28.133 に答える
1

この実装は、Key|value 型の挿入と検索のためのものだと思いますか? こちらは【英語】の単語を扱うものです。

class Trie {


static function insert_word(Node $root, $text) 
{
    $v = $root;
    foreach(str_split($text) as $char) {
    $next = $v->children[$char];
        if ($next === null)
        {
            $v->children[$char] = $next = new Node();
        }
        $v = $next;
    }

    $v->leaf = true;
}


static function get_words_sorted(Node $node, $text) 
{

    $res = array();  
    for($ch = 0; $ch < 128; $ch++) {
    $child = $node->children[chr($ch)];

        if ($child !== null)
        {
            $res = array_merge($res, Trie::get_words_sorted($child, $text . chr($ch)));

        }
    }
    if ($node->leaf === true) 
    {
        $res[] = $text;
    }
    return $res;

}

static function search(Node $root, $text) 
{
    $v = $root;
    while($v !== null)
    {
        foreach(str_split($text) as $char) {
            $next = $v->children[$char];
            if ($next === null)
            {
                return false;
            }
            else
            {
                $v = $next;
            }
        }

        if($v->leaf === true)
        {
            return true;
        }
        else
        {
            return false;
        }

    }
    return false;

}

}


class Node {

    public $children;
    public $leaf;


    function __construct()
    {
        $children = Array();

    }
}

使用例

    $root = new Node();
    $words = Array("an", "ant", "all", "allot", "alloy", "aloe", "are", "ate", "be");


    for ($i = 0; $i < sizeof($words); $i++)
    {

        Trie::insert_word($root, $words[$i]);
    }

    $search_words = array("alloy", "ant", "bee", "aren't", "allot");

    foreach($search_words as $word)
    {
        if(Trie::search($root, $word) === true)
        {
            echo $word . " IS in my dictionary<br/>";
        }
        else
        {
            echo $word . " is NOT in my dictionary <br/>";
        }
    }
于 2014-10-20T13:44:09.007 に答える