tree

package module
v0.0.5 Latest Latest
Warning

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

Go to latest
Published: Sep 6, 2023 License: BSD-3-Clause Imports: 4 Imported by: 0

README

Tree

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.

Multinode tree

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.

GoDoc Go Report Card

How to install it

The reference implementation in this repo is written in Go. To use tree in a Go program, install the code using this command: go get -u github.com/kirill-scherba/tree.

How to use it

There is basic example ./examples/simple/ which use basic tree functions. You can execute it locally:

go run ./examples/simple/

or run it directly from github:

go run github.com/kirill-scherba/tree/examples/simple@latest

This sample creates tree. And some elements, set path between elements. Print the tree and calculate path between elements.

Print tree example:

. My first element
├── My first child, cost: 1.00
│   ├── My first element, 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 second child, cost: 3.00 🡡
├── My fourth child, cost: 3.00 🡡
└── End point, cost: 5.00 (way not allowed) 🡡

You can find complete packets documentation at: https://pkg.go.dev/github.com/kirill-scherba/tree

Licence

BSD

Documentation

Overview

Tree is thread protected any direction multi-child tree implementation with way cost child.

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrChildAlreadyAdded  = errors.New("child already added")
	ErrParentAlreadyAdded = errors.New("parent already added")
)

Functions

This section is empty.

Types

type Element

type Element[T TreeData] struct {
	*sync.RWMutex
	// contains filtered or unexported fields
}

Element is the multi-chields tree element

func (*Element[T]) Add

func (e *Element[T]) Add(c *Element[T], options ...WayOptions) (*Element[T],
	error)

Add adds way from e tree element to the c tree element

func (*Element[T]) Cost

func (e *Element[T]) Cost(c *Element[T]) (cost float64, ok bool)

Cost returns elements way to child cost

func (*Element[T]) Del

func (e *Element[T]) Del(c *Element[T]) (*Element[T], error)

Del delete child from tree element

func (*Element[T]) PathTo

func (e *Element[T]) PathTo(dst *Element[T]) (p *PathArray[T])

PathTo finds pathes from current element to dst element in the tree

func (*Element[T]) Remove

func (e *Element[T]) Remove() (*Element[T], error)

Remove delete element from tree

func (*Element[T]) String

func (e *Element[T]) String() (str string)

String prints the tree started from e element

func (*Element[T]) Value

func (e *Element[T]) Value() T

Value returns elements value

func (*Element[T]) WayAllowed

func (e *Element[T]) WayAllowed(c *Element[T]) bool

WayAllowed return true if the path from e to c element is available

func (*Element[T]) Ways

func (e *Element[T]) Ways() waysMap[T]

Ways returns elments ways maps

type Path

type Path[T TreeData] struct {
	Cost float64
	Path []*Element[T]
}

Path contains cost of way through path, and path - array of elements

type PathArray

type PathArray[T TreeData] struct {
	// contains filtered or unexported fields
}

PathArray contains array of Path and mrthods to process this array

func (*PathArray[T]) Append

func (p *PathArray[T]) Append(path Path[T])

Append appends path to PathArray

func (*PathArray[T]) Len

func (p *PathArray[T]) Len() int

Len returns length of PathArray

func (*PathArray[T]) Sort

func (p *PathArray[T]) Sort(options ...PathSortOptions) *PathArray[T]

Sort sorts PathArray

func (*PathArray[T]) String

func (p *PathArray[T]) String() (str string)

String stringify PathArray

type PathSortOptions

type PathSortOptions struct {
	SortByCost      bool // default true
	SortByPeers     bool // default true
	SortByCostFirst bool // default true
}

PathSortOptions is path sort options used in Sort function

type Tree

type Tree[T TreeData] struct{}

Tree is the tree methods receiver

func New

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

New creates new multi-chields tree

func (*Tree[T]) New

func (t *Tree[T]) New(value T) *Element[T]

New creates newtree element

type TreeData

type TreeData interface {
	String() string
}

TreeData interface represents TreeData required methods

type WayOptions

type WayOptions struct {
	// Cost (weight) of this way (way to this element)
	Cost float64

	// If true than The road available only oneway from this element to selected
	// element (to child), and not available back from selected element (from
	// child) to this element
	Oneway bool
}

WayOptions is way options

Directories

Path Synopsis
examples
simple
Basic example of Tree package.
Basic example of Tree package.

Jump to

Keyboard shortcuts

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