tree

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Dec 26, 2022 License: MIT Imports: 1 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type MapNode

type MapNode[T any] struct {
	Parent   *MapNode[T]
	Children map[string]*MapNode[T]
	Value    T
}

MapNode is a node in the tree data structure

func NewNode

func NewNode[T any](value T) *MapNode[T]

NewNode creates a new tree node

type MapTree

type MapTree[T any] struct {
	Root  *MapNode[T]
	Nodes map[string]*MapNode[T]
	KeyFn func(T) string
}

MapTree is a tree data structure

func NewTree

func NewTree[T any](value T, fn func(T) string) *MapTree[T]

NewTree creates a new tree node

func (*MapTree[T]) Add

func (t *MapTree[T]) Add(parent *MapNode[T], child T) (*MapNode[T], error)

AddTree adds a sub-tree without children

func (*MapTree[T]) AddNode

func (t *MapTree[T]) AddNode(parent *MapNode[T], child *MapNode[T]) error

AddNode adds a sub-tree

func (*MapTree[T]) Ancestors

func (t *MapTree[T]) Ancestors(name string) ([]*MapNode[T], bool)

Ancestors returns a slice of all ancestors (i.e. recursive parents), and an ok bool whether the requested node was found.

The first ancestor is the direct parent, while the last ancestor is the root node.

func (*MapTree[T]) Descendants

func (t *MapTree[T]) Descendants(name string) ([]*MapNode[T], bool)

Descendants returns a slice of all descendants (i.e. recursive children), and an ok bool whether the requested node was found.

Descendants in the returned slice have undefined order.

Jump to

Keyboard shortcuts

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