Documentation
¶
Index ¶
- Variables
- type Node
- func (n *Node[T]) AddChild(child *Node[T])
- func (n *Node[T]) Children() []*Node[T]
- func (n *Node[T]) DeepClone() *Node[T]
- func (n *Node[T]) IsLeaf() bool
- func (n *Node[T]) IsRoot() bool
- func (n *Node[T]) Parent() *Node[T]
- func (n *Node[T]) RemoveChild(child *Node[T]) error
- func (n *Node[T]) SetValue(value T)
- func (n *Node[T]) ShallowClone() *Node[T]
- func (n *Node[T]) SwapChild(old *Node[T], new *Node[T]) error
- func (n *Node[T]) Value() T
- type Tree
Constants ¶
This section is empty.
Variables ¶
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 (*Node[T]) DeepClone ¶
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]) RemoveChild ¶
func (*Node[T]) ShallowClone ¶
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.
type Tree ¶
type Tree[T any] struct { // contains filtered or unexported fields }
func NewTree ¶
NewTree attempts to create a new Tree from a root node. It traverses the children graph to ensure it is acyclic.
func (*Tree[T]) SwapNode ¶
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