Documentation
¶
Index ¶
- type Tree
- func (t *Tree[T]) Add(parentID int, node *node.Node[T]) (addedNode bool)
- func (t *Tree[T]) AddRoot(n *node.Node[T]) (addedRoot bool)
- func (t *Tree[T]) Backtrack(id int) ([]*node.Node[T], bool)
- func (t *Tree[T]) Filter(filterFunc func(obj T) bool) (*Tree[T], bool)
- func (t *Tree[T]) Get(id int) (node *node.Node[T], found bool)
- func (t *Tree[T]) GetRoot() (root *node.Node[T], hasRoot bool)
- func (t *Tree[T]) GetStructure() ([]string, bool)
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Tree ¶
type Tree[T any] struct { // contains filtered or unexported fields }
nolint:structcheck,gocritic Tree represents the main entity of the package.
func New ¶
New creates a new Tree.
Example ¶
ExampleNew demonstrates how to create tree.
package main
import (
"github.com/johnfercher/go-tree/tree"
"github.com/johnfercher/go-tree/node"
)
func main() {
tr := tree.New[string]()
// Add nodes do tree
tr.AddRoot(node.New("root"))
// Do more things
}
func (*Tree[T]) Add ¶
Add adds a node into a parent node.
Example ¶
ExampleTree_Add demonstrates how to add node to tree.
package main
import (
"github.com/johnfercher/go-tree/tree"
"github.com/johnfercher/go-tree/node"
)
func main() {
tr := tree.New[bool]()
tr.AddRoot(node.New(true))
tr.Add(0, node.New(false))
// Do more things
}
func (*Tree[T]) AddRoot ¶
AddRoot adds a root node to Tree.
Example ¶
ExampleTree_AddRoot demonstrates how to add root node to tree.
package main
import (
"github.com/johnfercher/go-tree/tree"
"github.com/johnfercher/go-tree/node"
)
func main() {
tr := tree.New[int]()
tr.AddRoot(node.New(42))
// Do more things
}
func (*Tree[T]) Backtrack ¶
Backtrack retrieves a path from node to root.
Example ¶
ExampleTree_Backtrack demonstrates how to retrieve path of nodes from node to root.
package main
import (
"fmt"
"github.com/johnfercher/go-tree/tree"
"github.com/johnfercher/go-tree/node"
)
func main() {
tr := tree.New[string]()
tr.AddRoot(node.New("root"))
tr.Add(0, node.New("level1"))
tr.Add(1, node.New("level2"))
tr.Add(2, node.New("leaf"))
nodes, ok := tr.Backtrack(3)
if !ok {
return
}
for _, node := range nodes {
fmt.Println(node.GetData())
}
// Do more things
}
func (*Tree[T]) Get ¶
Get retrieves node from Tree.
Example ¶
ExampleTree_Get demonstrates how to retrieve node from tree.
package main
import (
"fmt"
"github.com/johnfercher/go-tree/tree"
"github.com/johnfercher/go-tree/node"
)
func main() {
tr := tree.New[uint]()
tr.AddRoot(node.New(uint(42)))
node, ok := tr.Get(0)
if !ok {
return
}
fmt.Println(node.GetData())
// Do more things
}
func (*Tree[T]) GetRoot ¶
GetRoot retrieves the root node from Tree.
Example ¶
ExampleTree_GetRoot demonstrates how to retrieve root node from tree.
package main
import (
"fmt"
"github.com/johnfercher/go-tree/tree"
"github.com/johnfercher/go-tree/node"
)
func main() {
tr := tree.New[float64]()
tr.AddRoot(node.New(3.14))
node, ok := tr.GetRoot()
if !ok {
return
}
fmt.Println(node.GetData())
// Do more things
}
func (*Tree[T]) GetStructure ¶
GetStructure retrieves Tree structure.
Example ¶
ExampleTree_GetStructure demonstrates how to retrieve tree structure.
package main
import (
"fmt"
"github.com/johnfercher/go-tree/tree"
"github.com/johnfercher/go-tree/node"
)
func main() {
tr := tree.New[string]()
tr.AddRoot(node.New("root"))
tr.Add(0, node.New("level1"))
tr.Add(1, node.New("level2"))
tr.Add(2, node.New("leaf"))
structure, ok := tr.GetStructure()
if !ok {
return
}
for _, str := range structure {
fmt.Println(str)
}
// Do more things
}