Skip to content

Latest commit

 

History

History

btree

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 

btree

import "github.com/zyedidia/generic/btree"

Package btree provides an implementation of a B-tree. A B-tree is a logarithmic search tree that maintains key-value pairs in sorted order. It is not binary because it stores more than 2 data entries per node. The branching factor for this tree is 64.

Example

package main

import (
	"fmt"
	g "github.com/zyedidia/generic"
	"github.com/zyedidia/generic/btree"
)

func main() {
	tree := btree.New[g.Int, g.String]()

	tree.Put(42, "foo")
	tree.Put(-10, "bar")
	tree.Put(0, "baz")

	tree.Iter().For(func(kv btree.KV[g.Int, g.String]) {
		fmt.Println(kv.Key, kv.Val)
	})

}

Output

-10 bar
0 baz
42 foo

Index

type KV

type KV[K g.Lesser[K], V any] struct {
    Key K
    Val V
}

type Tree

Tree implements a B-tree.

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

func New

func New[K g.Lesser[K], V any]() *Tree[K, V]

New returns an empty B-tree.

func (*BADRECV) Get

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

Get returns the value associated with 'key'.

func (*BADRECV) GetZ

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

GetZ is the same as Get but returns the zero value when nothing is found.

func (*BADRECV) Iter

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

Iter returns an iterator over all key-value pairs that iterates in sorted order from smallest to largest.

func (*BADRECV) Put

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

Put associates 'key' with 'val'.

func (*BADRECV) Size

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

Size returns the number of elements in the tree.

Generated by gomarkdoc