Skip to main content
Version: 2.8.x(Latest)
tip

The following is a list of commonly used methods; documentation updates may lag behind new features in the code. For more methods and examples, please refer to the code documentation: https://pkg.go.dev/github.com/gogf/gf/v2/container/gtree

NewBTree

  • Description: NewBTree creates a BTree using m (maximum number of child nodes) and a custom comparator. The safe parameter specifies whether to use a concurrency-safe tree, with a default value of false.

  • Note: The m parameter must be greater than or equal to 3, otherwise it will panic.

  • Format:

NewBTree(m int, comparator func(v1, v2 interface{}) int, safe ...bool) *BTree
  • Example:
func ExampleNewBTree() {
bTree := gtree.NewBTree(3, gutil.ComparatorString)
for i := 0; i < 6; i++ {
bTree.Set("key"+gconv.String(i), "val"+gconv.String(i))
}
fmt.Println(bTree.Map())

// Output:
// map[key0:val0 key1:val1 key2:val2 key3:val3 key4:val4 key5:val5]
}

NewBTreeFrom

  • Description: NewBTreeFrom creates a BTree using m (maximum number of child nodes), a custom comparator, and data of type map[interface{}]interface{}. The safe parameter specifies whether to use a concurrency-safe tree, with a default value of false.

  • Note: The m parameter must be greater than or equal to 3, otherwise it will panic.

  • Format:

NewBTreeFrom(m int, comparator func(v1, v2 interface{}) int, data map[interface{}]interface{}, safe ...bool) *BTree
  • Example:
func ExampleNewBTreeFrom() {
bTree := gtree.NewBTree(3, gutil.ComparatorString)
for i := 0; i < 6; i++ {
bTree.Set("key"+gconv.String(i), "val"+gconv.String(i))
}

otherBTree := gtree.NewBTreeFrom(3, gutil.ComparatorString, bTree.Map())
fmt.Println(otherBTree.Map())

// Output:
// map[key0:val0 key1:val1 key2:val2 key3:val3 key4:val4 key5:val5]
}

Clone

  • Description: Clone returns a new BTree, which is a copy of the current tree.

  • Format:

Clone() *BTree
  • Example:
func ExampleBTree_Clone() {
b := gtree.NewBTree(3, gutil.ComparatorString)
for i := 0; i < 6; i++ {
b.Set("key"+gconv.String(i), "val"+gconv.String(i))
}

tree := b.Clone()

fmt.Println(tree.Map())
fmt.Println(tree.Size())

// Output:
// map[key0:val0 key1:val1 key2:val2 key3:val3 key4:val4 key5:val5]
// 6
}

Set

  • Description: Set sets the key/value for the tree.

  • Format:

Set(key interface{}, value interface{})
  • Example:
func ExampleBTree_Set() {
tree := gtree.NewBTree(3, gutil.ComparatorString)
for i := 0; i < 6; i++ {
tree.Set("key"+gconv.String(i), "val"+gconv.String(i))
}

fmt.Println(tree.Map())
fmt.Println(tree.Size())

// Output:
// map[key0:val0 key1:val1 key2:val2 key3:val3 key4:val4 key5:val5]
// 6
}

Sets

  • Description: Sets sets key/value for the tree in batches.

  • Format:

Sets(data map[interface{}]interface{})
  • Example:
func ExampleBTree_Sets() {
tree := gtree.NewBTree(3, gutil.ComparatorString)

tree.Sets(map[interface{}]interface{}{
"key1": "val1",
"key2": "val2",
})

fmt.Println(tree.Map())
fmt.Println(tree.Size())

// Output:
// map[key1:val1 key2:val2]
// 2
}

Get

  • Description: Get returns the value value corresponding to the parameter key. If key does not exist, it returns Nil.

  • Format:

Get(key interface{}) (value interface{})
  • Example:
func ExampleBTree_Get() {
tree := gtree.NewBTree(3, gutil.ComparatorString)
for i := 0; i < 6; i++ {
tree.Set("key"+gconv.String(i), "val"+gconv.String(i))
}

fmt.Println(tree.Get("key1"))
fmt.Println(tree.Get("key10"))

// Output:
// val1
// <nil>
}

GetOrSet

  • Description: GetOrSet returns value if key exists; if key does not exist, it sets the key-value with key and value, then returns the value.

  • Format:

GetOrSet(key interface{}, value interface{}) interface{}
  • Example:
func ExampleBTree_GetOrSet() {
tree := gtree.NewBTree(3, gutil.ComparatorString)
for i := 0; i < 6; i++ {
tree.Set("key"+gconv.String(i), "val"+gconv.String(i))
}

fmt.Println(tree.GetOrSet("key1", "newVal1"))
fmt.Println(tree.GetOrSet("key6", "val6"))

// Output:
// val1
// val6
}

GetOrSetFunc

  • Description: GetOrSetFunc returns value if key exists; if key does not exist, it sets the key-value with key and the return value of func f, then returns the value.

  • Format:

GetOrSetFunc(key interface{}, f func() interface{}) interface{}
  • Example:
func ExampleBTree_GetOrSetFunc() {
tree := gtree.NewBTree(3, gutil.ComparatorString)
for i := 0; i < 6; i++ {
tree.Set("key"+gconv.String(i), "val"+gconv.String(i))
}

fmt.Println(tree.GetOrSetFunc("key1", func() interface{} {
return "newVal1"
}))
fmt.Println(tree.GetOrSetFunc("key6", func() interface{} {
return "val6"
}))

// Output:
// val1
// val6
}

GetOrSetFuncLock

  • Description: GetOrSetFunc returns value if key exists; if key does not exist, it sets the key-value with key and the return value of func f, then returns the value.

  • Note: The difference between GetOrSetFuncLock and GetOrSetFunc is that it executes the function f in a write lock.

  • Format:

GetOrSetFuncLock(key interface{}, f func() interface{}) interface{}
  • Example:
func ExampleBTree_GetOrSetFuncLock() {
tree := gtree.NewBTree(3, gutil.ComparatorString)
for i := 0; i < 6; i++ {
tree.Set("key"+gconv.String(i), "val"+gconv.String(i))
}

fmt.Println(tree.GetOrSetFuncLock("key1", func() interface{} {
return "newVal1"
}))
fmt.Println(tree.GetOrSetFuncLock("key6", func() interface{} {
return "val6"
}))

// Output:
// val1
// val6
}

GetVar

  • Description: GetVar searches and returns the key value corresponding to the key name key, the type is *gvar.Var.

  • Note: The returned gvar.Var is not concurrent safe.

  • Format:

GetVar(key interface{}) *gvar.Var
  • Example:
func ExampleBTree_GetVar() {
tree := gtree.NewBTree(3, gutil.ComparatorString)
for i := 0; i < 6; i++ {
tree.Set("key"+gconv.String(i), "val"+gconv.String(i))
}

fmt.Println(tree.GetVar("key1").String())

// Output:
// val1
}

GetVarOrSet

  • Description: GetVarOrSet returns the result of GetOrSet, the type is *gvar.Var.

  • Note: The returned gvar.Var is not concurrent safe.

  • Format:

GetVarOrSet(key interface{}, value interface{}) *gvar.Var
  • Example:
func ExampleBTree_GetVarOrSet() {
tree := gtree.NewBTree(3, gutil.ComparatorString)
for i := 0; i < 6; i++ {
tree.Set("key"+gconv.String(i), "val"+gconv.String(i))
}

fmt.Println(tree.GetVarOrSet("key1", "newVal1"))
fmt.Println(tree.GetVarOrSet("key6", "val6"))

// Output:
// val1
// val6
}

GetVarOrSetFunc

  • Description: GetVarOrSetFunc returns the result of GetOrSetFunc, the type is *gvar.Var.
  • Note: The returned gvar.Var is not concurrent safe.
  • Format:
GetVarOrSetFunc(key interface{}, f func() interface{}) *gvar.Var
  • Example:
func ExampleBTree_GetVarOrSetFunc() {
tree := gtree.NewBTree(3, gutil.ComparatorString)
for i := 0; i < 6; i++ {
tree.Set("key"+gconv.String(i), "val"+gconv.String(i))
}

fmt.Println(tree.GetVarOrSetFunc("key1", func() interface{} {
return "newVal1"
}))
fmt.Println(tree.GetVarOrSetFunc("key6", func() interface{} {
return "val6"
}))

// Output:
// val1
// val6
}

GetVarOrSetFuncLock

  • Description: GetVarOrSetFuncLock returns the result of GetOrSetFuncLock, the type is *gvar.Var.

  • Note: The returned gvar.Var is not concurrent safe.

  • Format:

GetVarOrSetFuncLock(key interface{}, f func() interface{}) *gvar.Var
  • Example:
func ExampleBTree_GetVarOrSetFuncLock() {
tree := gtree.NewBTree(3, gutil.ComparatorString)
for i := 0; i < 6; i++ {
tree.Set("key"+gconv.String(i), "val"+gconv.String(i))
}

fmt.Println(tree.GetVarOrSetFuncLock("key1", func() interface{} {
return "newVal1"
}))
fmt.Println(tree.GetVarOrSetFuncLock("key6", func() interface{} {
return "val6"
}))

// Output:
// val1
// val6
}

SetIfNotExist

  • Description: If key does not exist, SetIfNotExist sets the key-value pair key/value for the map and returns true. If key exists, it returns false, and value will be ignored.

  • Format:

SetIfNotExist(key interface{}, value interface{}) bool
  • Example:
func ExampleBTree_SetIfNotExist() {
tree := gtree.NewBTree(3, gutil.ComparatorString)
for i := 0; i < 6; i++ {
tree.Set("key"+gconv.String(i), "val"+gconv.String(i))
}

fmt.Println(tree.SetIfNotExist("key1", "newVal1"))
fmt.Println(tree.SetIfNotExist("key6", "val6"))

// Output:
// false
// true
}

SetIfNotExistFunc

  • Description: If key does not exist, SetIfNotExistFunc sets the value to the return value of the function f and returns true. If key exists, it returns false, and value will be ignored.

  • Format:

SetIfNotExistFunc(key interface{}, f func() interface{}) bool
  • Example:
func ExampleBTree_SetIfNotExistFunc() {
tree := gtree.NewBTree(3, gutil.ComparatorString)
for i := 0; i < 6; i++ {
tree.Set("key"+gconv.String(i), "val"+gconv.String(i))
}

fmt.Println(tree.SetIfNotExistFunc("key1", func() interface{} {
return "newVal1"
}))
fmt.Println(tree.SetIfNotExistFunc("key6", func() interface{} {
return "val6"
}))

// Output:
// false
// true
}

SetIfNotExistFuncLock

  • Description: If key does not exist, SetIfNotExistFunc sets the value to the return value of func c, then returns true. If key exists, it returns false, and value will be ignored.

  • The difference between SetIfNotExistFuncLock and SetIfNotExistFunc is that it executes the function f within mutex.Lock.

  • Format:

SetIfNotExistFuncLock(key interface{}, f func() interface{}) bool
  • Example:
func ExampleBTree_SetIfNotExistFuncLock() {
tree := gtree.NewBTree(3, gutil.ComparatorString)
for i := 0; i < 6; i++ {
tree.Set("key"+gconv.String(i), "val"+gconv.String(i))
}

fmt.Println(tree.SetIfNotExistFuncLock("key1", func() interface{} {
return "newVal1"
}))
fmt.Println(tree.SetIfNotExistFuncLock("key6", func() interface{} {
return "val6"
}))

// Output:
// false
// true
}

Contains

  • Description: Contains checks if key exists in the tree. If key exists, it returns true; otherwise, it returns false.

  • Format:

Contains(key interface{}) bool
  • Example:
func ExampleBTree_Contains() {
tree := gtree.NewBTree(3, gutil.ComparatorString)
for i := 0; i < 6; i++ {
tree.Set("key"+gconv.String(i), "val"+gconv.String(i))
}

fmt.Println(tree.Contains("key1"))
fmt.Println(tree.Contains("key6"))

// Output:
// true
// false
}

Remove

  • Description: Delete the value from tree according to the given key and return this deleted value.

  • Format:

Remove(key interface{}) (value interface{})
  • Example:
func ExampleBTree_Remove() {
tree := gtree.NewBTree(3, gutil.ComparatorString)
for i := 0; i < 6; i++ {
tree.Set("key"+gconv.String(i), "val"+gconv.String(i))
}

fmt.Println(tree.Remove("key1"))
fmt.Println(tree.Remove("key6"))
fmt.Println(tree.Map())

// Output:
// val1
// <nil>
// map[key0:val0 key2:val2 key3:val3 key4:val4 key5:val5]
}

Removes

  • Description: Removes deletes value of tree in batches according to the given key.

  • Format:

Removes(keys []interface{})
  • Example:
func ExampleBTree_Removes() {
tree := gtree.NewBTree(3, gutil.ComparatorString)
for i := 0; i < 6; i++ {
tree.Set("key"+gconv.String(i), "val"+gconv.String(i))
}

removeKeys := make([]interface{}, 2)
removeKeys = append(removeKeys, "key1")
removeKeys = append(removeKeys, "key6")

tree.Removes(removeKeys)

fmt.Println(tree.Map())

// Output:
// map[key0:val0 key2:val2 key3:val3 key4:val4 key5:val5]
}

IsEmpty

  • Description: IsEmpty checks if tree is empty. If tree is empty, it returns true; otherwise, it returns false.

  • Format:

IsEmpty() bool
  • Example:
func ExampleBTree_IsEmpty() {
tree := gtree.NewBTree(3, gutil.ComparatorString)

fmt.Println(tree.IsEmpty())

for i := 0; i < 6; i++ {
tree.Set("key"+gconv.String(i), "val"+gconv.String(i))
}

fmt.Println(tree.IsEmpty())

// Output:
// true
// false
}

Size

  • Description: Size returns the size of the tree.

  • Format:

Size() int
  • Example:
func ExampleBTree_Size() {
tree := gtree.NewBTree(3, gutil.ComparatorString)

fmt.Println(tree.Size())

for i := 0; i < 6; i++ {
tree.Set("key"+gconv.String(i), "val"+gconv.String(i))
}

fmt.Println(tree.Size())

// Output:
// 0
// 6
}

Keys

  • Description: Keys returns all keys in ascending order.

  • Format:

Keys() []interface{}
  • Example:
func ExampleBTree_Keys() {
tree := gtree.NewBTree(3, gutil.ComparatorString)
for i := 6; i > 0; i-- {
tree.Set("key"+gconv.String(i), "val"+gconv.String(i))
}

fmt.Println(tree.Keys())

// Output:
// [key1 key2 key3 key4 key5 key6]
}

Values

  • Description: Values returns all values in ascending order of key.

  • Format:

Values() []interface{}
  • Example:
func ExampleBTree_Values() {
tree := gtree.NewBTree(3, gutil.ComparatorString)
for i := 6; i > 0; i-- {
tree.Set("key"+gconv.String(i), "val"+gconv.String(i))
}

fmt.Println(tree.Values())

// Output:
// [val1 val2 val3 val4 val5 val6]
}

Map

  • Description: Map returns all key/value pairs in the form of a map.

  • Format:

Map() map[interface{}]interface{}
  • Example:
func ExampleBTree_Map() {
tree := gtree.NewBTree(3, gutil.ComparatorString)
for i := 0; i < 6; i++ {
tree.Set("key"+gconv.String(i), "val"+gconv.String(i))
}

fmt.Println(tree.Map())

// Output:
// map[key0:val0 key1:val1 key2:val2 key3:val3 key4:val4 key5:val5]
}

MapStrAny

  • Description: MapStrAny returns all key/value pairs in the form of map[string]interface{}.

  • Format:

MapStrAny() map[string]interface{}
  • Example:
func ExampleBTree_MapStrAny() {
tree := gtree.NewBTree(3, gutil.ComparatorString)
for i := 0; i < 6; i++ {
tree.Set(1000+i, "val"+gconv.String(i))
}

fmt.Println(tree.MapStrAny())

// Output:
// map[1000:val0 1001:val1 1002:val2 1003:val3 1004:val4 1005:val5]
}

Clear

  • Description: Clear deletes all data in the tree.

  • Format:

Clear()
  • Example:
func ExampleBTree_Clear() {
tree := gtree.NewBTree(3, gutil.ComparatorString)
for i := 0; i < 6; i++ {
tree.Set(1000+i, "val"+gconv.String(i))
}
fmt.Println(tree.Size())

tree.Clear()
fmt.Println(tree.Size())

// Output:
// 6
// 0
}

Replace

  • Description: Replace replaces the key/value in the tree with data of type map[interface{}]interface{}.

  • Format:

Replace(data map[interface{}]interface{})
  • Example:
func ExampleBTree_Replace() {
tree := gtree.NewBTree(3, gutil.ComparatorString)
for i := 0; i < 6; i++ {
tree.Set("key"+gconv.String(i), "val"+gconv.String(i))
}

fmt.Println(tree.Map())

data := map[interface{}]interface{}{
"newKey0": "newVal0",
"newKey1": "newVal1",
"newKey2": "newVal2",
}

tree.Replace(data)

fmt.Println(tree.Map())

// Output:
// map[key0:val0 key1:val1 key2:val2 key3:val3 key4:val4 key5:val5]
// map[newKey0:newVal0 newKey1:newVal1 newKey2:newVal2]
}

Height

  • Description: Height returns the height of the tree.

  • Format:

Height() int
  • Example:
func ExampleBTree_Height() {
tree := gtree.NewBTree(3, gutil.ComparatorInt)
for i := 0; i < 100; i++ {
tree.Set(i, i)
}
fmt.Println(tree.Height())

// Output:
// 6
}

Left

  • Description: Left returns the leftmost (smallest) node of type *BTreeEntry, or nil if the tree is empty.

  • Format:

Left() *BTreeEntry
  • Example:
func ExampleBTree_Left() {
tree := gtree.NewBTree(3, gutil.ComparatorInt)
for i := 1; i < 100; i++ {
tree.Set(i, i)
}
fmt.Println(tree.Left().Key, tree.Left().Value)

emptyTree := gtree.NewBTree(3, gutil.ComparatorInt)
fmt.Println(emptyTree.Left())

// Output:
// 1 1
// <nil>
}
  • Description: Right returns the rightmost (largest) node of type *BTreeEntry, or nil if the tree is empty.

  • Format:

Right() *BTreeEntry
  • Example:
func ExampleBTree_Right() {
tree := gtree.NewBTree(3, gutil.ComparatorInt)
for i := 1; i < 100; i++ {
tree.Set(i, i)
}
fmt.Println(tree.Right().Key, tree.Right().Value)

emptyTree := gtree.NewBTree(3, gutil.ComparatorInt)
fmt.Println(emptyTree.Left())

// Output:
// 99 99
// <nil>
}

String

  • Description: String returns a display (for debugging) of the node in the tree.

  • Format:

String() string
  • Example:
func ExampleBTree_String() {
tree := gtree.NewBTree(3, gutil.ComparatorString)
for i := 0; i < 6; i++ {
tree.Set("key"+gconv.String(i), "val"+gconv.String(i))
}

fmt.Println(tree.String())

// Output:
// key0
// key1
// key2
// key3
// key4
// key5
}
  • Description: Search searches the tree using the parameter key. If the key is found, it returns its corresponding key-value and returns the parameter found as true, otherwise false.

  • Format:

Search(key interface{}) (value interface{}, found bool)
  • Example:
func ExampleBTree_Search() {
tree := gtree.NewBTree(3, gutil.ComparatorString)
for i := 0; i < 6; i++ {
tree.Set("key"+gconv.String(i), "val"+gconv.String(i))
}

fmt.Println(tree.Search("key0"))
fmt.Println(tree.Search("key6"))

// Output:
// val0 true
// <nil> false
}

Print

  • Description: Print prints the tree to standard output.

  • Format:

Print()
  • Example:
func ExampleBTree_Print() {
tree := gtree.NewBTree(3, gutil.ComparatorString)
for i := 0; i < 6; i++ {
tree.Set("key"+gconv.String(i), "val"+gconv.String(i))
}

tree.Print()

// Output:
// key0
// key1
// key2
// key3
// key4
// key5
}

Iterator

  • Description: Iterator is equivalent to IteratorAsc.

  • Format:

Iterator(f func(key, value interface{}) bool)
  • Example:
func ExampleBTree_Iterator() {
tree := gtree.NewBTree(3, gutil.ComparatorString)
for i := 0; i < 10; i++ {
tree.Set(i, 10-i)
}

var totalKey, totalValue int
tree.Iterator(func(key, value interface{}) bool {
totalKey += key.(int)
totalValue += value.(int)

return totalValue < 20
})

fmt.Println("totalKey:", totalKey)
fmt.Println("totalValue:", totalValue)

// Output:
// totalKey: 3
// totalValue: 27
}

IteratorFrom

  • Description: IteratorFrom is equivalent to IteratorAscFrom.

  • Format:

IteratorFrom(key interface{}, match bool, f func(key, value interface{}) bool)
  • Example:
func ExampleBTree_IteratorFrom() {
m := make(map[interface{}]interface{})
for i := 1; i <= 5; i++ {
m[i] = i * 10
}
tree := gtree.NewBTreeFrom(3, gutil.ComparatorInt, m)

tree.IteratorFrom(1, true, func(key, value interface{}) bool {
fmt.Println("key:", key, ", value:", value)
return true
})

// Output:
// key: 1 , value: 10
// key: 2 , value: 20
// key: 3 , value: 30
// key: 4 , value: 40
// key: 5 , value: 50
}

IteratorAsc

  • Description: IteratorAsc iterates over the tree in ascending order using a custom callback function f in read-only mode. If f returns true, it continues iterating; if it returns false, it stops.

  • Format:

IteratorAsc(f func(key, value interface{}) bool)
  • Example:
func ExampleBTree_IteratorAsc() {
tree := gtree.NewBTree(3, gutil.ComparatorString)
for i := 0; i < 10; i++ {
tree.Set(i, 10-i)
}

tree.IteratorAsc(func(key, value interface{}) bool {
fmt.Println("key:", key, ", value:", value)
return true
})

// Output:
// key: 0 , value: 10
// key: 1 , value: 9
// key: 2 , value: 8
// key: 3 , value: 7
// key: 4 , value: 6
// key: 5 , value: 5
// key: 6 , value: 4
// key: 7 , value: 3
// key: 8 , value: 2
// key: 9 , value: 1
}

IteratorAscFrom

  • Description: IteratorAscFrom iterates over the tree in ascending order using a custom callback function f in read-only mode. The parameter key specifies from which key to start iterating. When match is true, iteration starts from the complete match of key; otherwise, iteration uses index searching. If f returns true, it continues iterating; if it returns false, it stops.

  • Format:

IteratorAscFrom(key interface{}, match bool, f func(key, value interface{}) bool)
  • Example:
func ExampleBTree_IteratorAscFrom_Normal() {
m := make(map[interface{}]interface{})
for i := 1; i <= 5; i++ {
m[i] = i * 10
}
tree := gtree.NewBTreeFrom(3, gutil.ComparatorInt, m)

tree.IteratorAscFrom(1, true, func(key, value interface{}) bool {
fmt.Println("key:", key, ", value:", value)
return true
})

// Output:
// key: 1 , value: 10
// key: 2 , value: 20
// key: 3 , value: 30
// key: 4 , value: 40
// key: 5 , value: 50
}
func ExampleBTree_IteratorAscFrom_NoExistKey() {
m := make(map[interface{}]interface{})
for i := 1; i <= 5; i++ {
m[i] = i * 10
}
tree := gtree.NewBTreeFrom(3, gutil.ComparatorInt, m)

tree.IteratorAscFrom(0, true, func(key, value interface{}) bool {
fmt.Println("key:", key, ", value:", value)
return true
})

// Output:
}
func ExampleBTree_IteratorAscFrom_NoExistKeyAndMatchFalse() {
m := make(map[interface{}]interface{})
for i := 1; i <= 5; i++ {
m[i] = i * 10
}
tree := gtree.NewBTreeFrom(3, gutil.ComparatorInt, m)

tree.IteratorAscFrom(0, false, func(key, value interface{}) bool {
fmt.Println("key:", key, ", value:", value)
return true
})

// Output:
// key: 1 , value: 10
// key: 2 , value: 20
// key: 3 , value: 30
// key: 4 , value: 40
// key: 5 , value: 50
}

IteratorDesc

  • Description: IteratorDesc iterates over the tree in descending order using a custom callback function f in read-only mode. If f returns true, it continues iterating; if it returns false, it stops.

  • Format:

IteratorDesc(f func(key, value interface{}) bool)
  • Example:
func ExampleBTree_IteratorDesc() {
tree := gtree.NewBTree(3, gutil.ComparatorString)
for i := 0; i < 10; i++ {
tree.Set(i, 10-i)
}

tree.IteratorDesc(func(key, value interface{}) bool {
fmt.Println("key:", key, ", value:", value)
return true
})

// Output:
// key: 9 , value: 1
// key: 8 , value: 2
// key: 7 , value: 3
// key: 6 , value: 4
// key: 5 , value: 5
// key: 4 , value: 6
// key: 3 , value: 7
// key: 2 , value: 8
// key: 1 , value: 9
// key: 0 , value: 10
}

IteratorDescFrom

  • Description: IteratorDescFrom iterates over the tree in descending order using a custom callback function f in read-only mode. The parameter key specifies from which key to start iterating. When match is true, iteration starts from the complete match of key; otherwise, iteration uses index searching. If f returns true, it continues iterating; if it returns false, it stops.

  • Format:

IteratorDescFrom(key interface{}, match bool, f func(key, value interface{}) bool)
  • Example:
func ExampleBTree_IteratorDescFrom() {
m := make(map[interface{}]interface{})
for i := 1; i <= 5; i++ {
m[i] = i * 10
}
tree := gtree.NewBTreeFrom(3, gutil.ComparatorInt, m)

tree.IteratorDescFrom(5, true, func(key, value interface{}) bool {
fmt.Println("key:", key, ", value:", value)
return true
})

// Output:
// key: 5 , value: 50
// key: 4 , value: 40
// key: 3 , value: 30
// key: 2 , value: 20
// key: 1 , value: 10
}

MarshalJson

  • Description: MarshalJSON implements the json.Marshal interface.

  • Format:

MarshalJSON() ([]byte, error)
  • Example:
func ExampleBTree_MarshalJSON() {
tree := gtree.NewBTree(3, gutil.ComparatorString)
for i := 0; i < 6; i++ {
tree.Set("key"+gconv.String(i), "val"+gconv.String(i))
}

bytes, err := json.Marshal(tree)
if err == nil {
fmt.Println(gconv.String(bytes))
}

// Output:
// {"key0":"val0","key1":"val1","key2":"val2","key3":"val3","key4":"val4","key5":"val5"}
}