Node

package
v0.2.17 Latest Latest
Warning

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

Go to latest
Published: Mar 16, 2024 License: MIT Imports: 4 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

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 func(T) error) 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.

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 func(T) error) 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.

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 func(T) bool) 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.

func (*Node[T]) String

func (n *Node[T]) String() string

String is a method of Node that returns a string representation of the node.

Returns:

  • string: A string representation of the node.

Jump to

Keyboard shortcuts

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