12 Star 47 Fork 9

ppmoon / gbt2260

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
trie.go 916 Bytes
一键复制 编辑 原始数据 按行查看 历史
刘允鹏 提交于 2020-07-18 18:39 . 增加单例
package gbt2260
//节点
type Node struct {
value string
children map[string]*Node
}
//跟树
type Trie struct {
root *Node
}
//创建一颗新树
func NewTrie() *Trie {
return &Trie{
root: &Node{
children: make(map[string]*Node),
},
}
}
//返回跟节点
func (t *Trie) Root() *Node {
return t.root
}
//添加节点
func (t *Trie) Add(lCode []string, name string) *Node {
node := t.root
for i := range lCode {
r := lCode[i]
if n, ok := node.children[r]; ok {
node = n
} else {
// 否则就创建这个节点
node = node.NewChild(r, name)
}
}
return node
}
//创建并返回一个新子节点的指针这里的key
func (n *Node) NewChild(key string, value string) *Node {
node := &Node{
value: value,
children: make(map[string]*Node),
}
n.children[key] = node
return node
}
// 返回一个子叶
func (n Node) Children() map[string]*Node {
return n.children
}
Go
1
https://gitee.com/ppmoon/gbt2260.git
git@gitee.com:ppmoon/gbt2260.git
ppmoon
gbt2260
gbt2260
master

搜索帮助