|
|
|
@@ -102,10 +102,15 @@ func (n *Node[T]) RemoveSelf(cleanParent bool) { |
|
|
|
} |
|
|
|
|
|
|
|
// 修改时需要注意允许在visitorFn中删除当前节点 |
|
|
|
func (n *Node[T]) Iterate(visitorFn func(word string, node *Node[T], isWordNode bool) VisitCtrl) { |
|
|
|
func (n *Node[T]) Iterate(visitorFn func(path []string, node *Node[T], isWordNode bool) VisitCtrl) { |
|
|
|
n.iterate([]string{}, visitorFn) |
|
|
|
} |
|
|
|
|
|
|
|
func (n *Node[T]) iterate(path []string, visitorFn func(path []string, node *Node[T], isWordNode bool) VisitCtrl) { |
|
|
|
if n.WordNexts != nil { |
|
|
|
for word, node := range n.WordNexts { |
|
|
|
ret := visitorFn(word, node, true) |
|
|
|
newPath := append(path, word) |
|
|
|
ret := visitorFn(newPath, node, true) |
|
|
|
if ret == VisitBreak { |
|
|
|
return |
|
|
|
} |
|
|
|
@@ -114,12 +119,13 @@ func (n *Node[T]) Iterate(visitorFn func(word string, node *Node[T], isWordNode |
|
|
|
continue |
|
|
|
} |
|
|
|
|
|
|
|
node.Iterate(visitorFn) |
|
|
|
node.iterate(newPath, visitorFn) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
if n.AnyNext != nil { |
|
|
|
ret := visitorFn("", n.AnyNext, false) |
|
|
|
newPath := append(path, "") |
|
|
|
ret := visitorFn(newPath, n.AnyNext, false) |
|
|
|
if ret == VisitBreak { |
|
|
|
return |
|
|
|
} |
|
|
|
@@ -128,7 +134,7 @@ func (n *Node[T]) Iterate(visitorFn func(word string, node *Node[T], isWordNode |
|
|
|
return |
|
|
|
} |
|
|
|
|
|
|
|
n.AnyNext.Iterate(visitorFn) |
|
|
|
n.AnyNext.iterate(newPath, visitorFn) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
@@ -198,6 +204,6 @@ func (t *Trie[T]) CreateWords(words []string) *Node[T] { |
|
|
|
return ptr |
|
|
|
} |
|
|
|
|
|
|
|
func (n *Trie[T]) Iterate(visitorFn func(word string, node *Node[T], isWordNode bool) VisitCtrl) { |
|
|
|
func (n *Trie[T]) Iterate(visitorFn func(path []string, node *Node[T], isWordNode bool) VisitCtrl) { |
|
|
|
n.Root.Iterate(visitorFn) |
|
|
|
} |