tree

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: 1

Documentation

Index

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

func New[T any]() *Tree[T]

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
}
Output:

func (*Tree[T]) Add

func (t *Tree[T]) Add(parentID int, node *node.Node[T]) (addedNode bool)

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
}
Output:

func (*Tree[T]) AddRoot

func (t *Tree[T]) AddRoot(n *node.Node[T]) (addedRoot bool)

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
}
Output:

func (*Tree[T]) Backtrack

func (t *Tree[T]) Backtrack(id int) ([]*node.Node[T], bool)

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
}
Output:

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

func (t *Tree[T]) Filter(filterFunc func(obj T) bool) (*Tree[T], bool)

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

func (*Tree[T]) Get

func (t *Tree[T]) Get(id int) (node *node.Node[T], found bool)

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
}
Output:

func (*Tree[T]) GetRoot

func (t *Tree[T]) GetRoot() (root *node.Node[T], hasRoot bool)

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
}
Output:

func (*Tree[T]) GetStructure

func (t *Tree[T]) GetStructure() ([]string, bool)

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
}
Output:

Jump to

Keyboard shortcuts

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