tree

package
v2.8.2 Latest Latest
Warning

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

Go to latest
Published: Apr 13, 2022 License: MIT Imports: 0 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Map

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

Map is a map type associating keys to values in a similar way to the standard Go map type, but backed by a balanced binary tree instead of a hashmap, which maintains ordering of keys.

The zero-value is a valid empty map which supports lookups and deletes, but must be initialized prior to inserting any keys.

func NewMap

func NewMap[K, V any](cmp func(K, K) int) *Map[K, V]

NewMap instantiates a new map using the given comparison function to order the keys.

func (*Map[K, V]) Delete

func (m *Map[K, V]) Delete(key K) (value V, deleted bool)

Delete deletes the given key from the map. If the key does not exist, the map is not modified. The method returns the value removed from the map and a boolean indicating whether the key was found.

Complexity: O(log n)

func (*Map[K, V]) Init

func (m *Map[K, V]) Init(cmp func(K, K) int)

Init initializes (or re-initializes) the map. The comparison function passed as argument will be used to order the keys.

Init must be called prior to inserting keys in the map, otherwise inserts will panic.

Complexity: O(1)

func (*Map[K, V]) Insert

func (m *Map[K, V]) Insert(key K, value V) (previous V, replaced bool)

Insert inserts a new entry in the map, or replaces the value if the key already existed. The method returns the previous value associated with the key or the zero-value if the key did not exist, and a boolean indicating whether the value was replaced.

The map must have been initialized by a call to NewMap or Init or the call to Insert will panic.

Complexity: O(log n)

func (*Map[K, V]) Len

func (m *Map[K, V]) Len() int

Len returns the number of entries currently held in the map.

Complexity: O(1)

func (*Map[K, V]) Lookup

func (m *Map[K, V]) Lookup(key K) (value V, found bool)

Lookup returns the value associated with the given key in the map, and a boolean value indicating whether the key was found in the map.

Complexity: O(log n)

func (*Map[K, V]) Max

func (m *Map[K, V]) Max() (key K, value V, found bool)

Max returns the entry with the largest key in the map.

Complexity: O(log n)

func (*Map[K, V]) Min

func (m *Map[K, V]) Min() (key K, value V, found bool)

Min returns the entry with the smallest key in the map.

Complexity: O(log n)

func (*Map[K, V]) Range

func (m *Map[K, V]) Range(min K, f func(K, V) bool)

Range calls f for each entry of the map for each key greater or equal to the min key passed as first argument. The keys and values are presented in ascending order according to the comparison function installed on the map.

Complexity: O(log n) + O(k) with k being the number of calls to f

func (*Map[K, V]) Search

func (m *Map[K, V]) Search(key K) (matchKey K, matchValue V, found bool)

Search returns the entry found in the map where the key was less or equal to the one passed as argument.

Complexity: O(log n)

type Tree

type Tree[E any] struct {
	// contains filtered or unexported fields
}

Tree is a balanced binary tree containing elements of type E.

func New

func New[E any](cmp func(E, E) int) *Tree[E]

New constructs a new tree using the comparison function passed as argument to order the elements.

func (*Tree[E]) Contains

func (t *Tree[E]) Contains(elem E) (found bool)

Contains returns true if the given element exists in the tree.

Complexity: O(log n)

func (*Tree[E]) Delete

func (t *Tree[E]) Delete(elem E) (deleted bool)

Delete removes an element from the tree.

Complexity: O(log n)

func (*Tree[E]) Init

func (t *Tree[E]) Init(cmp func(E, E) int)

Init initializes the tree with the given comparison function to order the elements.

Complexity: O(1)

func (*Tree[E]) Insert

func (t *Tree[E]) Insert(elem E) (replaced bool)

Insert inserts a new element in the tree. The method panics if the tree had not been initialized by a call to New or Init.

Complexity: O(log n)

func (*Tree[E]) Len

func (t *Tree[E]) Len() int

Len returns the number of elements in the tree.

Complexity: O(1)

func (*Tree[E]) Max added in v2.5.0

func (t *Tree[E]) Max() (max E, found bool)

Max returns the largest element in the tree.

Complexity: O(log n)

func (*Tree[E]) Min added in v2.5.0

func (t *Tree[E]) Min() (min E, found bool)

Min returns the smallest element in the tree.

Complexity: O(log n)

func (*Tree[E]) Range

func (t *Tree[E]) Range(min E, f func(E) bool)

Range calls f for each element in the tree, in the order defined by the comprison function. If f returns false, the iteration is stopped.

Complexity: O(n)

func (*Tree[E]) Search

func (t *Tree[E]) Search(elem E) (match E, found bool)

Search returns the largest element less or equal to the one passed as argument.

Complexity: O(log n)

Jump to

Keyboard shortcuts

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