trie

package
v1.1.2 Latest Latest
Warning

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

Go to latest
Published: Feb 18, 2026 License: MIT Imports: 5 Imported by: 0

Documentation

Overview

Package trie implements a simple prefix tree. This is designed to be used for text command completion and is reasonably efficient in that application.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Node

type Node struct {

	// Val is the value of the node if it's terminal, otherwise it will be
	// nil.
	Val any
	// contains filtered or unexported fields
}

Node represents an entire prefix tree or a node within it.

func (*Node) Add

func (n *Node) Add(key string, val any)

Add adds a key and value to the tree. Terminal values must be non-nil.

func (*Node) Children

func (n *Node) Children(sorted bool) []*Node

Children returns the immediate child nodes optionally sorted in alphabetical order.

func (*Node) Find

func (n *Node) Find(key string, sep rune) (match string, cur *Node)

Find returns the node that completes the key as much as possible while remaining unique. The key is split by sep and each part is completed individually.

func (*Node) Get

func (n *Node) Get(key string) *Node

Get returns the node of the given key or nil if it's not found.

func (Node) Match

func (n Node) Match(key string) iter.Seq[string]

Match returns all possible completions for the given key.

func (Node) String

func (n Node) String() string

String returns a pretty-printed string of the node and all its children.

Example
n := testTree()
fmt.Println(n.String())
Output:

`--(*)
     `--(?) => "?": <value for "?">
     `--(a)
     |   `--(r)
     |       `--(c)
     |           `--(h)
     |               `--(i)
     |                   `--(v)
     |                       `--(e) => "archive": <value for "archive">
     `--(c)
     |   `--(o)
     |       `--(n)
     |           `--(d)
     |               `--(i)
     |                   `--(t)
     |                       `--(i)
     |                           `--(o)
     |                               `--(n)
     |                                   `--(s) => "conditions": <value for "conditions">
     `--(d)
     |   `--(a)
     |       `--(t)
     |           `--(e) => "date": <value for "date">
     `--(e)
     |   `--(x)
     |       `--(i)
     |           `--(t) => "exit": <value for "exit">
     `--(h)
     |   `--(e)
     |       `--(a)
     |       |   `--(l)
     |       |       `--(t)
     |       |           `--(h) => "health": <value for "health">
     |       `--(l)
     |           `--(p) => "help": <value for "help">
     `--(l)
     |   `--(a)
     |   |   `--(m)
     |   |       `--(p)
     |   |           `--(s)
     |   |               `--( )
     |   |                   `--(o)
     |   |                       `--(f)
     |   |                       |   `--(f) => "lamps off": <value for "lamps off">
     |   |                       `--(n) => "lamps on": <value for "lamps on">
     |   `--(o)
     |       `--(g)
     |       |   `--(o)
     |       |       `--(u)
     |       |           `--(t) => "logout": <value for "logout">
     |       `--(o)
     |           `--(p) => "loop": <value for "loop">
     `--(q)
     |   `--(u)
     |       `--(i)
     |           `--(t) => "quit": <value for "quit">
     `--(t)
     |   `--(i)
     |   |   `--(m)
     |   |       `--(e) => "time": <value for "time">
     |   `--(r)
     |       `--(e)
     |           `--(n)
     |               `--(d) => "trend": <value for "trend">
     `--(u)
     |   `--(n)
     |   |   `--(a)
     |   |       `--(m)
     |   |           `--(e) => "uname": <value for "uname">
     |   `--(p)
     |       `--(t)
     |           `--(i)
     |               `--(m)
     |                   `--(e) => "uptime": <value for "uptime">
     `--(v)
     |   `--(e)
     |       `--(r)
     |           `--(s)
     |               `--(i)
     |                   `--(o)
     |                       `--(n) => "version": <value for "version">
     `--(w)
         `--(a)
         |   `--(t)
         |       `--(c)
         |           `--(h)
         |               `--( )
         |                   `--(c)
         |                   |   `--(o)
         |                   |       `--(n)
         |                   |           `--(d)
         |                   |               `--(i)
         |                   |                   `--(t)
         |                   |                       `--(i)
         |                   |                           `--(o)
         |                   |                               `--(n)
         |                   |                                   `--(s) => "watch conditions": <value for "watch conditions">
         |                   `--(l)
         |                       `--(o)
         |                           `--(g)
         |                           |   `--( )
         |                           |       `--(d)
         |                           |       |   `--(e)
         |                           |       |       `--(b)
         |                           |       |           `--(u)
         |                           |       |               `--(g) => "watch log debug": <value for "watch log debug">
         |                           |       `--(t)
         |                           |           `--(r)
         |                           |               `--(a)
         |                           |                   `--(c)
         |                           |                       `--(e) => "watch log trace": <value for "watch log trace">
         |                           `--(o)
         |                               `--(p)
         |                                   `--(s) => "watch loops": <value for "watch loops">
         `--(h)
             `--(o)
                 `--(a)
                     `--(m)
                         `--(i) => "whoami": <value for "whoami">

Jump to

Keyboard shortcuts

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