avl

package
v1.0.8 Latest Latest
Warning

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

Go to latest
Published: Nov 4, 2023 License: GPL-3.0 Imports: 1 Imported by: 2

Documentation

Overview

This package implements balanced and threaded trees (D. E. Knuth, The Art of Computer Programming, vol. 3, Sorting and Searching). Trees may be sorted or not; they are guaranteed to be sorted if insertion of values are made through the method 'SearchIns' (with a correct total 'Comparer'); if methods 'Insert', 'Prepend', 'Append' or 'Cat' are used, sorting may not be maintained. With sorted trees, methods 'SearchIns', 'Search', 'SearchNext' and 'Delete' can be used, but not with unsorted trees; all other methods may be used with all trees.

Index

Constants

View Source
const (

	//Results of comparison.
	Lt = Comp(-1) //less than
	Eq = Comp(0)  //equal
	Gt = Comp(+1) //greater than

)

Variables

This section is empty.

Functions

This section is empty.

Types

type Comp

type Comp = int8

Result of a comparison.

type Comparer

type Comparer interface {
	// Compare two values; the comparison must be reflexive, transitive, antisymmetric and total.
	Compare(Comparer) Comp
}

Values in elements of trees must verify the interface 'Comparer' if these trees implement the methods 'SearchIns', 'Search', 'SearchNext' or 'Delete'; in this case, if the methods 'Insert', 'Prepend', 'Append' and 'Cat' are not used, trees remain sorted.

type Copier

type Copier interface {
	// Copy a value.
	Copy() Copier
}

Values in elements of trees must verify the interface 'Copier' if these trees should be copied; for a value 'val', 'val.Copy ()' must return a copy of 'val'.

type DoFunc

type DoFunc func(v interface{}, p ...interface{})

Type of function used by 'WalkThrough'; 'v' is the value of an element of tree, 'p' are parameters.

type Elem

type Elem struct {
	// contains filtered or unexported fields
}

Element of a tree.

func (*Elem) SetVal

func (e *Elem) SetVal(v interface{})

'SetVal' sets the value 'v' into the element 'e'.

func (*Elem) Val

func (e *Elem) Val() interface{}

'Val' returns the value contained in the element 'e'.

type Tree

type Tree struct {
	// contains filtered or unexported fields
}

A tree.

func New

func New() *Tree

New creates a new empty tree.

func (*Tree) Append

func (t *Tree) Append(key interface{})

'Append' inserts the value 'key' at the last place into the tree 't'.

func (*Tree) Cat

func (t1 *Tree) Cat(t2 *Tree)

'Cat' concatenates the trees 't1' and 't2'; it returns the result in 't1', and 't2' is no more valid.

func (*Tree) Copy

func (t *Tree) Copy() *Tree

'Copy' returns a copy of 't'; values owned by elements of 't' must verify the interface 'Copier'

func (*Tree) Delete

func (t *Tree) Delete(key Comparer) bool

'Delete' erases the value 'key', if it exists, in the sorted tree 't'; returns true if 'key' is found, else does nothing.

func (*Tree) Empty

func (t *Tree) Empty()

'Empty' empties 't'

func (*Tree) Erase

func (t *Tree) Erase(rank int)

'Erase' erases the element at position 'rank' in the tree 't'; 'rank' must verify 1 <= rank <= t.NumberOfElems().

func (*Tree) Find

func (t *Tree) Find(rank int) (res *Elem, found bool)

'Find' finds and returns in 'res' the element at position 'rank' in the tree 't'; if the element does not exist, 'found' is false and 'res' returns nil. The rank of the first element in a tree is 1.

func (*Tree) Insert

func (t *Tree) Insert(key interface{}, rank int)

'Insert' inserts the value 'key' at the position Min(Max(rank, 1), t.NumberOfElems() + 1) in the tree 't'. The rank of the first element in a tree is 1.

func (*Tree) IsEmpty

func (t *Tree) IsEmpty() bool

Is 't' empty?

func (*Tree) Next

func (t *Tree) Next(e *Elem) *Elem

'Next' returns the element following 'e' in the tree 't'; if 'e' is nil, the first element is returned; if 'e' is the last element of the tree, nil is returned.

func (*Tree) NumberOfElems

func (t *Tree) NumberOfElems() int

'NumberOfElems' returns the number of elements in the tree 't'.

func (*Tree) Prepend

func (t *Tree) Prepend(key interface{})

'Prepend' inserts the value 'key' at the first place into the tree 't'.

func (*Tree) Previous

func (t *Tree) Previous(e *Elem) *Elem

'Previous' returns the element preceding 'e' in the tree 't'; if 'e' is nil, the last element is returned; if 'e' is the first element of the tree, nil is returned.

func (*Tree) Search

func (t *Tree) Search(key Comparer) (res *Elem, found bool, rank int)

'Search' searches the value 'key' in the sorted tree 't'; if 'key' is found, 'found' is true, the element containing this value is returned in 'res' and its rank in the tree in 'rank'; if 'key' is not found, 'res' is nil, 'found' is false and 'rank' is 0. The rank of the first element in a tree is 1.

func (*Tree) SearchIns

func (t *Tree) SearchIns(key Comparer) (res *Elem, found bool, rank int)

'SearchIns' searches the value 'key' in the sorted tree 't'; if 'key' is found, 'found' is true, the element containing this value is returned in 'res' and its rank in the tree in 'rank'; if 'key' is not found, it is inserted into a new element returned in 'res', 'found' is false and 'rank' returns the rank of the new element. The rank of the first element in a tree is 1.

func (*Tree) SearchNext

func (t *Tree) SearchNext(key Comparer) (res *Elem, found bool, rank int)

SearchNext searches the value 'key' in the sorted tree 't', or the next value if not found; if 'key' is found, 'found' is true, the element containing this value is returned in 'res' and its rank in the tree in 'rank'; if 'key' is not found, 'found' is false, 'res' returns the element containing the next value and its rank is returned in 'rank'; if 'key' is not found and there is no next value, 'res' is nil and 'rank' is 0. The rank of the first element in a tree is 1.

func (*Tree) Split

func (t1 *Tree) Split(after int) (t2 *Tree)

'Split' splits the tree 't1' after the element of rank 'after'; the result is returned in 't1' and 't2'.

func (*Tree) WalkThrough

func (t *Tree) WalkThrough(do DoFunc, p ...interface{})

'WalkThrough' traverses the tree 't' in inorder and calls the function 'do', with 'p' as parameters, on the value of each element.

Jump to

Keyboard shortcuts

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