项目作者: hslam

项目描述 :
Package btree implements a B-tree.
高级语言: Go
项目地址: git://github.com/hslam/btree.git
创建时间: 2020-11-03T03:20:23Z
项目社区:https://github.com/hslam/btree

开源协议:MIT License

下载


btree

PkgGoDev
Build Status
codecov
Go Report Card
LICENSE

Package btree implements a B-tree.

Definition

According to Knuth’s definition, a B-tree of order m is a tree which satisfies the following properties:

  • Every node has at most m children.
  • Every non-leaf node (except root) has at least ⌈m/2⌉ child nodes.
  • 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 in the same level and carry no information.

Benchmark

insertdelete

searchiterate

Get started

Install

  1. go get github.com/hslam/btree

Import

  1. import "github.com/hslam/btree"

Usage

Example

  1. package main
  2. import (
  3. "fmt"
  4. "github.com/hslam/btree"
  5. )
  6. func main() {
  7. tree := btree.New(2)
  8. str := String("Hello World")
  9. tree.Insert(str)
  10. fmt.Println(tree.Search(str))
  11. tree.Delete(str)
  12. }
  13. type String string
  14. func (a String) Less(b btree.Item) bool {
  15. return a < b.(String)
  16. }

Output

  1. Hello World

Iterator Example

  1. package main
  2. import (
  3. "fmt"
  4. "github.com/hslam/btree"
  5. )
  6. func main() {
  7. tree := btree.New(2)
  8. for i := 0; i < 10; i++ {
  9. tree.Insert(Int(i))
  10. }
  11. iter := tree.Min().MinIterator()
  12. for iter != nil {
  13. fmt.Printf("%d\t", iter.Item())
  14. iter = iter.Next()
  15. }
  16. }
  17. type Int int
  18. func (a Int) Less(b btree.Item) bool {
  19. return a < b.(Int)
  20. }

B-Tree

btree

Output

  1. 0 1 2 3 4 5 6 7 8 9

License

This package is licensed under a MIT license (Copyright (c) 2020 Meng Huang)

Author

btree was written by Meng Huang.