Skip to content

markelog/trie

Repository files navigation

Trie Build Status GoDoc Go Report Card Coverage

Implementation of "Trie" data structure

See https://en.wikipedia.org/wiki/Trie. Why yet another "trie" implementation?

Some of them do not expose needed methods like traversing, some of them do not expose parents or children. And some of them just plain peculiar. At least I think so :)

Install

go get github.com/markelog/trie

Usage

Simple example here, see docs for more stuff and explanations

package main

import (
	"github.com/markelog/trie"
	"github.com/markelog/trie/node"
)

func main() {
	tree := trie.New()

	tree.Add("cool", "So cool")
	tree.Add("coolio", 54) // Age of his at the moment of writing

	println(tree.Size) // 2

	println(tree.Contains("cool")) // true

	println(len(tree.Root.Children))   // 1
	println(tree.Root.Children[0].Key) // "cool"

	results := tree.Search("cool") // []string{"cool", "coolio"}
	cool := results[0]

	println(cool.Value.(string))  // "So cool"
	println(cool.Children[0].Key) // "coolio"

	coolio := tree.Find("coolio")

	println(coolio.Value.(int)) // 54

	println(coolio.Key)        // "coolio"
	println(coolio.Parent.Key) // "cool"

	i := 0
	tree.Traverse(func(item *node.Node) bool {
		i++
		return true
	})

	println(i) // 2

	i = 0
	tree.Visit(cool, func(item *node.Node) bool {
		i++
		return true
	})

	println(i) // 1

	// Including branches
	i = 0
	tree.VisitAll(cool, func(item *node.Node) bool {
		i++
		return true
	})

	println(i) // 2
}

About

Implementation of "Trie" data structure

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages