aa

package module
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Sep 22, 2025 License: MIT Imports: 5 Imported by: 1

README

Immutable AA Trees

Go Reference Go Report Go Coverage

A persistent implementation of AA Trees (paper), including the join-based tree algorithms, and augmented with order statistics.

Documentation

Overview

Package aa implements immutable AA trees.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Equal added in v0.3.7

func Equal[K cmp.Ordered, V comparable](t1, t2 *Tree[K, V]) bool

Equal reports whether two trees contain the same key/value pairs.

func Overlap added in v0.3.7

func Overlap[K cmp.Ordered, V any](t1, t2 *Tree[K, V]) bool

Overlap reports whether t1 contains any keys from t2.

func Subset added in v0.3.7

func Subset[K cmp.Ordered, V comparable](t1, t2 *Tree[K, V]) bool

Subset reports whether t2 contains every t1 key/value pair.

Types

type Tree

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

Tree is an immutable AA tree, a form of self-balancing binary search tree.

Use *Tree as a reference type; the zero value for *Tree (nil) is the empty tree:

var empty *aa.Tree[int, string]
one := empty.Put(1, "one")
one.Has(1) ⟹ true

Note: the zero value for Tree{} is a valid — but non-empty — tree.

func Difference added in v0.3.2

func Difference[K cmp.Ordered, V any](t1, t2 *Tree[K, V]) *Tree[K, V]

Difference returns the set difference of two trees.

func Intersection added in v0.3.2

func Intersection[K cmp.Ordered, V any](t1, t2 *Tree[K, V]) *Tree[K, V]

Intersection returns the set intersection of two trees, first value wins.

func MakeMap added in v0.3.4

func MakeMap[K cmp.Ordered, V any](m map[K]V) *Tree[K, V]

MakeMap builds a tree from a key-value map.

func MakeSet added in v0.3.4

func MakeSet[K cmp.Ordered](keys ...K) *Tree[K, struct{}]

MakeSet builds a tree from a set of keys.

func SymmetricDifference added in v0.3.3

func SymmetricDifference[K cmp.Ordered, V any](t1, t2 *Tree[K, V]) *Tree[K, V]

SymmetricDifference returns the set symmetric difference of two trees.

func Union added in v0.3.2

func Union[K cmp.Ordered, V any](t1, t2 *Tree[K, V]) *Tree[K, V]

Union returns the set union of two trees, last value wins.

func (*Tree[K, V]) Add

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

Add returns a (possibly) modified tree that contains key.

tree.Add(key).Has(key) ⟹ true

func (*Tree[K, V]) Ascend added in v0.3.0

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

Ascend returns an ascending iterator for this tree.

func (*Tree[K, V]) AscendCeil added in v0.3.0

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

AscendCeil returns an ascending iterator for this tree, starting at the least key in this tree greater-than or equal-to pivot.

func (*Tree[K, V]) AscendFloor added in v0.3.0

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

AscendFloor returns an ascending iterator for this tree, starting at the greatest key in this tree less-than or equal-to pivot.

func (*Tree[K, V]) Ceil added in v0.2.0

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

Ceil finds the least key in this tree greater-than or equal-to key, and returns the node for that key, or nil if no such key exists in this tree.

func (*Tree[K, V]) Collect added in v0.3.4

func (tree *Tree[K, V]) Collect() map[K]V

Collect collects key-value pairs from this tree into a new map and returns it.

func (*Tree[K, V]) Delete

func (tree *Tree[K, V]) Delete(key K, pred ...func(node *Tree[K, V]) bool) *Tree[K, V]

Delete returns a (possibly) modified tree with key removed from it. The optional pred is called to confirm deletion.

tree.Delete(key).Has(key) ⟹ false

func (*Tree[K, V]) DeleteMax added in v0.3.0

func (tree *Tree[K, V]) DeleteMax() (_, node *Tree[K, V])

DeleteMax returns a modified tree with its greatest key removed from it, and the removed node.

func (*Tree[K, V]) DeleteMin added in v0.3.0

func (tree *Tree[K, V]) DeleteMin() (_, node *Tree[K, V])

DeleteMin returns a modified tree with its least key removed from it, and the removed node.

func (*Tree[K, V]) Descend added in v0.3.0

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

Descend returns a descending iterator for this tree.

func (*Tree[K, V]) DescendCeil added in v0.3.0

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

DescendCeil returns a descending iterator for this tree, starting at the least key in this tree greater-than or equal-to pivot.

func (*Tree[K, V]) DescendFloor added in v0.3.0

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

DescendFloor returns a descending iterator for this tree, starting at the greatest key in this tree less-than or equal-to pivot.

func (*Tree[K, V]) Filter added in v0.3.3

func (tree *Tree[K, V]) Filter(pred func(node *Tree[K, V]) bool) *Tree[K, V]

Filter returns a tree of nodes for which pred returns true.

func (*Tree[K, V]) Floor added in v0.2.0

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

Floor finds the greatest key in this tree less-than or equal-to key, and returns the node for that key, or nil if no such key exists in this tree.

func (*Tree[K, V]) Get

func (tree *Tree[K, V]) Get(key K) (value V, found bool)

Get retrieves the value for a given key; found indicates whether key exists in this tree.

func (*Tree[K, V]) Has added in v0.2.0

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

Has reports whether key exists in this tree.

func (*Tree[K, V]) Key

func (tree *Tree[K, V]) Key() K

Key returns the key at the root of this tree.

Note: getting the root key of an empty tree (nil) causes a runtime panic.

func (*Tree[K, V]) Left

func (tree *Tree[K, V]) Left() *Tree[K, V]

Left returns the left subtree of this tree, containing all keys less than its root key.

Note: the left subtree of the empty tree is the empty tree (nil).

func (*Tree[K, V]) Len added in v0.3.0

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

Len returns the number of nodes in this tree.

func (*Tree[K, V]) Level

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

Level returns the level of this AA tree.

Notes:

  • the level of the empty tree (nil) is 0.
  • the height of a tree of level n is between n and 2·n.
  • the number of nodes in a tree of level n is between 2ⁿ-1 and 3ⁿ-1.

func (*Tree[K, V]) Max added in v0.3.0

func (tree *Tree[K, V]) Max() *Tree[K, V]

Max finds the greatest key in this tree, and returns the node for that key, or nil if this tree is empty.

func (*Tree[K, V]) Min added in v0.3.0

func (tree *Tree[K, V]) Min() *Tree[K, V]

Min finds the least key in this tree, and returns the node for that key, or nil if this tree is empty.

func (*Tree[K, V]) Partition added in v0.3.3

func (tree *Tree[K, V]) Partition(pred func(node *Tree[K, V]) bool) (t, f *Tree[K, V])

Partition returns a tree of nodes for which pred returns true, and a tree of nodes for which it returns false.

func (*Tree[K, V]) Patch

func (tree *Tree[K, V]) Patch(key K, update func(node *Tree[K, V]) (value V, ok bool)) *Tree[K, V]

Patch finds key in this tree, calls update with the node for that key (or nil, if key is not found), and returns a (possibly) modified tree.

The update callback can opt to set/update the value for the key, by returning (value, true), or not, by returning false.

func (*Tree[K, V]) Put

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

Put returns a modified tree with key set to value.

tree.Put(key, value).Get(key) ⟹ (value, true)

func (*Tree[K, V]) Rank added in v0.3.6

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

Rank finds the rank of key, the number of nodes in this tree less than key.

tree.Rank(tree.Select(i).Key()) ⟹ i, iff 0 ≤ i < tree.Len()

func (*Tree[K, V]) Right

func (tree *Tree[K, V]) Right() *Tree[K, V]

Right returns the right subtree of this tree, containing all keys greater than its root key.

Note: the right subtree of the empty tree is the empty tree (nil).

func (*Tree[K, V]) Select added in v0.3.6

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

Select finds the node at index i of this tree and returns it (or nil if i is out of range).

func (*Tree[K, V]) Split added in v0.3.2

func (tree *Tree[K, V]) Split(key K) (left, node, right *Tree[K, V])

Split partitions this tree around a key. It returns a left tree with keys less than key, a right tree with keys greater than key, and the node for the key (or nil if no such key exists in this tree).

func (*Tree[K, V]) Value

func (tree *Tree[K, V]) Value() V

Value returns the value at the root of this tree.

Note: getting the root value of an empty tree (nil) causes a runtime panic.

Jump to

Keyboard shortcuts

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