Skip to content

Instantly share code, notes, and snippets.

@bign8
Created August 27, 2016 23:10
Show Gist options
  • Save bign8/0d1b1e2aa256049cb55fbef17676910c to your computer and use it in GitHub Desktop.
Save bign8/0d1b1e2aa256049cb55fbef17676910c to your computer and use it in GitHub Desktop.
package example
type trie map[string]*trie
func (t *trie) Add(parts []string) {
if len(parts) == 0 {
return
}
child, ok := (*t)[parts[0]]
if !ok {
child = &trie{}
(*t)[parts[0]] = child
}
child.Add(parts[1:])
}
func (t *trie) Prefix(max int) []string {
if len(*t) != 1 || max == 0 {
return nil
}
var key string
for key = range *t {
/* nothing */
}
return append([]string{key}, (*t)[key].Prefix(max-1)...)
}
type walker func(val string, parent interface{}) interface{}
func (t *trie) Walk(start interface{}, walk walker) []interface{} {
var res []interface{}
for key, value := range *t {
parent := walk(key, start)
res = append(res, parent)
res = append(res, value.Walk(parent, walk)...)
}
return res
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment