Skip to content

A generic unbalanced doubly-linked tree implementation.

License

Notifications You must be signed in to change notification settings

johnfercher/go-tree

Repository files navigation

go-tree

GoDoc Go Report Card CI Lint Codecov Visits Badge

A generic unbalanced doubly-linked tree implementation, where you can define which node will be added to each node.

Installation

  • With go get:
go get -u github.com/johnfercher/go-tree

Contributing

Command Description Dependencies
make build Build project go
make test Run unit tests go
make fmt Format files gofmt, gofumpt and goimports
make lint Check files golangci-lint and goreportcard-cli
make dod (Definition of Done) Format files and check files Same as make build, make test, make fmt and make lint
make install Install all dependencies go, curl and git
make examples Run all examples go

Features

Node

Tree

Example

package main

import (
	"fmt"
	"github.com/johnfercher/go-tree/node"

	"github.com/johnfercher/go-tree/tree"
)

// nolint:gomnd,gocritic
func main() {
	tr := tree.New[string]()

	tr.AddRoot(node.New("0.0").WithID(0))

	tr.Add(0, node.New("0.1").WithID(1))
	tr.Add(0, node.New("0.2").WithID(2))

	tr.Add(1, node.New("1.3").WithID(3))
	tr.Add(1, node.New("1.4").WithID(4))

	tr.Add(2, node.New("2.5").WithID(5))
	tr.Add(2, node.New("2.6").WithID(6))

	root, ok := tr.GetRoot()
	fmt.Println(ok)             // true
	fmt.Println(root.GetData()) // 0.0

	node, ok := tr.Get(3)
	fmt.Println(ok)             // true
	fmt.Println(node.GetData()) // 1.3

	structure, ok := tr.GetStructure()
	fmt.Println(ok)        // true
	fmt.Println(structure) // (NULL) -> (0), (0) -> (1),  (1) -> (3),  (1) -> (4),  (0) -> (2),  (2) -> (5),  (2) -> (6)

	nodes, ok := tr.Backtrack(6)
	fmt.Println(ok) // true
	for _, node := range nodes {
		fmt.Println(node.GetData()) // 2.6; 0.2; 0.0
	}
}

About

A generic unbalanced doubly-linked tree implementation.

Topics

Resources

License

Stars

Watchers

Forks

Sponsor this project

 

Packages

No packages published