PHP 生成 Trie 树
更新:HHH   时间:2023-1-7


将所有敏感词生成 Trie 树结构,便于做敏感词检测,生成代码如下

class TrieNode
{

    private static $TrieTree;

    public function __construct()
    {
        static::$TrieTree = [];
    }

    public function insert($sensWords): TrieNode
    {
        $words = preg_split('//u', $sensWords, -1, PREG_SPLIT_NO_EMPTY);
        $_tree =  &static::$TrieTree;
        foreach ($words as $key => $_word) {
            if (!isset($_tree[$_word])) {
                $_tree[$_word] = [
                    'isEnd' => !isset($words[$key + 1]),
                    'child' => []
                ];
            }
            $_tree = &$_tree[$_word]['child'];
        }
        return $this;
    }

    public function getTree()
    {
        return static::$TrieTree;
    }
}

$treeNode = (new TrieNode)->insert('CNM')->insert('MLGB')->insert('WRNM')->getTree();
echo json_encode($treeNode,JSON_UNESCAPED_UNICODE);
返回web开发教程...