Documentation
¶
Index ¶
- type Node
- func (n *Node[T]) AddNext(node *Node[T])
- func (n *Node[T]) Backtrack() []*Node[T]
- func (n *Node[T]) Filter(filterFunc func(obj T) bool) (*Node[T], bool)
- func (n *Node[T]) GetData() T
- func (n *Node[T]) GetID() int
- func (n *Node[T]) GetNexts() []*Node[T]
- func (n *Node[T]) GetPrevious() *Node[T]
- func (n *Node[T]) GetStructure() []string
- func (n *Node[T]) IsLeaf() bool
- func (n *Node[T]) IsRoot() bool
- func (n *Node[T]) WithID(id int) *Node[T]
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 ¶
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
}
func (*Node[T]) AddNext ¶
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
}
func (*Node[T]) Backtrack ¶
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
}
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
}
func (*Node[T]) GetID ¶
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
}
func (*Node[T]) GetNexts ¶
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
}
func (*Node[T]) GetPrevious ¶
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
}
func (*Node[T]) GetStructure ¶
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
}
func (*Node[T]) IsLeaf ¶
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
}
func (*Node[T]) IsRoot ¶
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
}