ergotree

package module
v0.0.3 Latest Latest
Warning

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

Go to latest
Published: Feb 25, 2024 License: MIT Imports: 3 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 main

import (
	"reflect"
	"testing"

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

func TestErgonomicTree(t *testing.T) {

    //  create a tree
	life := ergotree.New[string](nil)
	
    //  create some children
    greatApes := life.Spawn("Primates").Spawn("Apes").Spawn("Great Apes")
	
    //  create some leaf nodes
    greatApes.Set("Western Gorilla")
    greatApes.Set("Eastern Gorilla")

    //  walk the whole tree, returning full paths to all leaf nodes
	got := life.Walk()

    //  check the data
	want := [][]string{
		{"Primates", "Apes", "Great Apes", "Eastern Gorilla"},
		{"Primates", "Apes", "Great Apes", "Western Gorilla"},
	}
	if !(reflect.DeepEqual(got[0], want[0]) || reflect.DeepEqual(got[0], want[1])) {
		t.Errorf("got %v but wanted %v", got, want)
	}
	if !(reflect.DeepEqual(got[1], want[0]) || reflect.DeepEqual(got[1], want[1])) {
		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] interface {
	Data() K
	Children() map[K]Node[K]
	Spawn(K) Node[K]
	RemoveChild(K)
	Parent() Node[K]
	Walk() [][]K
	Ancestry() []K
	IsTerminal() bool
	Set(K)
	Get(K) (Node[K], bool)
	SetParent(Node[K])
	fmt.Stringer
}

func New

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

New is a constructor

Jump to

Keyboard shortcuts

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