/trie

A trie for holding strings and values. Compact storage for keys with common prefixes.

Primary LanguageGoMIT LicenseMIT

trie

PkgGoDev Go Report Card CircleCI

A trie written in Go.

A trie is a tree structure for storing strings with common prefixes.

This implementation will store strings with values, the trie allow nil values to be stored but not nil.

API

Create new trie

Creates a new empty trie.

import trie
t := trie.NewTrie()

Put key value

Add a key and value in the trie.

import trie
t := trie.NewTrie()
t.put("www.test.com", 1)
//Null value
var empty int
t.put("www.example.com", empty)

Get value

Get a value from the trie for a given key.

import trie
t := trie.NewTrie()
t.put("www.test.com", 1)
t.get("www.test.com")

Delete key

Delete the key and value from the trie.

import trie
t := trie.NewTrie()
t.Put("www.test.com", 1)
t.Delete("www.test.com")

Contains key

Check if the key exists in the trie.

import trie
t := trie.NewTrie()
t.Put("www.test.com", 1)
t.Contains("www.test.com")

Number of keys

Get the number of keys in the trie.

import trie
t := trie.NewTrie()
t.Put("www.test.com", 1)
t.Put("www.example.com", 1)
t.Size()

No keys

Find out if the trie has no keys.

import trie
t := trie.NewTrie()
t.Put("www.test.com", 1)
t.IsEmpty()

Find longest prefix

Search the trie for the longest prefix of the given trie

import trie
t := trie.NewTrie()
t.Put("www.test.example.com", 1)
t.Put("www.dev.example.com", 1)
t.LongestPrefixOf("www.test.example.com/home.html")

Find keys with prefix

Search the trie for keys with the given prefix.

import trie
t := trie.NewTrie()
t.Put("www.test.example.com", 1)
t.Put("www.dev.example.com", 1)
t.KeysWithPrefix("www")

Find all keys in the trie.

Returns a channel with that return all the keys in the trie.

import trie
t := trie.NewTrie()
t.Put("www.test.example.com", 1)
t.Put("www.dev.example.com", 1)
t.KeysWithPrefix("www")

All key values in the trie

Find all keys in the trie.

Returns a channel with that returns all the key values in the trie, as NodeKeyValue.

import trie
t := trie.NewTrie()
t.Put("www.test.example.com", 1)
t.Put("www.dev.example.com", 1)
t.Items()

Tests

The tests can be invoked with go test

License

MIT © Oliver Daff