btree

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Mar 27, 2026 License: Apache-2.0 Imports: 6 Imported by: 0

README

BTree

Good performed algorithm in case of searching data inside. Thead-unsafe implementation.

Documentation

Overview

Package btree provides a B-tree for ordered key-value storage with configurable order.

NOT safe for concurrent use. Callers must synchronize access externally.

Index

Examples

Constants

View Source
const (
	MinOrder = 3
)

MinOrder is the minimum allowed order (maximum number of children) for a B-tree.

Variables

View Source
var (
	ErrInvalidOrder = errors.New("invalid order, should be at least 3")
)

All kind of errors

Functions

This section is empty.

Types

type Entry

type Entry[K comparable, V any] struct {
	Key   K
	Value V
}

Entry represents the key-value pair contained within nodes

func (*Entry[K, V]) String

func (entry *Entry[K, V]) String() string

type Iterator

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

Iterator holding the iterator's state

func (*Iterator[K, V]) Begin

func (iterator *Iterator[K, V]) Begin()

Begin resets the iterator to its initial state (one-before-first) Call Next() to fetch the first element if any.

func (*Iterator[K, V]) End

func (iterator *Iterator[K, V]) End()

End moves the iterator past the last element (one-past-the-end). Call Prev() to fetch the last element if any.

func (*Iterator[K, V]) First

func (iterator *Iterator[K, V]) First() bool

First moves the iterator to the first element and returns true if there was a first element in the container. If First() returns true, then first element's key and value can be retrieved by Key() and Value(). Modifies the state of the iterator

func (*Iterator[K, V]) Key

func (iterator *Iterator[K, V]) Key() K

Key returns the current element's key. Does not modify the state of the iterator.

func (*Iterator[K, V]) Last

func (iterator *Iterator[K, V]) Last() bool

Last moves the iterator to the last element and returns true if there was a last element in the container. If Last() returns true, then last element's key and value can be retrieved by Key() and Value(). Modifies the state of the iterator.

func (*Iterator[K, V]) Next

func (iterator *Iterator[K, V]) Next() bool

Next moves the iterator to the next element and returns true if there was a next element in the container. If Next() returns true, then next element's key and value can be retrieved by Key() and Value(). If Next() was called for the first time, then it will point the iterator to the first element if it exists. Modifies the state of the iterator.

func (*Iterator[K, V]) Prev

func (iterator *Iterator[K, V]) Prev() bool

Prev moves the iterator to the previous element and returns true if there was a previous element in the container. If Prev() returns true, then previous element's key and value can be retrieved by Key() and Value(). Modifies the state of the iterator.

func (*Iterator[K, V]) Value

func (iterator *Iterator[K, V]) Value() any

Value returns the current element's value. Does not modify the state of the iterator.

type Node

type Node[K comparable, V any] struct {
	Parent   *Node[K, V]
	Entries  []*Entry[K, V] // Contained keys in node
	Children []*Node[K, V]  // Children nodes
}

Node is a single element within the tree

type Tree

type Tree[K comparable, V any] struct {
	Root       *Node[K, V]           // Root node
	Comparator comparator.Comparator // Key comparator
	// contains filtered or unexported fields
}

Tree holds elements of the B-tree.

func NewWith

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

NewWith instantiates a B-tree with the order (maximum number of children) and a custom key comparator.

func NewWithIntComparator

func NewWithIntComparator(order int) (*Tree[int, any], error)

NewWithIntComparator instantiates a B-tree with the order (maximum number of children) and the IntComparator, i.e. keys are of type int.

Example
package main

import (
	"fmt"

	"github.com/FrogoAI/memory/btree"
)

func main() {
	tree, err := btree.NewWithIntComparator(3)
	if err != nil {
		panic(err)
	}

	_ = tree.Put(1, "one")
	_ = tree.Put(2, "two")
	_ = tree.Put(3, "three")

	fmt.Println(tree.Len())
}
Output:
3

func NewWithStringComparator

func NewWithStringComparator(order int) (*Tree[string, any], error)

NewWithStringComparator instantiates a B-tree with the order (maximum number of children) and the StringComparator, i.e. keys are of type string.

Example
package main

import (
	"fmt"

	"github.com/FrogoAI/memory/btree"
)

func main() {
	tree, err := btree.NewWithStringComparator(3)
	if err != nil {
		panic(err)
	}

	_ = tree.Put("b", "banana")
	_ = tree.Put("a", "apple")
	_ = tree.Put("c", "cherry")

	fmt.Println(tree.Keys())
}
Output:
[a b c]

func (*Tree[K, V]) Clear

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

Clear removes all nodes from the tree.

func (*Tree[K, V]) Copy

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

Copy returns a deep copy of the tree. The new tree shares the same comparator but has independent nodes. Keys and values are shallow-copied.

func (*Tree[K, V]) Empty

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

Empty returns true if tree does not contain any nodes.

func (*Tree[K, V]) Get

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

Get searches the node in the tree by key and returns its value or the zero value if not found. Second return parameter is true if key was found, otherwise false. Returns an error if the key type does not match the comparator.

Example
package main

import (
	"fmt"

	"github.com/FrogoAI/memory/btree"
)

func main() {
	tree, _ := btree.NewWithIntComparator(3)

	_ = tree.Put(1, "one")

	value, found, _ := tree.Get(1)
	fmt.Println(value, found)

	value, found, _ = tree.Get(99)
	fmt.Println(value, found)
}
Output:
one true
<nil> false

func (*Tree[K, V]) Height

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

Height returns the height of the tree.

func (*Tree[K, V]) Iterator

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

Iterator returns a stateful iterator whose elements are key/value pairs.

Example
package main

import (
	"fmt"

	"github.com/FrogoAI/memory/btree"
)

func main() {
	tree, _ := btree.NewWithIntComparator(3)

	_ = tree.Put(3, "three")
	_ = tree.Put(1, "one")
	_ = tree.Put(2, "two")

	it := tree.Iterator()
	for it.Next() {
		fmt.Printf("%d=%v ", it.Key(), it.Value())
	}

	fmt.Println()
}
Output:
1=one 2=two 3=three

func (*Tree[K, V]) Keys

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

Keys returns all keys in-order.

func (*Tree[K, V]) Left

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

Left returns the left-most (min) node or nil if tree is empty.

func (*Tree[K, V]) LeftKey

func (tree *Tree[K, V]) LeftKey() any

LeftKey returns the left-most (min) key or nil if tree is empty.

func (*Tree[K, V]) LeftValue

func (tree *Tree[K, V]) LeftValue() any

LeftValue returns the left-most value or nil if tree is empty.

func (*Tree[K, V]) Len

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

Len returns the number of keys in the tree.

func (*Tree[K, V]) Put

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

Put inserts key-value pair node into the tree. If key already exists, then its value is updated with the new value. Returns an error if the key type does not match the comparator.

Example
package main

import (
	"fmt"

	"github.com/FrogoAI/memory/btree"
)

func main() {
	tree, _ := btree.NewWithIntComparator(3)

	_ = tree.Put(3, "three")
	_ = tree.Put(1, "one")
	_ = tree.Put(2, "two")

	fmt.Println(tree.Keys())
}
Output:
[1 2 3]

func (*Tree[K, V]) Remove

func (tree *Tree[K, V]) Remove(key K) (err error)

Remove removes the node from the tree by key. Returns an error if the key type does not match the comparator.

Example
package main

import (
	"fmt"

	"github.com/FrogoAI/memory/btree"
)

func main() {
	tree, _ := btree.NewWithIntComparator(3)

	_ = tree.Put(1, "one")
	_ = tree.Put(2, "two")
	_ = tree.Put(3, "three")

	_ = tree.Remove(2)

	fmt.Println(tree.Keys())
}
Output:
[1 3]

func (*Tree[K, V]) Right

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

Right returns the right-most (max) node or nil if tree is empty.

func (*Tree[K, V]) RightKey

func (tree *Tree[K, V]) RightKey() any

RightKey returns the right-most (max) key or nil if tree is empty.

func (*Tree[K, V]) RightValue

func (tree *Tree[K, V]) RightValue() any

RightValue returns the right-most value or nil if tree is empty.

func (*Tree[K, V]) String

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

String returns a string representation of container (for debugging purposes).

func (*Tree[K, V]) Values

func (tree *Tree[K, V]) Values() []any

Values returns all values in-order based on the key.

Jump to

Keyboard shortcuts

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