m阶b树是一棵树,它满足以下性质:—每个节点最多有m个子节点。-每个非叶节点(根节点除外)至少有⌈m/2⌉子节点。—如果根节点不是叶节点,则根节点至少有两个子节点。—具有k个子节点的非叶节点包含k−1个键。-所有的叶子出现在同一水平

type Entry

entry表示节点中包含的键值对

  1. type Entry struct {
  2. Key interface{}
  3. Value interface{}
  4. }
  5. func (entry *Entry) String() string

type Node

  1. type Node struct {
  2. Parent *Node
  3. Entries []*Entry // Contained keys in node
  4. Children []*Node // Children nodes
  5. }

type Iterator

  1. func (iterator *Iterator) Begin() // 将迭代器重置为初始状态,然后调用Next获取第一个元素
  2. func (iterator *Iterator) End() // 将迭代器移过最后一个元素,然后调用Prev获取最后一个元素
  3. // 将迭代器移动到第一个元素,如果容器中有第一个元素则返回true。
  4. // 如果First()返回true,则可以通过index()和value()检索第一个元素的索引和值。修改迭代器的状态
  5. func (iterator *Iterator) First() bool
  6. // 将迭代器移动到最后元素,如果容器中有第一个元素则返回true。
  7. // 如果Last()返回true,则可以通过index()和value()检索第一个元素的索引和值。修改迭代器的状态
  8. func (iterator *Iterator) Last() bool
  9. func (iterator *Iterator) Next() bool
  10. func (iterator *Iterator) Prev() bool
  11. func (iterator *Iterator) Index() int
  12. func (iterator *Iterator) Value() interface{}

type Tree

  1. type Tree struct {
  2. Root *Node // Root node
  3. Comparator utils.Comparator // Key comparator
  4. // contains filtered or unexported fields
  5. }
  6. func NewWith(order int, comparator utils.Comparator) *Tree
  7. // 实例化一个具有IntComparator的树,即键是int类型的
  8. func NewWithIntComparator(order int) *Tree
  9. // 用StringComparator实例化一个树,即键的类型是string
  10. func NewWithStringComparator(order int) *Tree
  11. func (tree *Tree) Clear()
  12. func (tree *Tree) Empty() bool
  13. func (tree *Tree) FromJSON(data []byte) error
  14. func (tree *Tree) Get(key interface{}) (value interface{}, found bool)
  15. func (tree *Tree) Height() int
  16. func (tree *Tree) Iterator() Iterator
  17. func (tree *Tree) Keys() []interface{}
  18. func (tree *Tree) Left() *Node
  19. func (tree *Tree) LeftKey() interface{}
  20. func (tree *Tree) LeftValue() interface{}
  21. func (tree *Tree) Put(key interface{}, value interface{})
  22. func (tree *Tree) Remove(key interface{})
  23. func (tree *Tree) Right() *Node
  24. func (tree *Tree) RightKey() interface{}
  25. func (tree *Tree) RightValue() interface{}
  26. func (tree *Tree) Size() int
  27. func (tree *Tree) String() string
  28. func (tree *Tree) ToJSON() ([]byte, error)
  29. func (tree *Tree) Values() []interface{}

例子:

  1. package main
  2. import (
  3. "fmt"
  4. "github.com/emirpasic/gods/trees/btree"
  5. )
  6. // BTreeExample to demonstrate basic usage of BTree
  7. func main() {
  8. tree := btree.NewWithIntComparator(3) // empty (keys are of type int)
  9. tree.Put(1, "x") // 1->x
  10. tree.Put(2, "b") // 1->x, 2->b (in order)
  11. tree.Put(1, "a") // 1->a, 2->b (in order, replacement)
  12. tree.Put(3, "c") // 1->a, 2->b, 3->c (in order)
  13. tree.Put(4, "d") // 1->a, 2->b, 3->c, 4->d (in order)
  14. tree.Put(5, "e") // 1->a, 2->b, 3->c, 4->d, 5->e (in order)
  15. tree.Put(6, "f") // 1->a, 2->b, 3->c, 4->d, 5->e, 6->f (in order)
  16. tree.Put(7, "g") // 1->a, 2->b, 3->c, 4->d, 5->e, 6->f, 7->g (in order)
  17. fmt.Println(tree)
  18. // BTree
  19. // 1
  20. // 2
  21. // 3
  22. // 4
  23. // 5
  24. // 6
  25. // 7
  26. _ = tree.Values() // []interface {}{"a", "b", "c", "d", "e", "f", "g"} (in order)
  27. _ = tree.Keys() // []interface {}{1, 2, 3, 4, 5, 6, 7} (in order)
  28. tree.Remove(2) // 1->a, 3->c, 4->d, 5->e, 6->f (in order)
  29. fmt.Println(tree)
  30. // BTree
  31. // 1
  32. // 3
  33. // 4
  34. // 5
  35. // 6
  36. tree.Clear() // empty
  37. tree.Empty() // true
  38. tree.Size() // 0
  39. // Other:
  40. tree.Height() // gets the height of the tree
  41. tree.Left() // gets the left-most (min) node
  42. tree.LeftKey() // get the left-most (min) node's key
  43. tree.LeftValue() // get the left-most (min) node's value
  44. tree.Right() // get the right-most (max) node
  45. tree.RightKey() // get the right-most (max) node's key
  46. tree.RightValue() // get the right-most (max) node's value
  47. }