btree

package
v0.9.0 Latest Latest
Warning

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

Go to latest
Published: Jun 13, 2025 License: MIT Imports: 8 Imported by: 0

Documentation

Overview

Package btree implements a B-tree for ordered key-value storage.

A B-tree is a self-balancing tree data structure that maintains sorted data and allows searches, sequential access, insertions, and deletions in logarithmic time.

Properties of a B-Tree of order m: - Every node has at most m children. - Every non-leaf node (except root) has at least ⌈m/2⌉ children. - The root has at least two children if it is not a leaf node. - A non-leaf node with k children contains k−1 keys. - All leaves appear at the same level.

This implementation is not thread-safe.

References: https://en.wikipedia.org/wiki/B-tree

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Node

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

Node is a single element within the tree, containing entries and children.

func (*Node[K, V]) Children

func (n *Node[K, V]) Children() []*Node[K, V]

Children returns the children nodes of the node.

func (*Node[K, V]) Entries

func (n *Node[K, V]) Entries() []*entry[K, V]

Entries returns the key-value pairs of the node.

func (*Node[K, V]) Parent

func (n *Node[K, V]) Parent() *Node[K, V]

Parent returns the parent of the node.

func (*Node[K, V]) Size

func (n *Node[K, V]) Size() int

Size returns the number of nodes in the subtree rooted at this node. Time complexity: O(n).

func (*Node[K, V]) String added in v0.7.0

func (n *Node[K, V]) String() string

String returns a string representation of the node.

type Tree

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

Tree holds the elements and configuration of the B-tree.

func New

func New[K cmp.Ordered, V any](order int) *Tree[K, V]

New creates a new B-tree with the specified order and a built-in comparator. The order `m` must be 3 or greater. Panics if order is invalid. K must be an ordered type (e.g., int, string). Time complexity: O(1).

func NewWith

func NewWith[K comparable, V any](order int, cmp cmp.Comparator[K]) *Tree[K, V]

NewWith creates a new B-tree with a custom comparator. The order `m` must be 3 or greater. Panics if order is invalid. Time complexity: O(1).

func (*Tree[K, V]) Begin added in v0.6.1

func (t *Tree[K, V]) Begin() (k K, v V, ok bool)

Begin returns the minimum key-value pair. Time complexity: O(log n).

func (*Tree[K, V]) Clear

func (t *Tree[K, V]) Clear()

Clear removes all items from the tree. Time complexity: O(1).

func (*Tree[K, V]) Clone added in v0.5.0

func (t *Tree[K, V]) Clone() container.Map[K, V]

Clone creates a deep copy of the tree. Time complexity: O(n).

func (*Tree[K, V]) Comparator

func (t *Tree[K, V]) Comparator() cmp.Comparator[K]

Comparator returns the comparator used by the tree.

func (*Tree[K, V]) Delete added in v0.5.0

func (t *Tree[K, V]) Delete(key K) (v V, found bool)

Delete removes a key-value pair from the tree. Returns the value and true if the key was found and removed, false otherwise. Time complexity: O(log n).

func (*Tree[K, V]) DeleteBegin added in v0.6.1

func (t *Tree[K, V]) DeleteBegin() (k K, v V, ok bool)

DeleteBegin removes the minimum key-value pair. Returns the removed pair and true, or zero values and false if the tree is empty. Time complexity: O(log n).

func (*Tree[K, V]) DeleteEnd added in v0.6.1

func (t *Tree[K, V]) DeleteEnd() (k K, v V, ok bool)

DeleteEnd removes the maximum key-value pair. Returns the removed pair and true, or zero values and false if the tree is empty. Time complexity: O(log n).

func (*Tree[K, V]) End added in v0.6.1

func (t *Tree[K, V]) End() (k K, v V, ok bool)

End returns the maximum key-value pair. Time complexity: O(log n).

func (*Tree[K, V]) Entries added in v0.6.1

func (t *Tree[K, V]) Entries() ([]K, []V)

Entries returns slices of all keys and values in sorted order. Time complexity: O(n).

func (*Tree[K, V]) Get

func (t *Tree[K, V]) Get(key K) (V, bool)

Get retrieves the value for a given key. Returns the value and true if found, or the zero value and false otherwise. Time complexity: O(log n).

func (*Tree[K, V]) GetBeginNode added in v0.6.1

func (tree *Tree[K, V]) GetBeginNode() *Node[K, V]

GetBeginNode returns the node with the minimum key. Returns nil if the tree is empty. Time complexity: O(log n).

func (*Tree[K, V]) GetEndNode added in v0.6.1

func (tree *Tree[K, V]) GetEndNode() *Node[K, V]

GetEndNode returns the node with the maximum key. Returns nil if the tree is empty. Time complexity: O(log n).

func (*Tree[K, V]) GetNode

func (t *Tree[K, V]) GetNode(key K) *Node[K, V]

GetNode retrieves the node containing the given key. Returns the node if found, nil otherwise. Time complexity: O(log n).

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

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

Has checks if a key exists in the tree. Time complexity: O(log n).

func (*Tree[K, V]) Height

func (t *Tree[K, V]) Height() int

Height returns the height of the tree. A tree with a single node has a height of 1. Returns 0 if the tree is empty. Time complexity: O(log n).

func (*Tree[K, V]) IsEmpty added in v0.6.1

func (t *Tree[K, V]) IsEmpty() bool

IsEmpty returns true if the tree has no items. Time complexity: O(1).

func (*Tree[K, V]) Iter added in v0.6.0

func (t *Tree[K, V]) Iter() iter.Seq2[K, V]

Iter returns an iterator for in-order traversal.

func (*Tree[K, V]) Keys

func (t *Tree[K, V]) Keys() []K

Keys returns a slice of all keys in sorted order. Time complexity: O(n).

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

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

Len returns the number of items in the tree. Time complexity: O(1).

func (*Tree[K, V]) MarshalJSON

func (t *Tree[K, V]) MarshalJSON() ([]byte, error)

MarshalJSON implements the json.Marshaler interface. Time complexity: O(n).

func (*Tree[K, V]) MaxChildren added in v0.6.0

func (t *Tree[K, V]) MaxChildren() int

MaxChildren returns the maximum number of children allowed in a node.

func (*Tree[K, V]) Put

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

Put inserts a key-value pair into the tree, updating the value if the key already exists. Time complexity: O(log n).

func (*Tree[K, V]) RIter added in v0.6.1

func (t *Tree[K, V]) RIter() iter.Seq2[K, V]

RIter returns an iterator for reverse-order traversal.

func (*Tree[K, V]) Root

func (t *Tree[K, V]) Root() *Node[K, V]

Root returns the root node of the tree.

func (*Tree[K, V]) String

func (t *Tree[K, V]) String() string

String returns a string representation of the tree for debugging.

func (*Tree[K, V]) ToSlice added in v0.6.1

func (t *Tree[K, V]) ToSlice() []V

ToSlice returns a slice of all key-value pairs in sorted order. Time complexity: O(n).

func (*Tree[K, V]) UnmarshalJSON

func (t *Tree[K, V]) UnmarshalJSON(data []byte) error

UnmarshalJSON implements the json.Unmarshaler interface. Time complexity: O(m log m).

func (*Tree[K, V]) Values

func (t *Tree[K, V]) Values() []V

Values returns a slice of all values in sorted key order. Time complexity: O(n).

Jump to

Keyboard shortcuts

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