Trie / Radix Tree / Digital Tree
In computer science, a trie, also called digital tree and sometimes radix tree or prefix tree (as they can be searched by prefixes), is an ordered tree data structure that is used to store a dynamic set or associative array where the keys are usually strings.
Read More on:
https://en.wikipedia.org/wiki/Trie
Usage
You start by defining the root of the Trie. Using trie.NewTrie()
package main
import "github.com/claudiu/trie"
func main() {
rootTrie := trie.NewTrie()
rootTrie.Add("heart")
node := rootTrie.Find("heart")
}
You can now return the trie node using:
rootTrie.Find("heart")
If the node is not found rootTrie.Find("heart")
will return nil
Word Count
You can return the word count in a trie using:
rootTrie.Count()
Metadata
Each Trie node can hold metadata. Here's a small demo:
package main
import (
"github.com/claudiu/trie"
"fmt"
)
func main() {
rootTrie := trie.NewTrie()
rootTrie.Add("heart")
node := rootTrie.Find("heart")
node.Set("test", "alpha")
item, found := node.Get("test")
if found {
fmt.Println(item.(string))
}
}