
Product
Announcing Socket Fix 2.0
Socket Fix 2.0 brings targeted CVE remediation, smarter upgrade planning, and broader ecosystem support to help developers get to zero alerts.
github.com/gtr/trie
a quick and effective trie library implementation for go
use the go get
command:
go get github.com/gtr/trie
import the library into your go main file:
import "github.com/gtr/trie"
example usage:
package main
import (
"fmt"
"log"
"github.com/gtr/trie"
)
func main() {
t := trie.NewTrie()
t.InsertWord("hello")
t.InsertWords([]string{
"hi",
"hoop",
"hook",
"breakfast",
"brunch",
"brush",
"bank",
})
auto, err := t.AutoComplete("br")
if err != nil {
log.Fatalf("AutoComplete: %s", err)
}
for _, word := range auto {
fmt.Println(word)
}
all := t.GetAllWords()
fmt.Println("----")
for _, word := range all {
fmt.Println(word)
}
}
output:
breakfast
brush
brunch
----
hoop
hook
hello
hi
breakfast
brunch
brush
bank
type Trie struct {
Root *Node
}
Trie represents a trie object.
func NewTrie() *Trie
NewTrie returns a pointer to an empty trie.
func (t *Trie) InsertWord(word string)
InsertWord inserts a new word into the trie.
func (t *Trie) InsertWords(words []string)
InsertWords inserts multiple words into the trie.
func (t *Trie) FindWord(word string) bool
FindWord returns a bool if a word exists in the trie.
func (t *Trie) GetAllWords() []string
GetAllWords returns a slice of strings containing all the words in the entire trie.
func (t *Trie) AutoComplete(prefix string) ([]string, error)
AutoComplete returns a slice of strings containing all the possible words that can autocomplete the given prefix.
type Node struct {
IsWord bool
Children map[rune]*Node
}
Node represents a Node in the trie.
func NewNode() *Node
NewNode returns a pointer to an empty node.
func (n *Node) GetAllSubWords(curr string) []string
GetAllSubWords returns a slice of strings containing all the words in the subtrie contained in the current node n.
FAQs
Unknown package
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Product
Socket Fix 2.0 brings targeted CVE remediation, smarter upgrade planning, and broader ecosystem support to help developers get to zero alerts.
Security News
Socket CEO Feross Aboukhadijeh joins Risky Business Weekly to unpack recent npm phishing attacks, their limited impact, and the risks if attackers get smarter.
Product
Socket’s new Tier 1 Reachability filters out up to 80% of irrelevant CVEs, so security teams can focus on the vulnerabilities that matter.