Help us shape the future of Go! Take the 2026 Go Developer Survey

wbt

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Jan 6, 2026 License: MIT Imports: 3 Imported by: 3

README

Immutable Weight-Balanced Trees

Go Reference Go Report Go Coverage

A persistent implementation of weight-balanced trees, including the join-based tree algorithms and order statistics.

Documentation

Overview

Package wbt implements immutable weight-balanced trees.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Equal

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

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

Overlap reports whether t1 contains any keys from t2.

func Subset

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 weight-balanced 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 *wbt.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

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

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

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

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

MakeSet builds a tree from a set of keys.

func SymmetricDifference

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

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

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

Ascend returns an ascending iterator for this tree.

func (*Tree[K, V]) AscendCeil

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

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

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

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

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

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

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

Descend returns a descending iterator for this tree.

func (*Tree[K, V]) DescendCeil

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

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

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

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

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

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

Len returns the number of nodes in this tree.

func (*Tree[K, V]) Max

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

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

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

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

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

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