trie

package
v0.0.0-...-8d83361 Latest Latest
Warning

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

Go to latest
Published: Feb 21, 2026 License: GPL-3.0 Imports: 2 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Node

type Node[V any] struct {
	Value *V
	Right *Node[V]
	Left  *Node[V]
	// contains filtered or unexported fields
}

Node represents trie node which may contains a prefix if node has prefix it will have non-nil prefix value and otherwise it will have nil prefix value.

type Trie

type Trie[V any] struct {
	Root   *Node[V]
	Height uint
	Prefix string // when trie is subtrie this field represents old prefix of root
}

Trie represents binary trie for prefix lookup.

func New

func New[V any]() *Trie[V]

New creates new trie.

func (*Trie[V]) Add

func (t *Trie[V]) Add(route string, value V)

Add adds new route into trie. Given route must be in binary regex format e.g. *, 11*.

func (*Trie[V]) All

func (t *Trie[V]) All() iter.Seq2[string, V]

All returns an iterator over all (prefix, value) pairs in the trie.

func (*Trie[V]) Divide

func (t *Trie[V]) Divide(stride uint) [][]*Trie[V]

Divide divides the binary trie into different levels based on these k-bit subtries. If k = 4 thus, level 0 contains from prefix length 0 to prefix length 7, and so on. Each level contains one or more subtries. nolint: funlen, gocognit, cyclop

func (*Trie[V]) Lookup

func (t *Trie[V]) Lookup(route string) (V, bool)

Lookup looks up given route in trie and returns found value. Given route must be in binary representation e.g. 111111..

func (*Trie[V]) Matches

func (t *Trie[V]) Matches(route string) iter.Seq2[string, V]

Matches returns an iterator over all (prefix, value) pairs that match the given binary route, yielding from shortest to longest prefix.

func (*Trie[V]) ToArray

func (t *Trie[V]) ToArray() []Node[V]

ToArray returns node array of trie with following structure:

   i
  / \
2i  2i+1

e.g.

      1
    /  \
   2    3
 /  \  / \
4   5 6   7

Jump to

Keyboard shortcuts

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