Documentation
¶
Index ¶
- type Ternary
- func (t *Ternary[T]) Compress()
- func (t *Ternary[T]) Delete(key string)
- func (t *Ternary[T]) Find(key string) *T
- func (t *Ternary[T]) KeysMatch(p string) []string
- func (t *Ternary[T]) KeysWithPrefix(prefix string) []string
- func (t *Ternary[T]) LongestPrefixOf(s string) string
- func (t *Ternary[T]) Upsert(key string, val T) bool
- type Trie
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Ternary ¶
type Ternary[T any] struct { // contains filtered or unexported fields }
Example ¶
trie := NewTernary[string]()
for k, v := range dict {
trie.Upsert(k, v)
}
result := trie.KeysWithPrefix("")
sort.Strings(result)
fmt.Println("all keys:", result)
pattern := "b.te"
result = trie.KeysMatch(pattern)
sort.Strings(result)
fmt.Printf("keys match '%s': %v\n", pattern, result)
prefix := "bi"
result = trie.KeysWithPrefix(prefix)
sort.Strings(result)
fmt.Printf("keys with prefix '%s': %v\n", prefix, result)
str := "bitcoins"
fmt.Printf("longest key with prefix '%s': %s\n", str, trie.LongestPrefixOf(str))
Output: all keys: [a abandon abnormal am an apollo archive are automatic best bit bitcoin bite byte] keys match 'b.te': [bite byte] keys with prefix 'bi': [bit bitcoin bite] longest key with prefix 'bitcoins': bitcoin
func NewTernary ¶
func (*Ternary[T]) KeysWithPrefix ¶
func (*Ternary[T]) LongestPrefixOf ¶
type Trie ¶
type Trie[T any] struct { // contains filtered or unexported fields }
Example ¶
trie := NewTrie[string](alphabet.NewAlphabet(alphabet.ASCII))
for k, v := range dict {
trie.Upsert(k, v)
}
result := trie.KeysWithPrefix("")
sort.Strings(result)
fmt.Println("all keys:", result)
pattern := "b.te"
result = trie.KeysMatch(pattern)
sort.Strings(result)
fmt.Printf("keys match '%s': %v\n", pattern, result)
prefix := "bi"
result = trie.KeysWithPrefix(prefix)
sort.Strings(result)
fmt.Printf("keys with prefix '%s': %v\n", prefix, result)
str := "bitcoins"
fmt.Printf("longest key with prefix '%s': %s\n", str, trie.LongestPrefixOf(str))
Output: all keys: [a abandon abnormal am an apollo archive are automatic best bit bitcoin bite byte] keys match 'b.te': [bite byte] keys with prefix 'bi': [bit bitcoin bite] longest key with prefix 'bitcoins': bitcoin
func (*Trie[T]) KeysWithPrefix ¶
func (*Trie[T]) LongestPrefixOf ¶
Click to show internal directories.
Click to hide internal directories.