New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

github.com/gtr/trie

Package Overview
Dependencies
Alerts
File Explorer
Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

github.com/gtr/trie

  • v0.0.0-20200514171329-3b27e039de96
  • Source
  • Go
  • Socket score

Version published
Created
Source

a quick and effective trie library implementation for go

install

use the go get command:

go get github.com/gtr/trie

usage

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

public api

trie

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.

node

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

Package last updated on 14 May 2020

Did you know?

Socket

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.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc