Golang package tree is pure golang implementation of tree with any direction nodes. Each node in the tree can be connected to many children and has many parents.

In the implementation of this tree, parents are no different from children. The tree element contains references to branches without specifying whether it is a parent or a child.

Each node reference in an element contains a path cost that is taken into account when calculating the path from point A to point B. The path between points A and B calculates using threads (parallel) to minimize time of find all paths.

This tree has no root. Paths in a tree can be looped.

The tree elements can store any data structure. This structure is defined when the tree is created.

See the part of code to create Tree and it Elements:

```
t := tree.New[TreeData]("My first element")
// Create new tree elements (first element and end point element)
e := t.New("My first element")
ep := t.New("End point")
// Create children of e (first element) element
ch1, _ := e.Add(t.New("My first child"))
ch2, _ := e.Add(t.New("My second child"), tree.WayOptions{Cost: 3.0})
ch4, _ := e.Add(t.New("My fourth child"), tree.WayOptions{Cost: 3.0})
// Create sub children elements
ch3, _ := ch2.Add(t.New("Some third child"))
ch3.Add(ch4)
// Add children to ep (end point) element
ch1.Add(ch4)
ch2.Add(ep)
ch4.Add(ep)
// Set oneway path from ep (end point) to e (first element) element
ep.Add(e, tree.WayOptions{Cost: 5.0, Oneway: true})
// Print tree started from e (first element) element
fmt.Printf("\nPrint tree:\n%v\n\n", e)
```

The result of last printf:

```
. My first element
├── My first child, cost: 1.00
│ ├── My fourth child, cost: 1.00
│ │ ├── My first element, cost: 3.00 🡡
│ │ ├── Some third child, cost: 1.00
│ │ │ ├── My second child, cost: 1.00
│ │ │ │ ├── My first element, cost: 3.00 🡡
│ │ │ │ ├── Some third child, cost: 1.00 🡡
│ │ │ │ └── End point, cost: 1.00
│ │ │ │ ├── My second child, cost: 1.00 🡡
│ │ │ │ ├── My fourth child, cost: 1.00 🡡
│ │ │ │ └── My first element, cost: 5.00 (one way road) 🡡
│ │ │ └── My fourth child, cost: 1.00 🡡
│ │ ├── My first child, cost: 1.00 🡡
│ │ └── End point, cost: 1.00 🡡
│ └── My first element, cost: 1.00 🡡
├── My second child, cost: 3.00 🡡
├── My fourth child, cost: 3.00 🡡
└── End point, cost: 5.00 (way not allowed) 🡡
```

See the code and more descriptions and example at: https://github.com/kirill-scherba/tree

## Top comments (0)