dsa

package module
v0.1.2 Latest Latest
Warning

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

Go to latest
Published: Mar 27, 2022 License: MIT Imports: 7 Imported by: 0

README

dsa

Data structure and algorithm in golang

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Default

func Default[T comparable](v T, def T) T

Default return v or default if v has zero value

func Max added in v0.1.2

func Max[T constraints.Ordered](v1, v2 T) T

Max return maximum values

func MaxSlice added in v0.1.2

func MaxSlice[T constraints.Ordered](va ...T) T

MaxSlice return maximum values within slices

func Min added in v0.1.2

func Min[T constraints.Ordered](v1, v2 T) T

Min return minum between two values

func MinSlice added in v0.1.2

func MinSlice[T constraints.Ordered](va ...T) T

MinSlice return minimum values for given slices

Types

type FilterFn

type FilterFn[T any] func(v T) bool

FilterFn filter node function

type Node

type Node[T NodeType] struct {
	Value T
	Nodes Nodes[T]
	// contains filtered or unexported fields
}

Node stores tree node information

func (Node[T]) Leaf

func (n Node[T]) Leaf() (bool, int)

Leaf return true if this node doesn't have any children

type NodeType

type NodeType interface {
	comparable
	constraints.Ordered
}

NodeType constraint having comparable and Ordered constraint

type Nodes

type Nodes[T NodeType] []*Node[T]

Nodes list of tree nodes

func (Nodes[T]) Sort

func (n Nodes[T]) Sort(order ...SortOrder)

Sort nodes in ASCENDING / DESCENDING order. If parameter is not specified, nodes will be sorted in ASCENDING order.

type SearchFn

type SearchFn[T NodeType] func(nodes Nodes[T], value T) (*Node[T], int)

SearchFn search node function

type SortOrder

type SortOrder int

SortOrder types

const (
	Unsorted SortOrder = iota
	Ascending
	Descending
)

List of sort order

type StringTreeArg

type StringTreeArg struct {
	Sep         string
	Name        string
	FnFilter    FilterFn[string]
	FnTransform TransformFn[string]
}

StringTreeArg for generating tree

type TransformFn

type TransformFn[T any] func(v T) T

TransformFn node function

type Tree

type Tree[T NodeType] struct {
	// contains filtered or unexported fields
}

Tree structure

func NewStringTree

func NewStringTree(r io.Reader, arg StringTreeArg) *Tree[string]

NewStringTree with given name

func NewTree

func NewTree[T NodeType](name string) *Tree[T]

NewTree create tree structure with given name

func (*Tree[T]) Empty

func (t *Tree[T]) Empty() bool

Empty return true if tree has no node

func (*Tree[T]) ExactMatch

func (t *Tree[T]) ExactMatch(values []T) (*Node[T], bool)

Match all values

func (*Tree[T]) Insert

func (t *Tree[T]) Insert(values []T)

Insert values to tree

func (*Tree[T]) Match

func (t *Tree[T]) Match(values []T) (*Node[T], bool)

Match partially or all values

func (*Tree[T]) MaxDepth added in v0.1.2

func (t *Tree[T]) MaxDepth() int

MaxDepth return maksimum tree depth

func (*Tree[T]) MinDepth added in v0.1.2

func (t *Tree[T]) MinDepth() int

MinDepth return minimum tree depth

func (*Tree[T]) Name

func (t *Tree[T]) Name() string

Name of the tree

func (*Tree[T]) PrintTo

func (t *Tree[T]) PrintTo(w io.Writer)

PrintTo for debugging purpose

func (*Tree[T]) Sort

func (t *Tree[T]) Sort(order ...SortOrder)

Sort nodes in ASCENDING / DESCENDING order. If parameter is not specified, nodes will be sorted in ASCENDING order.

func (*Tree[T]) SortOrder

func (t *Tree[T]) SortOrder() SortOrder

SortOrder return nodes sorting order

func (*Tree[T]) Sorted

func (t *Tree[T]) Sorted() bool

Sorted return true if nodes are sorted either Ascending/Descending

func (*Tree[T]) Visit

func (t *Tree[T]) Visit(values []T) *Node[T]

Visit all values and return last visited nodes or nil if value not found in tree.

Jump to

Keyboard shortcuts

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