trie

package module
v0.0.0-...-53014ac Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 6, 2022 License: MIT Imports: 1 Imported by: 0

README

trie

Build codecov Go Report Card GoDoc

A missing trie implementation for Go.

Installation

go get github.com/krasun/trie

Usage

Known limitations:

  1. Empty string keys are not supported. And functions won't return an error.
  2. Keys are not ordered, so do not expect any ordering.

Without any stress:

t := trie.New() // or trie.NewRuneTrie()  

t.Insert("apple")
t.Insert("alphabet")
t.Insert("banana")

t.Contains("apple") // true
t.Contains("app") // false
t.Contains("banana") // true
t.Contains("ban") // false

t.SearchByPrefix("a") // []string{"apple", "alphabet"}

t.StartsWith("a") // true
A goroutine-safe (thread-safe) trie

By default, rune-wise trie are not safe to use concurrently, but it is easy to make them safe. You can wrap any trie into the safe version by:

safeTrie := trie.Safe(trie.NewTrie())

// the same interface as for a regular trie

Tests

To run tests, just execute:

$ go test . -race

Benchmarking

Zero heap allocations for Contains function:

$ go test -bench=. -benchmem -benchtime=1000x
goos: darwin
goarch: amd64
op	       0 allocs/op
BenchmarkRuneTrieInsert-8          	    1000	      7196 ns/op	    6984 B/op	     129 allocs/op
BenchmarkRuneTrieContains-8        	    1000	       517 ns/op	       0 B/op	       0 allocs/op
BenchmarkWordHashSetInsert-8       	    1000	      1406 ns/op	    1100 B/op	       4 allocs/op
BenchmarkWordHashSetContains-8     	    1000	       178 ns/op	       0 B/op	       0 allocs/op
PASS

But the hash set (map[string]{}struct) is much much more efficient than the trie. Carefully weigh when to use the trie.

Applications

Use the trie in cases when it only outperforms hash tables or hash sets (map[string]{}struct):

  1. Search key by the prefix.

License

trie is released under the MIT license.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Trie

type Trie interface {
	// Insert inserts a word into the trie and returns true
	// if the word already exists.
	Insert(word string) bool
	// Contains returns true if an exact match of the word is found, otherwise false.
	Contains(word string) bool
	// SearchByPrefix finds and returns words by prefix.
	SearchByPrefix(prefix string) []string
	// StartsWith returns true if there is any word in the trie that starts
	// with the given prefix.
	StartsWith(prefix string) bool
	// Size returns the number of keys in the tree.
	Size() int
}

Trie represents a Trie data structure.

https://en.wikipedia.org/wiki/Trie

func New

func New() Trie

New creates a new trie, by default rune-wise.

func NewRuneTrie

func NewRuneTrie() Trie

NewRuneTrie creates a rune-wise new trie.

func Safe

func Safe(trie Trie) Trie

Safe returns goroutine-safe (thread-safe) version of a given trie.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL