2019-07-14 19:29:58 +08:00
|
|
|
package trie
|
|
|
|
|
|
|
|
// Node is the trie's node
|
2022-11-02 22:28:18 +08:00
|
|
|
type Node[T any] struct {
|
2022-04-06 04:25:53 +08:00
|
|
|
children map[string]*Node[T]
|
2022-11-02 22:28:18 +08:00
|
|
|
inited bool
|
|
|
|
data T
|
2019-07-14 19:29:58 +08:00
|
|
|
}
|
|
|
|
|
2022-04-06 04:25:53 +08:00
|
|
|
func (n *Node[T]) getChild(s string) *Node[T] {
|
2019-07-14 19:29:58 +08:00
|
|
|
return n.children[s]
|
|
|
|
}
|
|
|
|
|
2022-04-06 04:25:53 +08:00
|
|
|
func (n *Node[T]) hasChild(s string) bool {
|
2019-07-14 19:29:58 +08:00
|
|
|
return n.getChild(s) != nil
|
|
|
|
}
|
|
|
|
|
2022-04-06 04:25:53 +08:00
|
|
|
func (n *Node[T]) addChild(s string, child *Node[T]) {
|
2019-07-14 19:29:58 +08:00
|
|
|
n.children[s] = child
|
|
|
|
}
|
|
|
|
|
2022-11-02 22:28:18 +08:00
|
|
|
func (n *Node[T]) isEmpty() bool {
|
|
|
|
if n == nil || n.inited == false {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *Node[T]) setData(data T) {
|
|
|
|
n.data = data
|
|
|
|
n.inited = true
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *Node[T]) Data() T {
|
|
|
|
return n.data
|
|
|
|
}
|
|
|
|
|
|
|
|
func newNode[T any]() *Node[T] {
|
2022-04-06 04:25:53 +08:00
|
|
|
return &Node[T]{
|
|
|
|
children: map[string]*Node[T]{},
|
2022-11-02 22:28:18 +08:00
|
|
|
inited: false,
|
|
|
|
data: getZero[T](),
|
2019-07-14 19:29:58 +08:00
|
|
|
}
|
|
|
|
}
|
2022-04-06 04:25:53 +08:00
|
|
|
|
2022-11-02 22:28:18 +08:00
|
|
|
func getZero[T any]() T {
|
2022-04-06 04:25:53 +08:00
|
|
|
var result T
|
|
|
|
return result
|
|
|
|
}
|