trie

package module
v0.0.0-...-c028259 Latest Latest
Warning

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

Go to latest
Published: Jun 18, 2013 License: BSD-3-Clause Imports: 0 Imported by: 0

Documentation

Overview

Ternary Search Tree

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Iterator

type Iterator func(key string, value interface{}) (cont bool)

type Trie

type Trie struct {
	// contains filtered or unexported fields
}

An empty value of this type can be used to Put values.

func (*Trie) Each

func (t *Trie) Each(f Iterator)

func (*Trie) Get

func (t *Trie) Get(key string) interface{}

Get returns the value of the desired key, or nil if the key wasn't found.

Complexity: O(m) worst case

Example
t := &Trie{}
t.Put("hello", "world")
fmt.Println(t.Get("hello").(string))
fmt.Println(t.Get("non-existant"))
Output:

world
<nil>

func (*Trie) LongestPrefix

func (t *Trie) LongestPrefix(key string) string

LongestPrefix returns the longest key that has a prefix in common with the key. If no match is found, "" is returned.

Complexity: O(m) worst case

Example
t := &Trie{}
t.Put("Hello", "World")
t.Put("Hello, brother", "World")
t.Put("Hello, bob", "World")
t.LongestPrefix("Hello, brandon") // "Hello"
t.LongestPrefix("Hel")            // ""
t.LongestPrefix("Hello")          // "Hello"
Output:

func (*Trie) Put

func (t *Trie) Put(key string, value interface{})

Put adds a key/value pair to the Trie.

Complexity: O(m)

Example
t := &Trie{}
t.Put("hello", "world")
t.Put("hello", "world") // does the same thing
fmt.Println(t.Get("hello"))
t.Put("1", 1)
fmt.Println(t.Get("1").(int))
Output:

world
1

func (*Trie) Wildcard

func (t *Trie) Wildcard(key string) []string

Wildcard Returns a sorted slice with matches for the key. The wildcard characters that match any character are '*' and '.'. If no match is found, an empty slice is returned.

Complexity: O(n) worst case

Example
t := &Trie{}
t.Put("Hello", "World")
t.Put("Hilly", "World")
t.Put("Hello, bob", "World")
t.Wildcard("H*ll.") // []string{"Hello", "Hilly"}
t.Wildcard("Hel")   // []string(nil)
Output:

Jump to

Keyboard shortcuts

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