普贤教程 › 包参考
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_insert | t, s |
tr_search | t, s |
tr_starts_with | t, prefix |
tr_to_list | t |
tr_keys_with_prefix | t, prefix |
tr_len | t |
其余 4 个 def(内部与辅助,可直接调用但不属稳定接口)
| 函数 | 参数 |
|---|---|
tr_collect | node, path, out |
tr_delete | t, s |
tr_longest_prefix | t, s |
tr_size | t |
包内说明(源码头注释)
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),中文等多字节词可直接用。 ============================================================