Node

package
v0.2.30 Latest Latest
Warning

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

Go to latest
Published: Apr 23, 2024 License: MIT Imports: 5 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type NextsFunc added in v0.2.30

type NextsFunc[T any] func(node T) ([]T, error)

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

func NewNode[T any](data T) Node[T]

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

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

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

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

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

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

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

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

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

func (n *Node[T]) HasChild(child *Node[T]) bool

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

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

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

type ObserverFunc[T any] func(node T) error

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.

func (*Traverser[T]) BFS added in v0.2.30

func (t *Traverser[T]) BFS(node T) error

BFS traverses the tree in breadth-first order.

Parameters:

  • node: The root node of the tree.

Returns:

  • error: An error if the traversal fails.

func (*Traverser[T]) DFS added in v0.2.30

func (t *Traverser[T]) DFS(node T) error

DFS traverses the tree in depth-first order.

Parameters:

  • node: The root node of the tree.

Returns:

  • error: An error if the traversal fails.

Jump to

Keyboard shortcuts

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