Documentation
¶
Index ¶
- type Node
- func (n *Node[T]) AddNext(node *Node[T])
- func (n *Node[T]) Backtrack() []*Node[T]
- 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
- type Tree
- func (t *Tree[T]) Add(parentID int, node *Node[T]) (addedNode bool)
- func (t *Tree[T]) AddRoot(node *Node[T]) (addedRoot bool)
- func (t *Tree[T]) Backtrack(id int) ([]*Node[T], bool)
- func (t *Tree[T]) Get(id int) (node *Node[T], found bool)
- func (t *Tree[T]) GetRoot() (root *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 Node ¶
type Node[T any] struct { // contains filtered or unexported fields }
nolint:structcheck,gocritic Node is the base of Tree construction.
func NewNode ¶
NewNode creates a new node.
Example ¶
ExampleNewNode demonstrates how to create a node.
package main import ( "github.com/johnfercher/go-tree/tree" ) func main() { n := tree.NewNode("node") n.GetData() // Do more things }
func NewNodeWithID ¶ added in v1.0.3
NewNodeWithID creates a new node with ID.
Example ¶
NewNodeWithID demonstrates how to create a node with ID.
package main import ( "github.com/johnfercher/go-tree/tree" ) func main() { n := tree.NewNodeWithID(0, "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/tree" ) func main() { n1 := tree.NewNodeWithID(0, 'a') n2 := tree.NewNodeWithID(0, '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/tree" ) func main() { n1 := tree.NewNodeWithID(0, 'a') n2 := tree.NewNodeWithID(0, 'b') n3 := tree.NewNodeWithID(0, '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 ¶ added in v1.0.3
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/tree" ) func main() { n := tree.NewNodeWithID(0, 3.14) data := n.GetData() fmt.Println(data) // Do more things }
func (*Node[T]) GetID ¶ added in v1.0.3
GetID retrieves id from node.
Example ¶
ExampleNode_GetID demonstrates how to retrieve id from node.
package main import ( "fmt" "github.com/johnfercher/go-tree/tree" ) func main() { n := tree.NewNodeWithID(0, 3.14) id := n.GetID() fmt.Println(id) // Do more things }
func (*Node[T]) GetNexts ¶ added in v1.0.2
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/tree" ) func main() { root := tree.NewNodeWithID(0, "root") leaf := tree.NewNodeWithID(1, "leaf") root.AddNext(leaf) nexts := root.GetNexts() fmt.Println(len(nexts)) // Do more things }
func (*Node[T]) GetPrevious ¶ added in v1.0.2
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/tree" ) func main() { root := tree.NewNodeWithID(0, "root") leaf := tree.NewNodeWithID(1, "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/tree" ) func main() { n1 := tree.NewNodeWithID(0, 'a') n2 := tree.NewNodeWithID(0, 'b') n3 := tree.NewNodeWithID(0, '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/tree" ) func main() { n1 := tree.NewNodeWithID(0, 'a') n2 := tree.NewNodeWithID(0, '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/tree" ) func main() { n := tree.NewNodeWithID(0, 'b') root := n.IsRoot() fmt.Println(root) // Do more things }
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" ) func main() { tr := tree.New[string]() // Add nodes do tree tr.AddRoot(tree.NewNodeWithID(0, "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" ) func main() { tr := tree.New[bool]() tr.AddRoot(tree.NewNodeWithID(0, true)) tr.Add(0, tree.NewNodeWithID(1, 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" ) func main() { tr := tree.New[int]() tr.AddRoot(tree.NewNodeWithID(0, 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" ) func main() { tr := tree.New[string]() tr.AddRoot(tree.NewNodeWithID(0, "root")) tr.Add(0, tree.NewNodeWithID(1, "level1")) tr.Add(1, tree.NewNodeWithID(2, "level2")) tr.Add(2, tree.NewNodeWithID(3, "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" ) func main() { tr := tree.New[uint]() tr.AddRoot(tree.NewNodeWithID(0, 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" ) func main() { tr := tree.New[float64]() tr.AddRoot(tree.NewNodeWithID(0, 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" ) func main() { tr := tree.New[string]() tr.AddRoot(tree.NewNodeWithID(0, "root")) tr.Add(0, tree.NewNodeWithID(1, "level1")) tr.Add(1, tree.NewNodeWithID(2, "level2")) tr.Add(2, tree.NewNodeWithID(3, "leaf")) structure, ok := tr.GetStructure() if !ok { return } for _, str := range structure { fmt.Println(str) } // Do more things }