treex

package
v1.0.0-beta.225 Latest Latest
Warning

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

Go to latest
Published: Nov 10, 2025 License: Apache-2.0 Imports: 3 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrRootNodeIsNil             = errors.New("root node cannot be nil")
	ErrGraphHasCycle             = errors.New("graph cannot contain cycles")
	ErrNodeHasNoParentButNotRoot = errors.New("node has no parent but is not the root")

	// catch-all error for invalid nodes or graph structure
	ErrNodeGraphInvalid = errors.New("invalid graph structure")
)

Functions

This section is empty.

Types

type Node

type Node[T any] struct {
	// contains filtered or unexported fields
}

func NewNode

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

func (*Node[T]) AddChild

func (n *Node[T]) AddChild(child *Node[T])

func (*Node[T]) Children

func (n *Node[T]) Children() []*Node[T]

func (*Node[T]) DeepClone

func (n *Node[T]) DeepClone() *Node[T]

DeepClone creates a deep copy of the node and all its descendants. The returned clone is fully detached (parent pointers set appropriately within the cloned subtree, with the top-level node having nil parent).

func (*Node[T]) IsLeaf

func (n *Node[T]) IsLeaf() bool

func (*Node[T]) IsRoot

func (n *Node[T]) IsRoot() bool

func (*Node[T]) Parent

func (n *Node[T]) Parent() *Node[T]

func (*Node[T]) RemoveChild

func (n *Node[T]) RemoveChild(child *Node[T]) error

func (*Node[T]) SetValue

func (n *Node[T]) SetValue(value T)

func (*Node[T]) ShallowClone

func (n *Node[T]) ShallowClone() *Node[T]

ShallowClone creates a new node with the same value and parent and a copied first-level children slice. It reattaches the existing first-level children to the cloned node (i.e., updates child.parent to point to the clone) but does not traverse deeper. This is useful for immutable-style updates when replacing a node while keeping its immediate subtree.

func (*Node[T]) SwapChild

func (n *Node[T]) SwapChild(old *Node[T], new *Node[T]) error

func (*Node[T]) Value

func (n *Node[T]) Value() T

type Tree

type Tree[T any] struct {
	// contains filtered or unexported fields
}

func NewTree

func NewTree[T any](root *Node[T]) (*Tree[T], error)

NewTree attempts to create a new Tree from a root node. It traverses the children graph to ensure it is acyclic.

func (*Tree[T]) DFS

func (t *Tree[T]) DFS(cb func(n *Node[T]) (stop bool, err error)) error

func (*Tree[T]) Leafs

func (t *Tree[T]) Leafs() []*Node[T]

Leafs returns all leaf nodes in the tree

func (*Tree[T]) Root

func (t *Tree[T]) Root() *Node[T]

func (*Tree[T]) SwapNode

func (t *Tree[T]) SwapNode(old *Node[T], new *Node[T]) error

SwapNode can swap any node in the tree including the root. If the old node wasn't found an error is returned. The new node's parent will be set to the old node's parent.

CAUTION: if you swap a node while walking the tree, you should start backtracking after the swap otherwise you'll keep iterating the detached subtree

Jump to

Keyboard shortcuts

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