2019-07-14 19:29:58 +08:00
|
|
|
package trie
|
|
|
|
|
|
|
|
// Node is the trie's node
|
|
|
|
type Node struct {
|
|
|
|
children map[string]*Node
|
2022-03-16 12:10:13 +08:00
|
|
|
Data any
|
2019-07-14 19:29:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (n *Node) getChild(s string) *Node {
|
2022-04-09 22:13:45 +08:00
|
|
|
if n.children == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-07-14 19:29:58 +08:00
|
|
|
return n.children[s]
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *Node) hasChild(s string) bool {
|
|
|
|
return n.getChild(s) != nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *Node) addChild(s string, child *Node) {
|
2022-04-09 22:13:45 +08:00
|
|
|
if n.children == nil {
|
|
|
|
n.children = map[string]*Node{}
|
|
|
|
}
|
|
|
|
|
2019-07-14 19:29:58 +08:00
|
|
|
n.children[s] = child
|
|
|
|
}
|
|
|
|
|
2022-03-16 12:10:13 +08:00
|
|
|
func newNode(data any) *Node {
|
2019-07-14 19:29:58 +08:00
|
|
|
return &Node{
|
|
|
|
Data: data,
|
2022-04-09 22:13:45 +08:00
|
|
|
children: nil,
|
2019-07-14 19:29:58 +08:00
|
|
|
}
|
|
|
|
}
|