btree

package
v0.0.0-...-9cb1c43 Latest Latest
Warning

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

Go to latest
Published: Sep 17, 2025 License: Unlicense Imports: 8 Imported by: 0

Documentation

Overview

Package btree implements an in-memory B-Tree.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type BTree

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

BTree is a generic in-memory B-Tree that can hold arbitrary keys and values.

func New

func New[K, V any](cmp func(K, K) int) *BTree[K, V]

New creates a new B-tree with the given comparison function, with the default branching factor tee. cmp(a, b) should return a negative number when a<b, a positive number when a>b and zero when a==b.

func NewWithTee

func NewWithTee[K, V any](cmp func(K, K) int, tee int) *BTree[K, V]

NewWithTee is like New, but accepts a custom branching factor tee.

func (*BTree[K, V]) All

func (bt *BTree[K, V]) All() iter.Seq2[K, V]

All returns an iterator over all key-value pairs in the tree, in order (sorted by key).

func (*BTree[K, V]) Delete

func (bt *BTree[K, V]) Delete(key K)

Delete deletes a key and its associated value from the tree. If key is not found in the tree, Delete is a no-op.

func (*BTree[K, V]) Get

func (bt *BTree[K, V]) Get(key K) (v V, ok bool)

Get looks for the given key in the tree. It returns the associated value and ok=true; otherwise, it returns ok=false.

func (*BTree[K, V]) Insert

func (bt *BTree[K, V]) Insert(key K, value V)

Insert inserts a new key=value pair into the tree. If `key` already exists in the tree, its value is replaced with `value`.

func (*BTree[K, V]) Stats

func (bt *BTree[K, V]) Stats() string

Stats returns a string with statistics about this B-Tree: total number of keys, nodes, leaf nodes etc.

func (*BTree[K, V]) Successor

func (bt *BTree[K, V]) Successor(key K) (K, V, bool)

Successor finds the successor of the given key in the tree, meaning the smallest key in the tree that is larger than the given key. If the key itself isn't found, or if the key has no successor (it's the largest key in the tree), Successor returns ok=false.

Jump to

Keyboard shortcuts

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