avltree

package
v3.0.1 Latest Latest
Warning

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

Go to latest
Published: Aug 21, 2024 License: MIT Imports: 3 Imported by: 0

Documentation

Overview

Package avltree provides a self balancing binary search tree. See https://en.wikipedia.org/wiki/AVL_tree for details pertaining to the AVL tree data structure. The main methods Add, Find and Remove should behave the same as operations operating on Go's built-in map type.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Tree

type Tree[K, V any] struct {
	// contains filtered or unexported fields
}

Tree is an AVL tree.

func New

func New[K, V any](compareKeys math.Comparator[K], options ...TreeOption[K, V]) *Tree[K, V]

New creates an AVL tree using the supplied compare function and tree options. Avoid using pointer keys that are dereferenced by the compare function as modifying such keys outside of the tree invalidates the ordering invariant of the tree.

func (*Tree[K, V]) Add

func (tree *Tree[K, V]) Add(key K, value V)

Add association between key and value to the tree. Any existing association for key is overwritten with key and value.

func (*Tree[K, V]) All

func (tree *Tree[K, V]) All() iter.Seq2[K, V]

All returns a "left to right" iterator over the tree. Any tree mutation while iterating, except for updating the value of an existing association, invalidates the iterator and iteration terminates prematurely.

func (*Tree[K, V]) Backward

func (tree *Tree[K, V]) Backward() iter.Seq2[K, V]

Backward returns a "right to left" iterator over the tree. Any tree mutation while iterating, except for updating the value of an existing association, invalidates the iterator and iteration terminates prematurely.

func (*Tree[K, V]) Clear

func (tree *Tree[K, V]) Clear(release func(K, V))

Clear removes all associations from the tree. A non-nil release function is called on each association in the tree. The release function must not fail. Remove each association by itself if the release operation can fail and handle errors properly.

func (*Tree[K, V]) Find

func (tree *Tree[K, V]) Find(key K) (V, bool)

Find value associated with key. Returns the found value and true or the zero value of V and false if no assocation was found.

func (*Tree[K, V]) FindEqualOrGreater

func (tree *Tree[K, V]) FindEqualOrGreater(key K) (K, V, bool)

FindEqualOrGreater returns the association that match key or the immediately greater association and true. The zero values of K and V and false is returned if no assocation was found.

func (*Tree[K, V]) FindEqualOrLesser

func (tree *Tree[K, V]) FindEqualOrLesser(key K) (K, V, bool)

FindEqualOrLesser returns the association that match key or the association with the immediately lesser key and true. The zero values of K and V and false is returned if no assocation was found.

func (*Tree[K, V]) FindHighest

func (tree *Tree[K, V]) FindHighest() (K, V, bool)

FindHighest returns the association with the highest key and true. The zero value of K and V and false is returned if the tree is empty.

func (*Tree[K, V]) FindLowest

func (tree *Tree[K, V]) FindLowest() (K, V, bool)

FindLowest returns the association with the lowest key and true. The zero value of K and V and false is returned if the tree is empty.

func (*Tree[K, V]) Length

func (tree *Tree[K, V]) Length() int

Length returns the number of associations in the tree.

func (*Tree[K, V]) Remove

func (tree *Tree[K, V]) Remove(key K)

Remove any association with key from tree.

func (*Tree[K, V]) Validate

func (tree *Tree[K, V]) Validate() (balanced, sorted bool)

Validate tree invariants. A valid tree should always be balanced and sorted.

type TreeOption

type TreeOption[K, V any] func(*Tree[K, V])

func WithSyncPool

func WithSyncPool[K, V any]() TreeOption[K, V]

WithSyncPool creates a tree option to use a sync.Pool to reuse nodes to reduce pressure on the garbage collector. It may improve performance for trees with lots of updates. The option holds an instance of a sync.Pool that may be used by multiple trees in multiple go routines in a safe manner.

Jump to

Keyboard shortcuts

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