Documentation ¶
Index ¶
- type NextsFunc
- type Node
- func (n *Node[T]) AddChild(data T)
- func (n *Node[T]) AddChildren(children ...T)
- func (n *Node[T]) BFSTraversal(observer itff.ObserverFunc[T]) error
- func (n *Node[T]) Cleanup()
- func (n *Node[T]) DFSTraversal(observer itff.ObserverFunc[T]) error
- func (n *Node[T]) DeleteChild(child *Node[T])
- func (n *Node[T]) FindBranchingPoint() *Node[T]
- func (n *Node[T]) GetChildren() []*Node[T]
- func (n *Node[T]) GetLeaves() []*Node[T]
- func (n *Node[T]) HasChild(child *Node[T]) bool
- func (n *Node[T]) Parent() *Node[T]
- func (n *Node[T]) PruneFunc(filter slext.PredicateFilter[T]) bool
- func (n *Node[T]) SnakeTraversal() [][]T
- type ObserverFunc
- type Traverser
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type NextsFunc ¶ added in v0.2.30
NextsFunc is a function that returns the children of a node.
Parameters:
- node: The node to compute the children of.
Returns:
- []T: The children of the node.
- error: An error if children cannot be computed.
type Node ¶
type Node[T any] struct { // Data is the value of the node. Data T // contains filtered or unexported fields }
Node is a generic data structure that represents a node in a tree.
func NewNode ¶
NewNode creates a new node with the given data.
Parameters:
- data: The value of the node.
Returns:
- *Node[T]: A pointer to the newly created node.
func (*Node[T]) AddChild ¶
func (n *Node[T]) AddChild(data T)
AddChild adds a new child to the node with the given data.
Parameters:
- data: The value of the new child.
func (*Node[T]) AddChildren ¶
func (n *Node[T]) AddChildren(children ...T)
AddChildren adds zero or more children to the node with the given data.
Parameters:
- children: The values of the new children.
func (*Node[T]) BFSTraversal ¶
func (n *Node[T]) BFSTraversal(observer itff.ObserverFunc[T]) error
BFSTraversal traverses the tree rooted at n in a breadth-first manner. The traversal stops when the observer returns an error.
Parameters:
- observer: A function that takes the value of a node and returns an error.
Returns:
- error: An error returned by the observer.
func (*Node[T]) Cleanup ¶
func (n *Node[T]) Cleanup()
Cleanup removes the node from the tree; including all its children.
func (*Node[T]) DFSTraversal ¶
func (n *Node[T]) DFSTraversal(observer itff.ObserverFunc[T]) error
DFSTraversal traverses the tree rooted at n in a depth-first manner. The traversal stops when the observer returns an error.
Parameters:
- observer: A function that takes the value of a node and returns an error.
Returns:
- error: An error returned by the observer.
func (*Node[T]) DeleteChild ¶
DeleteChild removes the given child from the children of the node. No op if the child is nil or not a child of the node.
Parameters:
- child: The child to remove.
Returns:
- []*Node[T]: A slice of pointers to the children of the node.
func (*Node[T]) FindBranchingPoint ¶
FindBranchingPoint returns the first node in the path from n to the root such that has more than one sibling. If no such node is found, it returns nil.
Returns:
- *Node[T]: A pointer to the branching point.
func (*Node[T]) GetChildren ¶
GetChildren returns all the children of the node. If the node has no children, it returns nil.
Returns:
- []*Node[T]: A slice of pointers to the children of the node.
func (*Node[T]) GetLeaves ¶
GetLeaves returns all the leaves of the tree rooted at n. The leaves are returned in the order of a breadth-first traversal.
Returns:
- []*Node[T]: A slice of pointers to the leaves of the tree.
func (*Node[T]) HasChild ¶
HasChild returns true if the node has the given child.
Parameters:
- child: The child to check for.
Returns:
- bool: True if the node has the child, false otherwise.
func (*Node[T]) Parent ¶
Parent is a getter for the parent of the node. If the node has no parent, it returns nil.
Returns:
- *Node[T]: A pointer to the parent of the node.
func (*Node[T]) PruneFunc ¶
func (n *Node[T]) PruneFunc(filter slext.PredicateFilter[T]) bool
PruneFunc removes all the children of the node that satisfy the given filter. The filter is a function that takes the value of a node and returns a boolean. If the filter returns true for a child, the child is removed.
Parameters:
- filter: The filter to apply.
Returns:
- bool: True if the node satisfies the filter, false otherwise.
func (*Node[T]) SnakeTraversal ¶
func (n *Node[T]) SnakeTraversal() [][]T
SnakeTraversal returns all the paths from n to the leaves of the tree rooted at n. The paths are returned in the order of a breadth-first traversal.
Returns:
- [][]T: A slice of slices of the values of the nodes in the paths.
type ObserverFunc ¶ added in v0.2.30
ObserverFunc is a function that observes a node.
Parameters:
- node: The node to observe.
Returns:
- error: An error if the observation fails.
type Traverser ¶ added in v0.2.30
type Traverser[T any] struct { // contains filtered or unexported fields }
Traverser is a struct that traverses a tree.
func NewTraverser ¶ added in v0.2.30
func NewTraverser[T any](observer ObserverFunc[T], nexts NextsFunc[T]) Traverser[T]
NewTraverser creates a new traverser with the given observer and nexts functions.
Parameters:
- observer: The function that observes a node.
- nexts: The function that computes the children of a node.