node.go 491 B

1234567891011121314151617181920212223242526272829303132
  1. package stringx
  2. type node struct {
  3. children map[rune]*node
  4. end bool
  5. }
  6. func (n *node) add(word string) {
  7. chars := []rune(word)
  8. if len(chars) == 0 {
  9. return
  10. }
  11. nd := n
  12. for _, char := range chars {
  13. if nd.children == nil {
  14. child := new(node)
  15. nd.children = map[rune]*node{
  16. char: child,
  17. }
  18. nd = child
  19. } else if child, ok := nd.children[char]; ok {
  20. nd = child
  21. } else {
  22. child := new(node)
  23. nd.children[char] = child
  24. nd = child
  25. }
  26. }
  27. nd.end = true
  28. }