tree

package
v1.0.3 Latest Latest
Warning

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

Go to latest
Published: Sep 14, 2023 License: MIT Imports: 1 Imported by: 2

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 NewNode

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

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

func NewNodeWithID[T any](id int, data T) *Node[T]

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

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/tree"
)

func main() {
	n1 := tree.NewNodeWithID(0, 'a')
	n2 := tree.NewNodeWithID(0, 'b')

	n1.AddNext(n2)

	// Do more things
}

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/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

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/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

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/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

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/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

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/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

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/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

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/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

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"
)

func main() {
	tr := tree.New[string]()

	// Add nodes do tree
	tr.AddRoot(tree.NewNodeWithID(0, "root"))

	// Do more things
}

func (*Tree[T]) Add

func (t *Tree[T]) Add(parentID int, 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"
)

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

func (t *Tree[T]) AddRoot(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"
)

func main() {
	tr := tree.New[int]()

	tr.AddRoot(tree.NewNodeWithID(0, 42))

	// Do more things
}

func (*Tree[T]) Backtrack

func (t *Tree[T]) Backtrack(id int) ([]*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"
)

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

func (t *Tree[T]) Get(id int) (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"
)

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

func (t *Tree[T]) GetRoot() (root *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"
)

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

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"
)

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
}

Jump to

Keyboard shortcuts

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