项目作者: MauriceGit

项目描述 :
An implementation of a balanced 2,3-tree that allows accessing next/previous elements in O(1) at all times.
高级语言: Go
项目地址: git://github.com/MauriceGit/tree23.git
创建时间: 2018-05-26T17:23:41Z
项目社区:https://github.com/MauriceGit/tree23

开源协议:MIT License

下载


Go Report Card

2,3-Tree

A Go library that implements a completely balanced 2,3-Tree that allows generic data as values in the tree.
A 2,3-Tree self-balances itself when inserting or deleting elements and it is guaranteed, that all leaf nodes will
be at a level at all times. The balancing itself runs in O(1) while ascending the tree.
All nodes will be positioned at the leaf-level. Inserting/Deleting/Searching all take O(log n) time with the logarithm between base 2 and 3.

Additionally, all leaf nodes are linked to another in a sorted order (and circularly). This additionally allows accessing
previous or next items (when we already have a leaf node) in O(1) without the need to further traverse the tree.
An iterator is simply achieved by retrieving the smallest child (there is a function for that) and following the .Next() links.

Further, the tree allows accessing elements close to a given value (without knowing the exact leaf value!).

Documentation:

For detailed functionality and API, please have a look at the generated Go Docs: https://godoc.org/github.com/MauriceGit/tree23.

A fully functional example of creating a tree, inserting/deleting/searching:

  1. import (
  2. "github.com/MauriceGit/tree23"
  3. )
  4. type Element struct {
  5. E int
  6. }
  7. func (e Element) Equal(e2 TreeElement) bool {
  8. return e.E == e2.(Element).E
  9. }
  10. func (e Element) ExtractValue() float64 {
  11. return float64(e.E)
  12. }
  13. func main() {
  14. tree := New()
  15. for i := 0; i < 100000; i++ {
  16. tree.Insert(Element{i})
  17. }
  18. // e1 will be the leaf node: 14
  19. e1, err := tree.FindFirstLargerLeaf(13.000001)
  20. // e2 will be the leaf node: 7
  21. e2, err := tree.Find(Element{7})
  22. // e3 will be the leaf node: 8
  23. e3, err := tree.Next(e2)
  24. // e4 will be the leaf node: 6
  25. e4, err := tree.Previous(e2)
  26. // smallest will be the leaf node: 0
  27. smallest, err := tree.GetSmallestLeaf()
  28. // largest will be the leaf node: 99999
  29. largest, err := tree.GetLargestLeaf()
  30. for i := 0; i < 100000; i++ {
  31. tree.Delete(Element{i})
  32. }
  33. // tree is now empty.
  34. ...
  35. }