node

package
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Feb 1, 2024 License: MIT Imports: 1 Imported by: 38

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Node

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

nolint:structcheck,gocritic Node is the base of Tree construction.

func New

func New[T any](data T) *Node[T]

New creates a new node.

Example

New demonstrates how to create a node with ID.

package main

import (
	"github.com/johnfercher/go-tree/node"
)

func main() {
	n := node.New("node")

	n.GetData()

	// Do more things
}
Output:

func (*Node[T]) AddNext

func (n *Node[T]) AddNext(node *Node[T])

AddNext add node to current node.

Example

ExampleNode_AddNext demonstrates how to add a node to a parent.

package main

import (
	"github.com/johnfercher/go-tree/node"
)

func main() {
	n1 := node.New('a')
	n2 := node.New('b')

	n1.AddNext(n2)

	// Do more things
}
Output:

func (*Node[T]) Backtrack

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

Backtrack retrieves a path from node to root.

Example

ExampleNode_Backtrack demonstrates how to retrieve the path between node to root.

package main

import (
	"fmt"

	"github.com/johnfercher/go-tree/node"
)

func main() {
	n1 := node.New('a')
	n2 := node.New('b')
	n3 := node.New('c')

	n1.AddNext(n2)
	n2.AddNext(n3)

	nodes := n3.Backtrack()
	for _, node := range nodes {
		fmt.Println(node.GetData())
	}

	// Do more things
}
Output:

func (*Node[T]) Filter added in v1.1.0

func (n *Node[T]) Filter(filterFunc func(obj T) bool) (*Node[T], bool)

Filter remove all sub-nodes that doesn´t respect a rule.

func (*Node[T]) GetData

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

GetData retrieves data from node.

Example

ExampleNode_GetData demonstrates how to retrieve data from node.

package main

import (
	"fmt"

	"github.com/johnfercher/go-tree/node"
)

func main() {
	n := node.New(3.14)

	data := n.GetData()
	fmt.Println(data)

	// Do more things
}
Output:

func (*Node[T]) GetID

func (n *Node[T]) GetID() int

GetID retrieves id from node.

Example

ExampleNode_GetID demonstrates how to retrieve id from node.

package main

import (
	"fmt"

	"github.com/johnfercher/go-tree/node"
)

func main() {
	n := node.New(3.14).WithID(1)

	id := n.GetID()
	fmt.Println(id)

	// Do more things
}
Output:

func (*Node[T]) GetNexts

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

GetNexts retrieves the next nodes.

Example

ExampleNode_GetNexts demonstrates how to retrieve next nodes from node.

package main

import (
	"fmt"

	"github.com/johnfercher/go-tree/node"
)

func main() {
	root := node.New("root")
	leaf := node.New("leaf")

	root.AddNext(leaf)
	nexts := root.GetNexts()
	fmt.Println(len(nexts))

	// Do more things
}
Output:

func (*Node[T]) GetPrevious

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

GetPrevious retrieves the next nodes.

Example

ExampleNode_GetPrevious demonstrates how to retrieve next nodes from node.

package main

import (
	"fmt"

	"github.com/johnfercher/go-tree/node"
)

func main() {
	root := node.New("root")
	leaf := node.New("leaf")

	root.AddNext(leaf)
	previous := leaf.GetPrevious()
	fmt.Println(previous.GetData())

	// Do more things
}
Output:

func (*Node[T]) GetStructure

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

GetStructure retrieves the node structure.

Example

ExampleNode_GetStructure demonstrates how to retrieve the tree structure from node.

package main

import (
	"fmt"

	"github.com/johnfercher/go-tree/node"
)

func main() {
	n1 := node.New('a')
	n2 := node.New('b')
	n3 := node.New('c')

	n1.AddNext(n2)
	n2.AddNext(n3)

	structure := n3.GetStructure()
	for _, str := range structure {
		fmt.Println(str)
	}

	// Do more things
}
Output:

func (*Node[T]) IsLeaf

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

IsLeaf retrieves info if node is leaf.

Example

ExampleNode_IsLeaf demonstrates how to retrieve info if node is leaf.

package main

import (
	"fmt"

	"github.com/johnfercher/go-tree/node"
)

func main() {
	n1 := node.New('a')
	n2 := node.New('b')

	n1.AddNext(n2)

	leaf := n2.IsLeaf()
	fmt.Println(leaf)

	// Do more things
}
Output:

func (*Node[T]) IsRoot

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

IsRoot retrieves info if node is root.

Example

ExampleNode_IsRoot demonstrates how to retrieve info if node is root.

package main

import (
	"fmt"

	"github.com/johnfercher/go-tree/node"
)

func main() {
	n := node.New('b')

	root := n.IsRoot()
	fmt.Println(root)

	// Do more things
}
Output:

func (*Node[T]) WithID

func (n *Node[T]) WithID(id int) *Node[T]

WithID retrieves data from node.

Jump to

Keyboard shortcuts

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