免费视频淫片aa毛片_日韩高清在线亚洲专区vr_日韩大片免费观看视频播放_亚洲欧美国产精品完整版

打開APP
userphoto
未登錄

開通VIP,暢享免費(fèi)電子書等14項(xiàng)超值服

開通VIP
Trie樹的php實(shí)現(xiàn)
本文原創(chuàng),轉(zhuǎn)載請注明出處
<?php
/**
 * Trie樹
 * 
 * @author iamlaobie@gmail.com
 * @since 2012-08-19
 */
class Trie
{  
    /**
     * 樹的存儲
     */
    private $_tree = array();

    /**
     * 插入一個(gè)新詞
     * 
     * @param $word 詞
     * @param $data 詞附加屬性
     */
    public function insert($word, $data = array())
    {
        $cur = & $this->_tree;
        for($i = 0, $len = strlen($word); $i < $len; $i++){
            $ascValue = ord($word[$i]);
            if(!isset($cur[$ascValue])){
                $cur[$ascValue] = array();
            }

            if($i == $len - 1){
                $data['text'] = $word;
                $cur[$ascValue]['$'] = $data;
                break;
            }else{
                $cur = & $cur[$ascValue];
            }
        }
    }
    
    /**
     * 查找詞
     */
    public function find($word)
    {
        $cur = & $this->_tree;
        for($i = 0, $len = strlen($word); $i < $len; $i++){
            $ascValue = ord($word[$i]); 
            if(!isset($cur[$ascValue])){
                return null;
            }
            
            if($i == $len - 1){
                if(isset($cur[$ascValue]['$'])){
                    return $cur[$ascValue]['$'];
                }
            }else{
                $cur = $cur[$ascValue];
            }
        }
        return null;
    }

    /**
     * 獲取以$word為前綴的詞
     * 
     * @param $depth 后綴長度
     */
    public function suffixes($word, $depth = 1)
    {
        $cur = & $this->_tree;
        $words = array();
        for($i = 0, $len = strlen($word); $i < $len; $i++){
            $ascValue = ord($word[$i]);
            if(!isset($cur[$ascValue])){
                return null;
            }

            if($i == $len - 1){
                return $this->traverse($cur, $depth); 
            }else{
                $cur = $cur[$ascValue];
            }
        }
        return null; 
    }

    /**
     * 遍歷
     */
    public function traverse($tree = null, $depth = 1, $words = array(), $recuDepth = 0)
    {
        if($tree === null){
            $tree = $this->_tree;
        }

        $recuDepth += 1;
        unset($tree['$']);
         
        //中文utf8編碼長度為3
        $realDepth = $depth * 3;
        foreach($tree as $ascValue => $subTree){
            if(isset($subTree['$'])){
                $words[] = $subTree['$'];    
            }
            if(!empty($subTree) && $recuDepth <= $realDepth){
                $words = $this->traverse($subTree, $depth, $words, $recuDepth);
            }
        }
        return $words;
    }

    /**
     * 打印樹
     */
    public function tree()
    {
        print_r($this->_tree);
    }
}

//測試
$trie = new Trie();

$trie->insert("中國人", array('a' => 1));
$trie->insert("中國通", array('a' => 1));
$trie->insert("中國人地", array('a' => 1));
$trie->insert("中國人民", array('a' => 1));
$trie->insert("中方通", array('a' => 2));
$trie->insert("abc", array('a' => 2));
$trie->insert("外方", array('a' => 2));

var_dump($trie->suffixes('中國人', 1));

本站僅提供存儲服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點(diǎn)擊舉報(bào)
打開APP,閱讀全文并永久保存 查看更多類似文章
猜你喜歡
類似文章
2008
AC算法詳解
trie樹
Trie樹
glib庫數(shù)組GArray介紹
JS之?dāng)?shù)組的幾個(gè)牛逼操作~面試高頻
更多類似文章 >>
生活服務(wù)
分享 收藏 導(dǎo)長圖 關(guān)注 下載文章
綁定賬號成功
后續(xù)可登錄賬號暢享VIP特權(quán)!
如果VIP功能使用有故障,
可點(diǎn)擊這里聯(lián)系客服!

聯(lián)系客服