Binary Search Tree implemented with concurrency safe on Golang.
- Concurrency safe
- Serialization
- Persistent storage
type BTree struct {
root *Node
lock sync.RWMutex
size int
}
type Node struct {
Key int
Value interface{}
Left *Node
Right *Node
}
package main
import (
"strconv"
"github.com/xfrr/btree"
)
func main() {
// Create tree
var tree = &BTree{}
...
}
Insert key-value.
for i := 1; i < 1000; i++ {
v := strconv.Itoa(i)
tree.Put(i, v)
}
Search and return the node searching by key.
node := tree.Find(132)
Delete and return the node searching by key.
node := tree.Remove(132)
Serialize tree and save it to disk.
err := tree.Commit()
Load tree from disk.
err := tree.Load()
Returns the node with max key value.
max := tree.Max()
Returns the node with min key value.
min := tree.Min()
Iterate over all nodes in order
tree.TraverseInOrder(tree.root, func(n *Node) {
... print node
})
- Put
- Find
- Remove
- Max
- Min
- Size
- ...
Command to execute
go test -v