Skip to content

Files

Latest commit

9d4c79e · Oct 6, 2022

History

History

btree

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
Oct 6, 2022
Mar 27, 2022
Mar 27, 2022

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[int, string](g.Less[int])

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

	tree.Each(func(key int, val string) {
		fmt.Println(key, val)
	})

}

Output

-10 bar
0 baz
42 foo

Index

type Tree

Tree implements a B-tree.

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

func New

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

New returns an empty B-tree.

func (*Tree[K, V]) Each

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

Each calls 'fn' on every node in the tree in order.

func (*Tree[K, V]) Get

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

Get returns the value associated with 'key'.

func (*Tree[K, V]) Put

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

Put associates 'key' with 'val'.

func (*Tree[K, V]) Remove

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

Remove removes the value associated with 'key'.

func (*Tree[K, V]) Size

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

Size returns the number of elements in the tree.

Generated by gomarkdoc