btree

package module
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Aug 26, 2025 License: MIT Imports: 3 Imported by: 0

README

Btree

codecov license

An in-memory generic B-tree data structure in Go.

Installation

go get github.com/dxtym/btree

Features

  • Insert: Add a key-value item
  • Search: Find a value by key
  • Remove: Delete an item by key
  • Traverse: Get all items in order

Example

b, err := btree.New[int, string](3)
if err != nil {
    panic(err)
}

for range 100 {
    key := rand.Intn(100)
    b.Insert(key, strconv.Itoa(key))
}

if value, err := b.Search(42); err != nil {
    panic(err)
}

if err := b.Remove(42); err != nil {
    panic(err)
}

Plans

  • Make concurrent safe
  • Add benchmarks tests
  • Optimize memory usage

License

MIT License. See LICENSE for details.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrKeyNotFound  = errors.New("key not found")
	ErrInvalidOrder = errors.New("order must be at least 3")
)

Functions

This section is empty.

Types

type Btree

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

func New

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

New initializes a new B-tree with the given order.

func (*Btree[K, V]) Insert

func (b *Btree[K, V]) Insert(key K, value V)

Insert adds a new key-value pair to the tree.

func (*Btree[K, V]) Iter

func (b *Btree[K, V]) Iter() <-chan *item[K, V]

Iter returns a channel to traverse the tree.

func (*Btree[K, V]) Remove

func (b *Btree[K, V]) Remove(key K) error

Remove deletes the key from the tree.

func (*Btree[K, V]) Search

func (b *Btree[K, V]) Search(key K) (V, error)

Search finds the value of the given key.

Jump to

Keyboard shortcuts

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