px普贤教程PuXian
普贤教程 › 包参考

trie 包

字典树 / 前缀匹配

项值
包名trie
版本0.2.0
可用版本0.1.0 / 0.2.0
入口trie.px
分类数据结构
文件数 / 行数1 / 126

安装与引入

bash
pxpkg init          # 首次
pxpkg add trie@0.2.0
pxpkg install
px
import trie
所属分类教程:数据结构

公开接口

函数参数
tr_new
tr_insertt, s
tr_searcht, s
tr_starts_witht, prefix
tr_to_listt
tr_keys_with_prefixt, prefix
tr_lent
其余 4 个 def(内部与辅助,可直接调用但不属稳定接口)
函数参数
tr_collectnode, path, out
tr_deletet, s
tr_longest_prefixt, s
tr_sizet

包内说明(源码头注释)

text
============================================================
普贤生态库:registry/trie —— 字典树 / 前缀匹配
------------------------------------------------------------
纯函数库(无 IO、无状态,树状态以嵌套 dict 显式传递),双模式一致。
tr_new()                  → dict   空树根
tr_insert(t, s)           → t      插入一个词(可链式)
tr_search(t, s)           → bool   精确匹配
tr_starts_with(t, prefix) → bool   是否存在以 prefix 开头的词
tr_keys_with_prefix(t, prefix) → [str]  所有以 prefix 开头的词(未排序)
tr_to_list(t)             → [str]  全部词
tr_len(t)                 → int    词数
结构:嵌套 dict;子节点键 = 单字符(rune),词尾用元数据键 "end" 标记
(子键恒为单字符,多字符 "end" 不会冲突)。
索引按 rune(M185 起 s[i] 为 rune),中文等多字节词可直接用。
============================================================