tree

package
v0.0.0-...-950aede Latest Latest
Warning

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

Go to latest
Published: May 20, 2024 License: MIT Imports: 6 Imported by: 0

Documentation

Overview

Package tree provides tree structure related types and utilities.

Index

Constants

This section is empty.

Variables

View Source
var ErrSkipSubtree = errors.New("skip subtree")

ErrSkipSubtree is a sentinel value that can be returned by a NodeWalker to skip the remainder of a subtree. This error halts iteration of that subtree at the first handling parent.

Functions

This section is empty.

Types

type BasicNode

type BasicNode[K constraints.Ordered, V comparable] struct {
	// contains filtered or unexported fields
}

BasicNode is a basic, arbitrarily-ordered, non-balancing, key/value tree.

func NewBasicNode

func NewBasicNode[K constraints.Ordered, V comparable](
	key K,
	value V,
) *BasicNode[K, V]

NewBasicNode creates a new BasicNode.

func (*BasicNode[K, V]) Add

func (n *BasicNode[K, V]) Add(key K, value V) *BasicNode[K, V]

Add adds a new child with the given key and value to this node, returning the new node.

func (*BasicNode[K, V]) Child

func (n *BasicNode[K, V]) Child(key K) *BasicNode[K, V]

Child returns the child of with the given key, if one exists. If no child with the given key is found, nil is returned.

func (*BasicNode[K, V]) Children

func (n *BasicNode[K, V]) Children() map[K]*BasicNode[K, V]

Children returns the node's children.

func (*BasicNode[K, V]) Key

func (n *BasicNode[K, V]) Key() (key K)

Key returns the node's key.

func (*BasicNode[K, V]) Len

func (n *BasicNode[K, V]) Len() (total int)

Len returns the recursive length of the tree relative to the node.

func (*BasicNode[K, V]) Parent

func (n *BasicNode[K, V]) Parent() *BasicNode[K, V]

Parent returns the node's parent node.

func (*BasicNode[K, V]) Path

func (n *BasicNode[K, V]) Path() []K

Path returns all keys in the path from the root node to this node.

func (*BasicNode[K, V]) PathRev

func (n *BasicNode[K, V]) PathRev() []K

PathRev returns all keys in the path from this node to the root.

func (*BasicNode[K, V]) Remove

func (n *BasicNode[K, V]) Remove(
	key K,
) (child *BasicNode[K, V], removed bool)

Remove removes the child with the given key, if one exists.

func (*BasicNode[K, V]) SetParent

func (n *BasicNode[K, V]) SetParent(parent *BasicNode[K, V])

SetParent sets the node's parent to the given parent node.

func (*BasicNode[K, V]) SetValue

func (n *BasicNode[K, V]) SetValue(value V)

SetValue sets the node's value.

func (*BasicNode[K, V]) Value

func (n *BasicNode[K, V]) Value() (value V)

Value returns the node's value.

func (*BasicNode[K, V]) Walk

func (n *BasicNode[K, V]) Walk(fn NodeWalker[K, V]) error

Walk walks through the tree depth-first.

func (*BasicNode[K, V]) WalkRev

func (n *BasicNode[K, V]) WalkRev(fn NodeWalker[K, V]) error

WalkRev walks through the tree depth-first in reverse.

type NodeWalker

type NodeWalker[K constraints.Ordered, V comparable] func(node *BasicNode[K, V]) error

A NodeWalker is a function that controls how nodes are walked.

Jump to

Keyboard shortcuts

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