ergotree

package module
v0.0.4 Latest Latest
Warning

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

Go to latest
Published: Dec 1, 2024 License: MIT Imports: 2 Imported by: 0

README

ergotree

ergotree is a Go package that implements a tree data-structure using a recursively defined map as it's backing store.

Getting Started

go get github.com/sean9999/go-ergonomic-tree

Example


package ergotree_test

import (
	"slices"
	"testing"

	tree "github.com/sean9999/go-ergonomic-tree"
)

func TestNew(t *testing.T) {

	//	life is the root node of the tree of life
	life := tree.New[string](nil)

	//	Great Apes descend from Apes, which descend from Primates
	greatApes := life.Spawn("Primates").Spawn("Apes").Spawn("Great Apes")

	//	Eastern and Western Gorilla are each distinct species which descend from Great Apes
	greatApes.Spawn("Eastern Gorilla")
	greatApes.Spawn("Western Gorilla")

	//	walk the tree of life (depth first)
	got := life.Walk()

	want := [][]string{
		{"Primates", "Apes", "Great Apes", "Eastern Gorilla"},
		{"Primates", "Apes", "Great Apes", "Western Gorilla"},
	}

	//	we cannot be guratantted in which order we get trees of the branch
	//	since our backing data structure is a map
	if slices.Compare(want[0], got[0]) != 0 && slices.Compare(want[1], got[0]) != 0 {
		t.Errorf("got %v but wanted %v", got, want)
	}
	if slices.Compare(want[0], got[1]) != 0 && slices.Compare(want[1], got[1]) != 0 {
		t.Errorf("got %v but wanted %v", got, want)
	}

}


Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Node

type Node[K comparable] map[K]*Node[K]

func New

func New[K comparable](parent *Node[K]) *Node[K]

New is a constructor

func (*Node[K]) Ancestry

func (t *Node[K]) Ancestry() []K

returns a slice of []K, indicating the path to the Node

func (*Node[K]) Children

func (t *Node[K]) Children() map[K]*Node[K]

Children returns the map, omiting the special "parent" entry

func (*Node[K]) Data

func (t *Node[K]) Data() K

get key of self by querying parent zero value means root (no key)

func (*Node[K]) Equals added in v0.0.4

func (t *Node[K]) Equals(t2 *Node[K]) bool

basic equality check

func (*Node[K]) Get

func (t *Node[K]) Get(key K) (*Node[K], bool)

Get gets the value from the map

func (*Node[K]) IsTerminal

func (t *Node[K]) IsTerminal() bool

IsTerminal returns true if the Node contains no child Nodes

func (*Node[K]) Parent

func (t *Node[K]) Parent() *Node[K]

Parent returns a Node's parent

func (*Node[K]) RemoveChild

func (t *Node[K]) RemoveChild(key K)

RemoveChild removes a child Node

func (*Node[K]) Set

func (t *Node[K]) Set(key K)

Set simply calls Spawn(), but discards return value

func (*Node[K]) SetParent

func (t *Node[K]) SetParent(p *Node[K])

func (*Node[K]) Spawn

func (t *Node[K]) Spawn(key K) *Node[K]

Spawn creates and returns a child Node

func (*Node[K]) String added in v0.0.4

func (t *Node[K]) String() string

func (*Node[K]) Walk

func (t *Node[K]) Walk() [][]K

Walk returns all leaf nodes as ancestries

Jump to

Keyboard shortcuts

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