tree

package module
v0.0.0-...-3ea18f6 Latest Latest
Warning

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

Go to latest
Published: Aug 7, 2023 License: MIT Imports: 3 Imported by: 0

README

tree

Library for work with Binary trees.

You can create a Binary node and use a list of functions to work with it.

Tree functions

Empty tree's creation example
t := tree.New[int]() // empty int tree
t := tree.New[string]() // empty string tree
Tree's creation with one element example
t := tree.NewWithElement[int](1,1) // int tree creation with one element
t := tree.NewWithElement[string]("key", "value") // string tree creation with one element
Insert element to tree
t := tree.New[int]() // empty int tree
t.Insert(22, 22) // insert to tree element: key=22, value=22
t.Insert(8, 8) // insert to tree element: key=8, value=8
t.Insert(4, 4) // insert to tree element: key=4, value=4

// or
t.InsertWithoutRecursion(4, 4) // insert to tree element: key=4, value=4
Tree traversal

you can make tree traversal by two methods:

t := tree.New[int]()
t.Insert(22, 22) 
t.Insert(8, 8)
t.Insert(4, 4)

resultAsc := t.InOrderTreeWalk(tree.Asc)   // [4, 8, 22]
resultDesc := t.InOrderTreeWalk(tree.Desc)   // [22, 8, 4]

// or
resultAsc := t.InOrderTreeWalkWithStack(tree.Asc)   // [4, 8, 22]
resultDesc := t.InOrderTreeWalkWithStack(tree.Desc)   // [22, 8, 4]

Exists element
t := tree.New[int]()
t.Insert(22, 22) 
t.Insert(8, 8)
t.Insert(4, 4)

resultNil := t.Exists(15) // false
result    := t.Exists(8)  // true
Get value by key element
t := tree.New[int]()
t.Insert(22, 22) 
t.Insert(8, 8)
t.Insert(4, 4)

resultNil, err := t.GetValue(15) // nil, err
result, err    := t.GetValue(8)  // 8, nil
Min tree element
t := tree.New[int]()
t.Insert(22, 22) 
t.Insert(8, 8)
t.Insert(4, 4)

result := t.Min() // 4
Max tree element
t := tree.New[int]()
t.Insert(22, 22) 
t.Insert(8, 8)
t.Insert(4, 4)

result := t.Max() // 22
PreOrder Successor
t := tree.New[int]()
t.Insert(22, 22) 
t.Insert(8, 8)
t.Insert(4, 4)

resultNil, err := t.PreOrderSuccessor(22) // nil, err
result, err    := t.PreOrderSuccessor(8)  // 22, nil
PostOrder Successor
t := tree.New[int]()
t.Insert(22, 22) 
t.Insert(8, 8)
t.Insert(4, 4)

resultNil, err := t.PostOrderSuccessor(4) // nil, err
result, err    := t.PostOrderSuccessor(8)  // 4, nil
Delete element by key from tree
t := tree.New[int]()
t.Insert(22, 22) 
t.Insert(8, 8)
t.Insert(4, 4)

err := t.Delete(22) // without err

Documentation

Overview

Package tree is a package for work with Binary trees.

Index

Constants

View Source
const (
	// Desc specifies the sort direction to be descending.
	Desc direction = "desc"
	// Asc specifies the sort direction to be ascending.
	Asc direction = "asc"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Tree

type Tree[V constraints.Ordered] struct {
	// contains filtered or unexported fields
}

func New

func New[V constraints.Ordered]() *Tree[V]

New is a function for creation empty tree - param should be `ordered type` (`int`, `string`, `float` etc)

func NewWithElement

func NewWithElement[V constraints.Ordered](key V, value any) *Tree[V]

NewWithElement is a function for creation tree with one element - param should be `ordered type` (`int`, `string`, `float` etc)

func (*Tree[V]) Delete

func (t *Tree[V]) Delete(key V)

Delete is a function for deleting node in node - param key should be `ordered type` (`int`, `string`, `float` etc)

func (*Tree[V]) Exists

func (t *Tree[V]) Exists(key V) bool

Exists is a function for searching element in node. If element exists in tree- return true, else - false - param key should be `ordered type` (`int`, `string`, `float` etc)

func (*Tree[V]) GetValue

func (t *Tree[V]) GetValue(key V) (any, error)

GetValue is a function for searching element in node and returning value of this element - param key should be `ordered type` (`int`, `string`, `float` etc)

func (*Tree[V]) InOrderTreeWalk

func (t *Tree[V]) InOrderTreeWalk(d direction) []V

InOrderTreeWalk is a function for getting ordered array of tree's elements. - param key should be `ordered type` (`int`, `string`, `float` etc)

func (*Tree[V]) InOrderTreeWalkWithStack

func (t *Tree[V]) InOrderTreeWalkWithStack(d direction) []V

InOrderTreeWalkWithStack is a function for getting ordered array of tree's elements. - param key should be `ordered type` (`int`, `string`, `float` etc)

func (*Tree[V]) Insert

func (t *Tree[V]) Insert(key V, value any)

Insert is a function for inserting element into node - param key should be `ordered type` (`int`, `string`, `float` etc.) - param value can be any type

func (*Tree[V]) InsertWithoutRecursion

func (t *Tree[V]) InsertWithoutRecursion(key V, value any)

InsertWithoutRecursion is a function for inserting element into node - param key should be `ordered type` (`int`, `string`, `float` etc.) - param value can be any type

func (*Tree[V]) Max

func (t *Tree[V]) Max() V

Max is a function for searching max element in tree (by key).

func (*Tree[V]) Min

func (t *Tree[V]) Min() V

Min is a function for searching min element in tree (by key).

func (*Tree[V]) PostOrderSuccessor

func (t *Tree[V]) PostOrderSuccessor(key V) (V, error)

PostOrderSuccessor is a function for searching postOrder key for income element (if we found it by input key param) - param key should be `ordered type` (`int`, `string`, `float` etc)

func (*Tree[V]) PreOrderSuccessor

func (t *Tree[V]) PreOrderSuccessor(key V) (V, error)

PreOrderSuccessor is a function for searching preOrder key for income element (if we found it by input key param) - param key should be `ordered type` (`int`, `string`, `float` etc)

Jump to

Keyboard shortcuts

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