https://github.com/gtr/trie
quick and effective trie library implementation for go
https://github.com/gtr/trie
Last synced: 5 months ago
JSON representation
quick and effective trie library implementation for go
- Host: GitHub
- URL: https://github.com/gtr/trie
- Owner: gtr
- Created: 2020-05-14T17:10:54.000Z (about 6 years ago)
- Default Branch: master
- Last Pushed: 2020-05-14T17:11:41.000Z (about 6 years ago)
- Last Synced: 2024-06-19T15:08:33.130Z (almost 2 years ago)
- Language: Go
- Homepage:
- Size: 1.95 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
a quick and effective trie library implementation for go
## install
use the `go get` command:
```bash
go get github.com/gtr/trie
```
## usage
import the library into your go main file:
```go
import "github.com/gtr/trie"
```
example usage:
```go
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
```
## public api
### trie
```go
type Trie struct {
Root *Node
}
```
Trie represents a trie object.
```go
func NewTrie() *Trie
```
NewTrie returns a pointer to an empty trie.
```go
func (t *Trie) InsertWord(word string)
```
InsertWord inserts a new word into the trie.
```go
func (t *Trie) InsertWords(words []string)
```
InsertWords inserts multiple words into the trie.
```go
func (t *Trie) FindWord(word string) bool
```
FindWord returns a bool if a word exists in the trie.
```go
func (t *Trie) GetAllWords() []string
```
GetAllWords returns a slice of strings containing all the words in the entire trie.
```go
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.
### node
```go
type Node struct {
IsWord bool
Children map[rune]*Node
}
```
Node represents a Node in the trie.
```go
func NewNode() *Node
```
NewNode returns a pointer to an empty node.
```go
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.