gtree

package
v2.3.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 8, 2023 License: MIT Imports: 10 Imported by: 14

Documentation

Overview

Package gtree provides concurrent-safe/unsafe tree containers.

Some implements are from: https://github.com/emirpasic/gods

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type AVLTree

type AVLTree struct {
	// contains filtered or unexported fields
}

AVLTree holds elements of the AVL tree.

func NewAVLTree

func NewAVLTree(comparator func(v1, v2 interface{}) int, safe ...bool) *AVLTree

NewAVLTree instantiates an AVL tree with the custom key comparator. The parameter `safe` is used to specify whether using tree in concurrent-safety, which is false in default.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/container/gtree"
	"github.com/gogf/gf/v2/util/gconv"
	"github.com/gogf/gf/v2/util/gutil"
)

func main() {
	avlTree := gtree.NewAVLTree(gutil.ComparatorString)
	for i := 0; i < 6; i++ {
		avlTree.Set("key"+gconv.String(i), "val"+gconv.String(i))
	}

	fmt.Println(avlTree)

}
Output:

│       ┌── key5
│   ┌── key4
└── key3
    │   ┌── key2
    └── key1
        └── key0

func NewAVLTreeFrom

func NewAVLTreeFrom(comparator func(v1, v2 interface{}) int, data map[interface{}]interface{}, safe ...bool) *AVLTree

NewAVLTreeFrom instantiates an AVL tree with the custom key comparator and data map. The parameter `safe` is used to specify whether using tree in concurrent-safety, which is false in default.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/container/gtree"
	"github.com/gogf/gf/v2/util/gconv"
	"github.com/gogf/gf/v2/util/gutil"
)

func main() {
	avlTree := gtree.NewAVLTree(gutil.ComparatorString)
	for i := 0; i < 6; i++ {
		avlTree.Set("key"+gconv.String(i), "val"+gconv.String(i))
	}

	otherAvlTree := gtree.NewAVLTreeFrom(gutil.ComparatorString, avlTree.Map())
	fmt.Println(otherAvlTree)

	// May Output:
	// │   ┌── key5
	// │   │   └── key4
	// └── key3
	//     │   ┌── key2
	//     └── key1
	//         └── key0
}
Output:

func (*AVLTree) Ceiling

func (tree *AVLTree) Ceiling(key interface{}) (ceiling *AVLTreeNode, found bool)

Ceiling finds ceiling node of the input key, return the ceiling node or nil if no ceiling node is found. Second return parameter is true if ceiling was found, otherwise false.

Ceiling node is defined as the smallest node that is larger than or equal to the given node. A ceiling node may not be found, either because the tree is empty, or because all nodes in the tree is smaller than the given node.

Key should adhere to the comparator's type assertion, otherwise method panics.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/container/gtree"
	"github.com/gogf/gf/v2/util/gutil"
)

func main() {
	tree := gtree.NewAVLTree(gutil.ComparatorInt)
	for i := 1; i < 100; i++ {
		if i != 50 {
			tree.Set(i, i)
		}
	}

	node, found := tree.Ceiling(1)
	if found {
		fmt.Println("Ceiling 1:", node.Key)
	}

	node, found = tree.Ceiling(50)
	if found {
		fmt.Println("Ceiling 50:", node.Key)
	}

	node, found = tree.Ceiling(100)
	if found {
		fmt.Println("Ceiling 100:", node.Key)
	}

	node, found = tree.Ceiling(-1)
	if found {
		fmt.Println("Ceiling -1:", node.Key)
	}

}
Output:

Ceiling 1: 1
Ceiling 50: 51
Ceiling -1: 1

func (*AVLTree) Clear

func (tree *AVLTree) Clear()

Clear removes all nodes from the tree.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/container/gtree"
	"github.com/gogf/gf/v2/util/gconv"
	"github.com/gogf/gf/v2/util/gutil"
)

func main() {
	tree := gtree.NewAVLTree(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

func (*AVLTree) Clone

func (tree *AVLTree) Clone() *AVLTree

Clone returns a new tree with a copy of current tree.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/container/gtree"
	"github.com/gogf/gf/v2/util/gconv"
	"github.com/gogf/gf/v2/util/gutil"
)

func main() {
	avl := gtree.NewAVLTree(gutil.ComparatorString)
	for i := 0; i < 6; i++ {
		avl.Set("key"+gconv.String(i), "val"+gconv.String(i))
	}

	tree := avl.Clone()

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

}
Output:

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

func (*AVLTree) Contains

func (tree *AVLTree) Contains(key interface{}) bool

Contains checks whether `key` exists in the tree.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/container/gtree"
	"github.com/gogf/gf/v2/util/gconv"
	"github.com/gogf/gf/v2/util/gutil"
)

func main() {
	tree := gtree.NewAVLTree(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

func (*AVLTree) Flip

func (tree *AVLTree) Flip(comparator ...func(v1, v2 interface{}) int)

Flip exchanges key-value of the tree to value-key. Note that you should guarantee the value is the same type as key, or else the comparator would panic.

If the type of value is different with key, you pass the new `comparator`.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/container/gtree"
	"github.com/gogf/gf/v2/util/gutil"
)

func main() {
	tree := gtree.NewAVLTree(gutil.ComparatorInt)
	for i := 1; i < 6; i++ {
		tree.Set(i, i*10)
	}

	fmt.Println("Before Flip", tree.Map())

	tree.Flip()

	fmt.Println("After Flip", tree.Map())

}
Output:

Before Flip map[1:10 2:20 3:30 4:40 5:50]
After Flip map[10:1 20:2 30:3 40:4 50:5]

func (*AVLTree) Floor

func (tree *AVLTree) Floor(key interface{}) (floor *AVLTreeNode, found bool)

Floor Finds floor node of the input key, return the floor node or nil if no floor node is found. Second return parameter is true if floor was found, otherwise false.

Floor node is defined as the largest node that is smaller than or equal to the given node. A floor node may not be found, either because the tree is empty, or because all nodes in the tree is larger than the given node.

Key should adhere to the comparator's type assertion, otherwise method panics.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/container/gtree"
	"github.com/gogf/gf/v2/util/gutil"
)

func main() {
	tree := gtree.NewAVLTree(gutil.ComparatorInt)
	for i := 1; i < 100; i++ {
		if i != 50 {
			tree.Set(i, i)
		}
	}

	node, found := tree.Floor(95)
	if found {
		fmt.Println("Floor 95:", node.Key)
	}

	node, found = tree.Floor(50)
	if found {
		fmt.Println("Floor 50:", node.Key)
	}

	node, found = tree.Floor(100)
	if found {
		fmt.Println("Floor 100:", node.Key)
	}

	node, found = tree.Floor(0)
	if found {
		fmt.Println("Floor 0:", node.Key)
	}

}
Output:

Floor 95: 95
Floor 50: 49
Floor 100: 99

func (*AVLTree) Get

func (tree *AVLTree) Get(key interface{}) (value interface{})

Get searches the node in the tree by `key` and returns its value or nil if key is not found in tree.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/container/gtree"
	"github.com/gogf/gf/v2/util/gconv"
	"github.com/gogf/gf/v2/util/gutil"
)

func main() {
	tree := gtree.NewAVLTree(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>

func (*AVLTree) GetOrSet

func (tree *AVLTree) GetOrSet(key interface{}, value interface{}) interface{}

GetOrSet returns the value by key, or sets value with given `value` if it does not exist and then returns this value.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/container/gtree"
	"github.com/gogf/gf/v2/util/gconv"
	"github.com/gogf/gf/v2/util/gutil"
)

func main() {
	tree := gtree.NewAVLTree(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

func (*AVLTree) GetOrSetFunc

func (tree *AVLTree) GetOrSetFunc(key interface{}, f func() interface{}) interface{}

GetOrSetFunc returns the value by key, or sets value with returned value of callback function `f` if it does not exist and then returns this value.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/container/gtree"
	"github.com/gogf/gf/v2/util/gconv"
	"github.com/gogf/gf/v2/util/gutil"
)

func main() {
	tree := gtree.NewAVLTree(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

func (*AVLTree) GetOrSetFuncLock

func (tree *AVLTree) GetOrSetFuncLock(key interface{}, f func() interface{}) interface{}

GetOrSetFuncLock returns the value by key, or sets value with returned value of callback function `f` if it does not exist and then returns this value.

GetOrSetFuncLock differs with GetOrSetFunc function is that it executes function `f` with mutex.Lock of the hash map.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/container/gtree"
	"github.com/gogf/gf/v2/util/gconv"
	"github.com/gogf/gf/v2/util/gutil"
)

func main() {
	tree := gtree.NewAVLTree(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

func (*AVLTree) GetVar

func (tree *AVLTree) GetVar(key interface{}) *gvar.Var

GetVar returns a gvar.Var with the value by given `key`. The returned gvar.Var is un-concurrent safe.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/container/gtree"
	"github.com/gogf/gf/v2/util/gconv"
	"github.com/gogf/gf/v2/util/gutil"
)

func main() {
	tree := gtree.NewAVLTree(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

func (*AVLTree) GetVarOrSet

func (tree *AVLTree) GetVarOrSet(key interface{}, value interface{}) *gvar.Var

GetVarOrSet returns a gvar.Var with result from GetVarOrSet. The returned gvar.Var is un-concurrent safe.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/container/gtree"
	"github.com/gogf/gf/v2/util/gconv"
	"github.com/gogf/gf/v2/util/gutil"
)

func main() {
	tree := gtree.NewAVLTree(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

func (*AVLTree) GetVarOrSetFunc

func (tree *AVLTree) GetVarOrSetFunc(key interface{}, f func() interface{}) *gvar.Var

GetVarOrSetFunc returns a gvar.Var with result from GetOrSetFunc. The returned gvar.Var is un-concurrent safe.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/container/gtree"
	"github.com/gogf/gf/v2/util/gconv"
	"github.com/gogf/gf/v2/util/gutil"
)

func main() {
	tree := gtree.NewAVLTree(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

func (*AVLTree) GetVarOrSetFuncLock

func (tree *AVLTree) GetVarOrSetFuncLock(key interface{}, f func() interface{}) *gvar.Var

GetVarOrSetFuncLock returns a gvar.Var with result from GetOrSetFuncLock. The returned gvar.Var is un-concurrent safe.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/container/gtree"
	"github.com/gogf/gf/v2/util/gconv"
	"github.com/gogf/gf/v2/util/gutil"
)

func main() {
	tree := gtree.NewAVLTree(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

func (*AVLTree) IsEmpty

func (tree *AVLTree) IsEmpty() bool

IsEmpty returns true if tree does not contain any nodes.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/container/gtree"
	"github.com/gogf/gf/v2/util/gconv"
	"github.com/gogf/gf/v2/util/gutil"
)

func main() {
	tree := gtree.NewAVLTree(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

func (*AVLTree) Iterator

func (tree *AVLTree) Iterator(f func(key, value interface{}) bool)

Iterator is alias of IteratorAsc.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/container/gtree"
	"github.com/gogf/gf/v2/util/gutil"
)

func main() {
	tree := gtree.NewAVLTree(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

func (*AVLTree) IteratorAsc

func (tree *AVLTree) IteratorAsc(f func(key, value interface{}) bool)

IteratorAsc iterates the tree readonly in ascending order with given callback function `f`. If `f` returns true, then it continues iterating; or false to stop.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/container/gtree"
	"github.com/gogf/gf/v2/util/gutil"
)

func main() {
	tree := gtree.NewAVLTree(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

func (*AVLTree) IteratorAscFrom

func (tree *AVLTree) IteratorAscFrom(key interface{}, match bool, f func(key, value interface{}) bool)

IteratorAscFrom iterates the tree readonly in ascending order with given callback function `f`. The parameter `key` specifies the start entry for iterating. The `match` specifies whether starting iterating if the `key` is fully matched, or else using index searching iterating. If `f` returns true, then it continues iterating; or false to stop.

func (*AVLTree) IteratorDesc

func (tree *AVLTree) IteratorDesc(f func(key, value interface{}) bool)

IteratorDesc iterates the tree readonly in descending order with given callback function `f`. If `f` returns true, then it continues iterating; or false to stop.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/container/gtree"
	"github.com/gogf/gf/v2/util/gutil"
)

func main() {
	tree := gtree.NewAVLTree(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

func (*AVLTree) IteratorDescFrom

func (tree *AVLTree) IteratorDescFrom(key interface{}, match bool, f func(key, value interface{}) bool)

IteratorDescFrom iterates the tree readonly in descending order with given callback function `f`. The parameter `key` specifies the start entry for iterating. The `match` specifies whether starting iterating if the `key` is fully matched, or else using index searching iterating. If `f` returns true, then it continues iterating; or false to stop.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/container/gtree"
	"github.com/gogf/gf/v2/util/gutil"
)

func main() {
	m := make(map[interface{}]interface{})
	for i := 1; i <= 5; i++ {
		m[i] = i * 10
	}
	tree := gtree.NewAVLTreeFrom(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

func (*AVLTree) IteratorFrom

func (tree *AVLTree) IteratorFrom(key interface{}, match bool, f func(key, value interface{}) bool)

IteratorFrom is alias of IteratorAscFrom.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/container/gtree"
	"github.com/gogf/gf/v2/util/gutil"
)

func main() {
	m := make(map[interface{}]interface{})
	for i := 1; i <= 5; i++ {
		m[i] = i * 10
	}
	tree := gtree.NewAVLTreeFrom(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

func (*AVLTree) Keys

func (tree *AVLTree) Keys() []interface{}

Keys returns all keys in asc order.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/container/gtree"
	"github.com/gogf/gf/v2/util/gconv"
	"github.com/gogf/gf/v2/util/gutil"
)

func main() {
	tree := gtree.NewAVLTree(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]

func (*AVLTree) Left

func (tree *AVLTree) Left() *AVLTreeNode

Left returns the minimum element of the AVL tree or nil if the tree is empty.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/container/gtree"
	"github.com/gogf/gf/v2/util/gutil"
)

func main() {
	tree := gtree.NewAVLTree(gutil.ComparatorInt)
	for i := 1; i < 100; i++ {
		tree.Set(i, i)
	}
	fmt.Println(tree.Left().Key, tree.Left().Value)

	emptyTree := gtree.NewAVLTree(gutil.ComparatorInt)
	fmt.Println(emptyTree.Left())

}
Output:

1 1
<nil>

func (*AVLTree) Map

func (tree *AVLTree) Map() map[interface{}]interface{}

Map returns all key-value items as map.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/container/gtree"
	"github.com/gogf/gf/v2/util/gconv"
	"github.com/gogf/gf/v2/util/gutil"
)

func main() {
	tree := gtree.NewAVLTree(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]

func (*AVLTree) MapStrAny

func (tree *AVLTree) MapStrAny() map[string]interface{}

MapStrAny returns all key-value items as map[string]interface{}.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/container/gtree"
	"github.com/gogf/gf/v2/util/gconv"
	"github.com/gogf/gf/v2/util/gutil"
)

func main() {
	tree := gtree.NewAVLTree(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]

func (AVLTree) MarshalJSON

func (tree AVLTree) MarshalJSON() (jsonBytes []byte, err error)

MarshalJSON implements the interface MarshalJSON for json.Marshal.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/container/gtree"
	"github.com/gogf/gf/v2/internal/json"
	"github.com/gogf/gf/v2/util/gconv"
	"github.com/gogf/gf/v2/util/gutil"
)

func main() {
	tree := gtree.NewAVLTree(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"}

func (*AVLTree) Print

func (tree *AVLTree) Print()

Print prints the tree to stdout.

Example
package main

import (
	"github.com/gogf/gf/v2/container/gtree"
	"github.com/gogf/gf/v2/util/gconv"
	"github.com/gogf/gf/v2/util/gutil"
)

func main() {
	tree := gtree.NewAVLTree(gutil.ComparatorString)
	for i := 0; i < 6; i++ {
		tree.Set("key"+gconv.String(i), "val"+gconv.String(i))
	}

	tree.Print()

}
Output:

│       ┌── key5
│   ┌── key4
└── key3
    │   ┌── key2
    └── key1
        └── key0

func (*AVLTree) Remove

func (tree *AVLTree) Remove(key interface{}) (value interface{})

Remove removes the node from the tree by key. Key should adhere to the comparator's type assertion, otherwise method panics.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/container/gtree"
	"github.com/gogf/gf/v2/util/gconv"
	"github.com/gogf/gf/v2/util/gutil"
)

func main() {
	tree := gtree.NewAVLTree(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]

func (*AVLTree) Removes

func (tree *AVLTree) Removes(keys []interface{})

Removes batch deletes values of the tree by `keys`.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/container/gtree"
	"github.com/gogf/gf/v2/util/gconv"
	"github.com/gogf/gf/v2/util/gutil"
)

func main() {
	tree := gtree.NewAVLTree(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]

func (*AVLTree) Replace

func (tree *AVLTree) Replace(data map[interface{}]interface{})

Replace the data of the tree with given `data`.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/container/gtree"
	"github.com/gogf/gf/v2/util/gconv"
	"github.com/gogf/gf/v2/util/gutil"
)

func main() {
	tree := gtree.NewAVLTree(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]

func (*AVLTree) Right

func (tree *AVLTree) Right() *AVLTreeNode

Right returns the maximum element of the AVL tree or nil if the tree is empty.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/container/gtree"
	"github.com/gogf/gf/v2/util/gutil"
)

func main() {
	tree := gtree.NewAVLTree(gutil.ComparatorInt)
	for i := 1; i < 100; i++ {
		tree.Set(i, i)
	}
	fmt.Println(tree.Right().Key, tree.Right().Value)

	emptyTree := gtree.NewAVLTree(gutil.ComparatorInt)
	fmt.Println(emptyTree.Left())

}
Output:

99 99
<nil>

func (*AVLTree) Search

func (tree *AVLTree) Search(key interface{}) (value interface{}, found bool)

Search searches the tree with given `key`. Second return parameter `found` is true if key was found, otherwise false.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/container/gtree"
	"github.com/gogf/gf/v2/util/gconv"
	"github.com/gogf/gf/v2/util/gutil"
)

func main() {
	tree := gtree.NewAVLTree(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

func (*AVLTree) Set

func (tree *AVLTree) Set(key interface{}, value interface{})

Set inserts node into the tree.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/container/gtree"
	"github.com/gogf/gf/v2/util/gconv"
	"github.com/gogf/gf/v2/util/gutil"
)

func main() {
	tree := gtree.NewAVLTree(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

func (*AVLTree) SetIfNotExist

func (tree *AVLTree) SetIfNotExist(key interface{}, value interface{}) bool

SetIfNotExist sets `value` to the map if the `key` does not exist, and then returns true. It returns false if `key` exists, and `value` would be ignored.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/container/gtree"
	"github.com/gogf/gf/v2/util/gconv"
	"github.com/gogf/gf/v2/util/gutil"
)

func main() {
	tree := gtree.NewAVLTree(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

func (*AVLTree) SetIfNotExistFunc

func (tree *AVLTree) SetIfNotExistFunc(key interface{}, f func() interface{}) bool

SetIfNotExistFunc sets value with return value of callback function `f`, and then returns true. It returns false if `key` exists, and `value` would be ignored.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/container/gtree"
	"github.com/gogf/gf/v2/util/gconv"
	"github.com/gogf/gf/v2/util/gutil"
)

func main() {
	tree := gtree.NewAVLTree(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

func (*AVLTree) SetIfNotExistFuncLock

func (tree *AVLTree) SetIfNotExistFuncLock(key interface{}, f func() interface{}) bool

SetIfNotExistFuncLock sets value with return value of callback function `f`, and then returns true. It returns false if `key` exists, and `value` would be ignored.

SetIfNotExistFuncLock differs with SetIfNotExistFunc function is that it executes function `f` with mutex.Lock of the hash map.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/container/gtree"
	"github.com/gogf/gf/v2/util/gconv"
	"github.com/gogf/gf/v2/util/gutil"
)

func main() {
	tree := gtree.NewAVLTree(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

func (*AVLTree) Sets

func (tree *AVLTree) Sets(data map[interface{}]interface{})

Sets batch sets key-values to the tree.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/container/gtree"
	"github.com/gogf/gf/v2/util/gutil"
)

func main() {
	tree := gtree.NewAVLTree(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

func (*AVLTree) Size

func (tree *AVLTree) Size() int

Size returns number of nodes in the tree.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/container/gtree"
	"github.com/gogf/gf/v2/util/gconv"
	"github.com/gogf/gf/v2/util/gutil"
)

func main() {
	tree := gtree.NewAVLTree(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

func (*AVLTree) String

func (tree *AVLTree) String() string

String returns a string representation of container

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/container/gtree"
	"github.com/gogf/gf/v2/util/gconv"
	"github.com/gogf/gf/v2/util/gutil"
)

func main() {
	tree := gtree.NewAVLTree(gutil.ComparatorString)
	for i := 0; i < 6; i++ {
		tree.Set("key"+gconv.String(i), "val"+gconv.String(i))
	}

	fmt.Println(tree.String())

}
Output:

│       ┌── key5
│   ┌── key4
└── key3
    │   ┌── key2
    └── key1
        └── key0

func (*AVLTree) Values

func (tree *AVLTree) Values() []interface{}

Values returns all values in asc order based on the key.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/container/gtree"
	"github.com/gogf/gf/v2/util/gconv"
	"github.com/gogf/gf/v2/util/gutil"
)

func main() {
	tree := gtree.NewAVLTree(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]

type AVLTreeNode

type AVLTreeNode struct {
	Key   interface{}
	Value interface{}
	// contains filtered or unexported fields
}

AVLTreeNode is a single element within the tree.

func (*AVLTreeNode) Next

func (node *AVLTreeNode) Next() *AVLTreeNode

Next returns the next element in an inorder walk of the AVL tree.

func (*AVLTreeNode) Prev

func (node *AVLTreeNode) Prev() *AVLTreeNode

Prev returns the previous element in an inorder walk of the AVL tree.

type BTree

type BTree struct {
	// contains filtered or unexported fields
}

BTree holds elements of the B-tree.

func NewBTree

func NewBTree(m int, comparator func(v1, v2 interface{}) int, safe ...bool) *BTree

NewBTree instantiates a B-tree with `m` (maximum number of children) and a custom key comparator. The parameter `safe` is used to specify whether using tree in concurrent-safety, which is false in default. Note that the `m` must be greater or equal than 3, or else it panics.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/container/gtree"
	"github.com/gogf/gf/v2/util/gconv"
	"github.com/gogf/gf/v2/util/gutil"
)

func main() {
	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]

func NewBTreeFrom

func NewBTreeFrom(m int, comparator func(v1, v2 interface{}) int, data map[interface{}]interface{}, safe ...bool) *BTree

NewBTreeFrom instantiates a B-tree with `m` (maximum number of children), a custom key comparator and data map. The parameter `safe` is used to specify whether using tree in concurrent-safety, which is false in default.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/container/gtree"
	"github.com/gogf/gf/v2/util/gconv"
	"github.com/gogf/gf/v2/util/gutil"
)

func main() {
	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]

func (*BTree) Clear

func (tree *BTree) Clear()

Clear removes all nodes from the tree.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/container/gtree"
	"github.com/gogf/gf/v2/util/gconv"
	"github.com/gogf/gf/v2/util/gutil"
)

func main() {
	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

func (*BTree) Clone

func (tree *BTree) Clone() *BTree

Clone returns a new tree with a copy of current tree.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/container/gtree"
	"github.com/gogf/gf/v2/util/gconv"
	"github.com/gogf/gf/v2/util/gutil"
)

func main() {
	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

func (*BTree) Contains

func (tree *BTree) Contains(key interface{}) bool

Contains checks whether `key` exists in the tree.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/container/gtree"
	"github.com/gogf/gf/v2/util/gconv"
	"github.com/gogf/gf/v2/util/gutil"
)

func main() {
	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

func (*BTree) Get

func (tree *BTree) Get(key interface{}) (value interface{})

Get searches the node in the tree by `key` and returns its value or nil if key is not found in tree.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/container/gtree"
	"github.com/gogf/gf/v2/util/gconv"
	"github.com/gogf/gf/v2/util/gutil"
)

func main() {
	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>

func (*BTree) GetOrSet

func (tree *BTree) GetOrSet(key interface{}, value interface{}) interface{}

GetOrSet returns the value by key, or sets value with given `value` if it does not exist and then returns this value.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/container/gtree"
	"github.com/gogf/gf/v2/util/gconv"
	"github.com/gogf/gf/v2/util/gutil"
)

func main() {
	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

func (*BTree) GetOrSetFunc

func (tree *BTree) GetOrSetFunc(key interface{}, f func() interface{}) interface{}

GetOrSetFunc returns the value by key, or sets value with returned value of callback function `f` if it does not exist and then returns this value.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/container/gtree"
	"github.com/gogf/gf/v2/util/gconv"
	"github.com/gogf/gf/v2/util/gutil"
)

func main() {
	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

func (*BTree) GetOrSetFuncLock

func (tree *BTree) GetOrSetFuncLock(key interface{}, f func() interface{}) interface{}

GetOrSetFuncLock returns the value by key, or sets value with returned value of callback function `f` if it does not exist and then returns this value.

GetOrSetFuncLock differs with GetOrSetFunc function is that it executes function `f` with mutex.Lock of the hash map.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/container/gtree"
	"github.com/gogf/gf/v2/util/gconv"
	"github.com/gogf/gf/v2/util/gutil"
)

func main() {
	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

func (*BTree) GetVar

func (tree *BTree) GetVar(key interface{}) *gvar.Var

GetVar returns a gvar.Var with the value by given `key`. The returned gvar.Var is un-concurrent safe.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/container/gtree"
	"github.com/gogf/gf/v2/util/gconv"
	"github.com/gogf/gf/v2/util/gutil"
)

func main() {
	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

func (*BTree) GetVarOrSet

func (tree *BTree) GetVarOrSet(key interface{}, value interface{}) *gvar.Var

GetVarOrSet returns a gvar.Var with result from GetVarOrSet. The returned gvar.Var is un-concurrent safe.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/container/gtree"
	"github.com/gogf/gf/v2/util/gconv"
	"github.com/gogf/gf/v2/util/gutil"
)

func main() {
	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

func (*BTree) GetVarOrSetFunc

func (tree *BTree) GetVarOrSetFunc(key interface{}, f func() interface{}) *gvar.Var

GetVarOrSetFunc returns a gvar.Var with result from GetOrSetFunc. The returned gvar.Var is un-concurrent safe.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/container/gtree"
	"github.com/gogf/gf/v2/util/gconv"
	"github.com/gogf/gf/v2/util/gutil"
)

func main() {
	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

func (*BTree) GetVarOrSetFuncLock

func (tree *BTree) GetVarOrSetFuncLock(key interface{}, f func() interface{}) *gvar.Var

GetVarOrSetFuncLock returns a gvar.Var with result from GetOrSetFuncLock. The returned gvar.Var is un-concurrent safe.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/container/gtree"
	"github.com/gogf/gf/v2/util/gconv"
	"github.com/gogf/gf/v2/util/gutil"
)

func main() {
	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

func (*BTree) Height

func (tree *BTree) Height() int

Height returns the height of the tree.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/container/gtree"
	"github.com/gogf/gf/v2/util/gutil"
)

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

}
Output:

6

func (*BTree) IsEmpty

func (tree *BTree) IsEmpty() bool

IsEmpty returns true if tree does not contain any nodes

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/container/gtree"
	"github.com/gogf/gf/v2/util/gconv"
	"github.com/gogf/gf/v2/util/gutil"
)

func main() {
	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

func (*BTree) Iterator

func (tree *BTree) Iterator(f func(key, value interface{}) bool)

Iterator is alias of IteratorAsc.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/container/gtree"
	"github.com/gogf/gf/v2/util/gutil"
)

func main() {
	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

func (*BTree) IteratorAsc

func (tree *BTree) IteratorAsc(f func(key, value interface{}) bool)

IteratorAsc iterates the tree readonly in ascending order with given callback function `f`. If `f` returns true, then it continues iterating; or false to stop.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/container/gtree"
	"github.com/gogf/gf/v2/util/gutil"
)

func main() {
	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

func (*BTree) IteratorAscFrom

func (tree *BTree) IteratorAscFrom(key interface{}, match bool, f func(key, value interface{}) bool)

IteratorAscFrom iterates the tree readonly in ascending order with given callback function `f`. The parameter `key` specifies the start entry for iterating. The `match` specifies whether starting iterating if the `key` is fully matched, or else using index searching iterating. If `f` returns true, then it continues iterating; or false to stop.

func (*BTree) IteratorDesc

func (tree *BTree) IteratorDesc(f func(key, value interface{}) bool)

IteratorDesc iterates the tree readonly in descending order with given callback function `f`. If `f` returns true, then it continues iterating; or false to stop.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/container/gtree"
	"github.com/gogf/gf/v2/util/gutil"
)

func main() {
	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

func (*BTree) IteratorDescFrom

func (tree *BTree) IteratorDescFrom(key interface{}, match bool, f func(key, value interface{}) bool)

IteratorDescFrom iterates the tree readonly in descending order with given callback function `f`. The parameter `key` specifies the start entry for iterating. The `match` specifies whether starting iterating if the `key` is fully matched, or else using index searching iterating. If `f` returns true, then it continues iterating; or false to stop.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/container/gtree"
	"github.com/gogf/gf/v2/util/gutil"
)

func main() {
	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

func (*BTree) IteratorFrom

func (tree *BTree) IteratorFrom(key interface{}, match bool, f func(key, value interface{}) bool)

IteratorFrom is alias of IteratorAscFrom.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/container/gtree"
	"github.com/gogf/gf/v2/util/gutil"
)

func main() {
	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

func (*BTree) Keys

func (tree *BTree) Keys() []interface{}

Keys returns all keys in asc order.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/container/gtree"
	"github.com/gogf/gf/v2/util/gconv"
	"github.com/gogf/gf/v2/util/gutil"
)

func main() {
	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]

func (*BTree) Left

func (tree *BTree) Left() *BTreeEntry

Left returns the left-most (min) entry or nil if tree is empty.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/container/gtree"
	"github.com/gogf/gf/v2/util/gutil"
)

func main() {
	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>

func (*BTree) Map

func (tree *BTree) Map() map[interface{}]interface{}

Map returns all key-value items as map.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/container/gtree"
	"github.com/gogf/gf/v2/util/gconv"
	"github.com/gogf/gf/v2/util/gutil"
)

func main() {
	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]

func (*BTree) MapStrAny

func (tree *BTree) MapStrAny() map[string]interface{}

MapStrAny returns all key-value items as map[string]interface{}.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/container/gtree"
	"github.com/gogf/gf/v2/util/gconv"
	"github.com/gogf/gf/v2/util/gutil"
)

func main() {
	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]

func (BTree) MarshalJSON

func (tree BTree) MarshalJSON() (jsonBytes []byte, err error)

MarshalJSON implements the interface MarshalJSON for json.Marshal.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/container/gtree"
	"github.com/gogf/gf/v2/internal/json"
	"github.com/gogf/gf/v2/util/gconv"
	"github.com/gogf/gf/v2/util/gutil"
)

func main() {
	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"}

func (*BTree) Print

func (tree *BTree) Print()

Print prints the tree to stdout.

Example
package main

import (
	"github.com/gogf/gf/v2/container/gtree"
	"github.com/gogf/gf/v2/util/gconv"
	"github.com/gogf/gf/v2/util/gutil"
)

func main() {
	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

func (*BTree) Remove

func (tree *BTree) Remove(key interface{}) (value interface{})

Remove removes the node from the tree by `key`.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/container/gtree"
	"github.com/gogf/gf/v2/util/gconv"
	"github.com/gogf/gf/v2/util/gutil"
)

func main() {
	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]

func (*BTree) Removes

func (tree *BTree) Removes(keys []interface{})

Removes batch deletes values of the tree by `keys`.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/container/gtree"
	"github.com/gogf/gf/v2/util/gconv"
	"github.com/gogf/gf/v2/util/gutil"
)

func main() {
	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]

func (*BTree) Replace

func (tree *BTree) Replace(data map[interface{}]interface{})

Replace the data of the tree with given `data`.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/container/gtree"
	"github.com/gogf/gf/v2/util/gconv"
	"github.com/gogf/gf/v2/util/gutil"
)

func main() {
	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]

func (*BTree) Right

func (tree *BTree) Right() *BTreeEntry

Right returns the right-most (max) entry or nil if tree is empty.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/container/gtree"
	"github.com/gogf/gf/v2/util/gutil"
)

func main() {
	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>

func (*BTree) Search

func (tree *BTree) Search(key interface{}) (value interface{}, found bool)

Search searches the tree with given `key`. Second return parameter `found` is true if key was found, otherwise false.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/container/gtree"
	"github.com/gogf/gf/v2/util/gconv"
	"github.com/gogf/gf/v2/util/gutil"
)

func main() {
	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

func (*BTree) Set

func (tree *BTree) Set(key interface{}, value interface{})

Set inserts key-value item into the tree.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/container/gtree"
	"github.com/gogf/gf/v2/util/gconv"
	"github.com/gogf/gf/v2/util/gutil"
)

func main() {
	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

func (*BTree) SetIfNotExist

func (tree *BTree) SetIfNotExist(key interface{}, value interface{}) bool

SetIfNotExist sets `value` to the map if the `key` does not exist, and then returns true. It returns false if `key` exists, and `value` would be ignored.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/container/gtree"
	"github.com/gogf/gf/v2/util/gconv"
	"github.com/gogf/gf/v2/util/gutil"
)

func main() {
	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

func (*BTree) SetIfNotExistFunc

func (tree *BTree) SetIfNotExistFunc(key interface{}, f func() interface{}) bool

SetIfNotExistFunc sets value with return value of callback function `f`, and then returns true. It returns false if `key` exists, and `value` would be ignored.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/container/gtree"
	"github.com/gogf/gf/v2/util/gconv"
	"github.com/gogf/gf/v2/util/gutil"
)

func main() {
	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

func (*BTree) SetIfNotExistFuncLock

func (tree *BTree) SetIfNotExistFuncLock(key interface{}, f func() interface{}) bool

SetIfNotExistFuncLock sets value with return value of callback function `f`, and then returns true. It returns false if `key` exists, and `value` would be ignored.

SetIfNotExistFuncLock differs with SetIfNotExistFunc function is that it executes function `f` with mutex.Lock of the hash map.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/container/gtree"
	"github.com/gogf/gf/v2/util/gconv"
	"github.com/gogf/gf/v2/util/gutil"
)

func main() {
	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

func (*BTree) Sets

func (tree *BTree) Sets(data map[interface{}]interface{})

Sets batch sets key-values to the tree.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/container/gtree"
	"github.com/gogf/gf/v2/util/gutil"
)

func main() {
	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

func (*BTree) Size

func (tree *BTree) Size() int

Size returns number of nodes in the tree.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/container/gtree"
	"github.com/gogf/gf/v2/util/gconv"
	"github.com/gogf/gf/v2/util/gutil"
)

func main() {
	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

func (*BTree) String

func (tree *BTree) String() string

String returns a string representation of container (for debugging purposes)

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/container/gtree"
	"github.com/gogf/gf/v2/util/gconv"
	"github.com/gogf/gf/v2/util/gutil"
)

func main() {
	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

func (*BTree) Values

func (tree *BTree) Values() []interface{}

Values returns all values in asc order based on the key.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/container/gtree"
	"github.com/gogf/gf/v2/util/gconv"
	"github.com/gogf/gf/v2/util/gutil"
)

func main() {
	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]

type BTreeEntry

type BTreeEntry struct {
	Key   interface{}
	Value interface{}
}

BTreeEntry represents the key-value pair contained within nodes.

type BTreeNode

type BTreeNode struct {
	Parent   *BTreeNode
	Entries  []*BTreeEntry // Contained keys in node
	Children []*BTreeNode  // Children nodes
}

BTreeNode is a single element within the tree.

type RedBlackTree

type RedBlackTree struct {
	// contains filtered or unexported fields
}

RedBlackTree holds elements of the red-black tree.

func NewRedBlackTree

func NewRedBlackTree(comparator func(v1, v2 interface{}) int, safe ...bool) *RedBlackTree

NewRedBlackTree instantiates a red-black tree with the custom key comparator. The parameter `safe` is used to specify whether using tree in concurrent-safety, which is false in default.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/container/gtree"
	"github.com/gogf/gf/v2/util/gconv"
	"github.com/gogf/gf/v2/util/gutil"
)

func main() {
	rbTree := gtree.NewRedBlackTree(gutil.ComparatorString)
	for i := 0; i < 6; i++ {
		rbTree.Set("key"+gconv.String(i), "val"+gconv.String(i))
	}

	fmt.Println(rbTree)

}
Output:

│           ┌── key5
│       ┌── key4
│   ┌── key3
│   │   └── key2
└── key1
    └── key0

func NewRedBlackTreeFrom

func NewRedBlackTreeFrom(comparator func(v1, v2 interface{}) int, data map[interface{}]interface{}, safe ...bool) *RedBlackTree

NewRedBlackTreeFrom instantiates a red-black tree with the custom key comparator and `data` map. The parameter `safe` is used to specify whether using tree in concurrent-safety, which is false in default.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/container/gtree"
	"github.com/gogf/gf/v2/util/gconv"
	"github.com/gogf/gf/v2/util/gutil"
)

func main() {
	rbTree := gtree.NewRedBlackTree(gutil.ComparatorString)
	for i := 0; i < 6; i++ {
		rbTree.Set("key"+gconv.String(i), "val"+gconv.String(i))
	}

	otherRBTree := gtree.NewRedBlackTreeFrom(gutil.ComparatorString, rbTree.Map())
	fmt.Println(otherRBTree)

	// May Output:
	// │           ┌── key5
	// │       ┌── key4
	// │   ┌── key3
	// │   │   └── key2
	// └── key1
	//     └── key0
}
Output:

func (*RedBlackTree) Ceiling

func (tree *RedBlackTree) Ceiling(key interface{}) (ceiling *RedBlackTreeNode, found bool)

Ceiling finds ceiling node of the input key, return the ceiling node or nil if no ceiling node is found. Second return parameter is true if ceiling was found, otherwise false.

Ceiling node is defined as the smallest node that its key is larger than or equal to the given `key`. A ceiling node may not be found, either because the tree is empty, or because all nodes in the tree are smaller than the given node.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/container/gtree"
	"github.com/gogf/gf/v2/util/gutil"
)

func main() {
	tree := gtree.NewRedBlackTree(gutil.ComparatorInt)
	for i := 1; i < 100; i++ {
		if i != 50 {
			tree.Set(i, i)
		}
	}

	node, found := tree.Ceiling(1)
	if found {
		fmt.Println("Ceiling 1:", node.Key)
	}

	node, found = tree.Ceiling(50)
	if found {
		fmt.Println("Ceiling 50:", node.Key)
	}

	node, found = tree.Ceiling(100)
	if found {
		fmt.Println("Ceiling 100:", node.Key)
	}

	node, found = tree.Ceiling(-1)
	if found {
		fmt.Println("Ceiling -1:", node.Key)
	}

}
Output:

Ceiling 1: 1
Ceiling 50: 51
Ceiling -1: 1

func (*RedBlackTree) Clear

func (tree *RedBlackTree) Clear()

Clear removes all nodes from the tree.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/container/gtree"
	"github.com/gogf/gf/v2/util/gconv"
	"github.com/gogf/gf/v2/util/gutil"
)

func main() {
	tree := gtree.NewRedBlackTree(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

func (*RedBlackTree) Clone

func (tree *RedBlackTree) Clone() *RedBlackTree

Clone returns a new tree with a copy of current tree.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/container/gtree"
	"github.com/gogf/gf/v2/util/gconv"
	"github.com/gogf/gf/v2/util/gutil"
)

func main() {
	b := gtree.NewRedBlackTree(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

func (*RedBlackTree) Contains

func (tree *RedBlackTree) Contains(key interface{}) bool

Contains checks whether `key` exists in the tree.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/container/gtree"
	"github.com/gogf/gf/v2/util/gconv"
	"github.com/gogf/gf/v2/util/gutil"
)

func main() {
	tree := gtree.NewRedBlackTree(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

func (*RedBlackTree) Flip

func (tree *RedBlackTree) Flip(comparator ...func(v1, v2 interface{}) int)

Flip exchanges key-value of the tree to value-key. Note that you should guarantee the value is the same type as key, or else the comparator would panic.

If the type of value is different with key, you pass the new `comparator`.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/container/gtree"
	"github.com/gogf/gf/v2/util/gutil"
)

func main() {
	tree := gtree.NewRedBlackTree(gutil.ComparatorString)
	for i := 1; i < 6; i++ {
		tree.Set(i, i*10)
	}

	fmt.Println("Before Flip", tree.Map())

	tree.Flip()

	fmt.Println("After Flip", tree.Map())

}
Output:

Before Flip map[1:10 2:20 3:30 4:40 5:50]
After Flip map[10:1 20:2 30:3 40:4 50:5]

func (*RedBlackTree) Floor

func (tree *RedBlackTree) Floor(key interface{}) (floor *RedBlackTreeNode, found bool)

Floor Finds floor node of the input key, return the floor node or nil if no floor node is found. Second return parameter is true if floor was found, otherwise false.

Floor node is defined as the largest node that its key is smaller than or equal to the given `key`. A floor node may not be found, either because the tree is empty, or because all nodes in the tree are larger than the given node.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/container/gtree"
	"github.com/gogf/gf/v2/util/gutil"
)

func main() {
	tree := gtree.NewRedBlackTree(gutil.ComparatorInt)
	for i := 1; i < 100; i++ {
		if i != 50 {
			tree.Set(i, i)
		}
	}

	node, found := tree.Floor(95)
	if found {
		fmt.Println("Floor 95:", node.Key)
	}

	node, found = tree.Floor(50)
	if found {
		fmt.Println("Floor 50:", node.Key)
	}

	node, found = tree.Floor(100)
	if found {
		fmt.Println("Floor 100:", node.Key)
	}

	node, found = tree.Floor(0)
	if found {
		fmt.Println("Floor 0:", node.Key)
	}

}
Output:

Floor 95: 95
Floor 50: 49
Floor 100: 99

func (*RedBlackTree) Get

func (tree *RedBlackTree) Get(key interface{}) (value interface{})

Get searches the node in the tree by `key` and returns its value or nil if key is not found in tree.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/container/gtree"
	"github.com/gogf/gf/v2/util/gconv"
	"github.com/gogf/gf/v2/util/gutil"
)

func main() {
	tree := gtree.NewRedBlackTree(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>

func (*RedBlackTree) GetOrSet

func (tree *RedBlackTree) GetOrSet(key interface{}, value interface{}) interface{}

GetOrSet returns the value by key, or sets value with given `value` if it does not exist and then returns this value.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/container/gtree"
	"github.com/gogf/gf/v2/util/gconv"
	"github.com/gogf/gf/v2/util/gutil"
)

func main() {
	tree := gtree.NewRedBlackTree(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

func (*RedBlackTree) GetOrSetFunc

func (tree *RedBlackTree) GetOrSetFunc(key interface{}, f func() interface{}) interface{}

GetOrSetFunc returns the value by key, or sets value with returned value of callback function `f` if it does not exist and then returns this value.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/container/gtree"
	"github.com/gogf/gf/v2/util/gconv"
	"github.com/gogf/gf/v2/util/gutil"
)

func main() {
	tree := gtree.NewRedBlackTree(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

func (*RedBlackTree) GetOrSetFuncLock

func (tree *RedBlackTree) GetOrSetFuncLock(key interface{}, f func() interface{}) interface{}

GetOrSetFuncLock returns the value by key, or sets value with returned value of callback function `f` if it does not exist and then returns this value.

GetOrSetFuncLock differs with GetOrSetFunc function is that it executes function `f` with mutex.Lock of the hash map.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/container/gtree"
	"github.com/gogf/gf/v2/util/gconv"
	"github.com/gogf/gf/v2/util/gutil"
)

func main() {
	tree := gtree.NewRedBlackTree(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

func (*RedBlackTree) GetVar

func (tree *RedBlackTree) GetVar(key interface{}) *gvar.Var

GetVar returns a gvar.Var with the value by given `key`. The returned gvar.Var is un-concurrent safe.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/container/gtree"
	"github.com/gogf/gf/v2/util/gconv"
	"github.com/gogf/gf/v2/util/gutil"
)

func main() {
	tree := gtree.NewRedBlackTree(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

func (*RedBlackTree) GetVarOrSet

func (tree *RedBlackTree) GetVarOrSet(key interface{}, value interface{}) *gvar.Var

GetVarOrSet returns a gvar.Var with result from GetVarOrSet. The returned gvar.Var is un-concurrent safe.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/container/gtree"
	"github.com/gogf/gf/v2/util/gconv"
	"github.com/gogf/gf/v2/util/gutil"
)

func main() {
	tree := gtree.NewRedBlackTree(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

func (*RedBlackTree) GetVarOrSetFunc

func (tree *RedBlackTree) GetVarOrSetFunc(key interface{}, f func() interface{}) *gvar.Var

GetVarOrSetFunc returns a gvar.Var with result from GetOrSetFunc. The returned gvar.Var is un-concurrent safe.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/container/gtree"
	"github.com/gogf/gf/v2/util/gconv"
	"github.com/gogf/gf/v2/util/gutil"
)

func main() {
	tree := gtree.NewRedBlackTree(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

func (*RedBlackTree) GetVarOrSetFuncLock

func (tree *RedBlackTree) GetVarOrSetFuncLock(key interface{}, f func() interface{}) *gvar.Var

GetVarOrSetFuncLock returns a gvar.Var with result from GetOrSetFuncLock. The returned gvar.Var is un-concurrent safe.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/container/gtree"
	"github.com/gogf/gf/v2/util/gconv"
	"github.com/gogf/gf/v2/util/gutil"
)

func main() {
	tree := gtree.NewRedBlackTree(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

func (*RedBlackTree) IsEmpty

func (tree *RedBlackTree) IsEmpty() bool

IsEmpty returns true if tree does not contain any nodes.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/container/gtree"
	"github.com/gogf/gf/v2/util/gconv"
	"github.com/gogf/gf/v2/util/gutil"
)

func main() {
	tree := gtree.NewRedBlackTree(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

func (*RedBlackTree) Iterator

func (tree *RedBlackTree) Iterator(f func(key, value interface{}) bool)

Iterator is alias of IteratorAsc.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/container/gtree"
	"github.com/gogf/gf/v2/util/gutil"
)

func main() {
	tree := gtree.NewRedBlackTree(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

func (*RedBlackTree) IteratorAsc

func (tree *RedBlackTree) IteratorAsc(f func(key, value interface{}) bool)

IteratorAsc iterates the tree readonly in ascending order with given callback function `f`. If `f` returns true, then it continues iterating; or false to stop.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/container/gtree"
	"github.com/gogf/gf/v2/util/gutil"
)

func main() {
	tree := gtree.NewRedBlackTree(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

func (*RedBlackTree) IteratorAscFrom

func (tree *RedBlackTree) IteratorAscFrom(key interface{}, match bool, f func(key, value interface{}) bool)

IteratorAscFrom iterates the tree readonly in ascending order with given callback function `f`. The parameter `key` specifies the start entry for iterating. The `match` specifies whether starting iterating if the `key` is fully matched, or else using index searching iterating. If `f` returns true, then it continues iterating; or false to stop.

func (*RedBlackTree) IteratorDesc

func (tree *RedBlackTree) IteratorDesc(f func(key, value interface{}) bool)

IteratorDesc iterates the tree readonly in descending order with given callback function `f`. If `f` returns true, then it continues iterating; or false to stop.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/container/gtree"
	"github.com/gogf/gf/v2/util/gutil"
)

func main() {
	tree := gtree.NewRedBlackTree(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

func (*RedBlackTree) IteratorDescFrom

func (tree *RedBlackTree) IteratorDescFrom(key interface{}, match bool, f func(key, value interface{}) bool)

IteratorDescFrom iterates the tree readonly in descending order with given callback function `f`. The parameter `key` specifies the start entry for iterating. The `match` specifies whether starting iterating if the `key` is fully matched, or else using index searching iterating. If `f` returns true, then it continues iterating; or false to stop.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/container/gtree"
	"github.com/gogf/gf/v2/util/gutil"
)

func main() {
	m := make(map[interface{}]interface{})
	for i := 1; i <= 5; i++ {
		m[i] = i * 10
	}
	tree := gtree.NewRedBlackTreeFrom(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

func (*RedBlackTree) IteratorFrom

func (tree *RedBlackTree) IteratorFrom(key interface{}, match bool, f func(key, value interface{}) bool)

IteratorFrom is alias of IteratorAscFrom.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/container/gtree"
	"github.com/gogf/gf/v2/util/gutil"
)

func main() {
	m := make(map[interface{}]interface{})
	for i := 1; i <= 5; i++ {
		m[i] = i * 10
	}
	tree := gtree.NewRedBlackTreeFrom(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

func (*RedBlackTree) Keys

func (tree *RedBlackTree) Keys() []interface{}

Keys returns all keys in asc order.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/container/gtree"
	"github.com/gogf/gf/v2/util/gconv"
	"github.com/gogf/gf/v2/util/gutil"
)

func main() {
	tree := gtree.NewRedBlackTree(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]

func (*RedBlackTree) Left

func (tree *RedBlackTree) Left() *RedBlackTreeNode

Left returns the left-most (min) node or nil if tree is empty.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/container/gtree"
	"github.com/gogf/gf/v2/util/gutil"
)

func main() {
	tree := gtree.NewRedBlackTree(gutil.ComparatorInt)
	for i := 1; i < 100; i++ {
		tree.Set(i, i)
	}
	fmt.Println(tree.Left().Key, tree.Left().Value)

	emptyTree := gtree.NewRedBlackTree(gutil.ComparatorInt)
	fmt.Println(emptyTree.Left())

}
Output:

1 1
<nil>

func (*RedBlackTree) Map

func (tree *RedBlackTree) Map() map[interface{}]interface{}

Map returns all key-value items as map.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/container/gtree"
	"github.com/gogf/gf/v2/util/gconv"
	"github.com/gogf/gf/v2/util/gutil"
)

func main() {
	tree := gtree.NewRedBlackTree(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]

func (*RedBlackTree) MapStrAny

func (tree *RedBlackTree) MapStrAny() map[string]interface{}

MapStrAny returns all key-value items as map[string]interface{}.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/container/gtree"
	"github.com/gogf/gf/v2/util/gconv"
	"github.com/gogf/gf/v2/util/gutil"
)

func main() {
	tree := gtree.NewRedBlackTree(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]

func (RedBlackTree) MarshalJSON

func (tree RedBlackTree) MarshalJSON() (jsonBytes []byte, err error)

MarshalJSON implements the interface MarshalJSON for json.Marshal.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/container/gtree"
	"github.com/gogf/gf/v2/internal/json"
	"github.com/gogf/gf/v2/util/gconv"
	"github.com/gogf/gf/v2/util/gutil"
)

func main() {
	tree := gtree.NewRedBlackTree(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"}

func (*RedBlackTree) Print

func (tree *RedBlackTree) Print()

Print prints the tree to stdout.

Example
package main

import (
	"github.com/gogf/gf/v2/container/gtree"
	"github.com/gogf/gf/v2/util/gconv"
	"github.com/gogf/gf/v2/util/gutil"
)

func main() {
	tree := gtree.NewRedBlackTree(gutil.ComparatorString)
	for i := 0; i < 6; i++ {
		tree.Set("key"+gconv.String(i), "val"+gconv.String(i))
	}

	tree.Print()

}
Output:

│           ┌── key5
│       ┌── key4
│   ┌── key3
│   │   └── key2
└── key1
    └── key0

func (*RedBlackTree) Remove

func (tree *RedBlackTree) Remove(key interface{}) (value interface{})

Remove removes the node from the tree by `key`.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/container/gtree"
	"github.com/gogf/gf/v2/util/gconv"
	"github.com/gogf/gf/v2/util/gutil"
)

func main() {
	tree := gtree.NewRedBlackTree(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]

func (*RedBlackTree) Removes

func (tree *RedBlackTree) Removes(keys []interface{})

Removes batch deletes values of the tree by `keys`.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/container/gtree"
	"github.com/gogf/gf/v2/util/gconv"
	"github.com/gogf/gf/v2/util/gutil"
)

func main() {
	tree := gtree.NewRedBlackTree(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]

func (*RedBlackTree) Replace

func (tree *RedBlackTree) Replace(data map[interface{}]interface{})

Replace the data of the tree with given `data`.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/container/gtree"
	"github.com/gogf/gf/v2/util/gconv"
	"github.com/gogf/gf/v2/util/gutil"
)

func main() {
	tree := gtree.NewRedBlackTree(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]

func (*RedBlackTree) Right

func (tree *RedBlackTree) Right() *RedBlackTreeNode

Right returns the right-most (max) node or nil if tree is empty.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/container/gtree"
	"github.com/gogf/gf/v2/util/gutil"
)

func main() {
	tree := gtree.NewRedBlackTree(gutil.ComparatorInt)
	for i := 1; i < 100; i++ {
		tree.Set(i, i)
	}
	fmt.Println(tree.Right().Key, tree.Right().Value)

	emptyTree := gtree.NewRedBlackTree(gutil.ComparatorInt)
	fmt.Println(emptyTree.Left())

}
Output:

99 99
<nil>

func (*RedBlackTree) Search

func (tree *RedBlackTree) Search(key interface{}) (value interface{}, found bool)

Search searches the tree with given `key`. Second return parameter `found` is true if key was found, otherwise false.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/container/gtree"
	"github.com/gogf/gf/v2/util/gconv"
	"github.com/gogf/gf/v2/util/gutil"
)

func main() {
	tree := gtree.NewRedBlackTree(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

func (*RedBlackTree) Set

func (tree *RedBlackTree) Set(key interface{}, value interface{})

Set inserts key-value item into the tree.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/container/gtree"
	"github.com/gogf/gf/v2/util/gconv"
	"github.com/gogf/gf/v2/util/gutil"
)

func main() {
	tree := gtree.NewRedBlackTree(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

func (*RedBlackTree) SetComparator

func (tree *RedBlackTree) SetComparator(comparator func(a, b interface{}) int)

SetComparator sets/changes the comparator for sorting.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/container/gtree"
	"github.com/gogf/gf/v2/util/gconv"
	"github.com/gogf/gf/v2/util/gutil"
)

func main() {
	var tree gtree.RedBlackTree
	tree.SetComparator(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

func (*RedBlackTree) SetIfNotExist

func (tree *RedBlackTree) SetIfNotExist(key interface{}, value interface{}) bool

SetIfNotExist sets `value` to the map if the `key` does not exist, and then returns true. It returns false if `key` exists, and `value` would be ignored.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/container/gtree"
	"github.com/gogf/gf/v2/util/gconv"
	"github.com/gogf/gf/v2/util/gutil"
)

func main() {
	tree := gtree.NewRedBlackTree(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

func (*RedBlackTree) SetIfNotExistFunc

func (tree *RedBlackTree) SetIfNotExistFunc(key interface{}, f func() interface{}) bool

SetIfNotExistFunc sets value with return value of callback function `f`, and then returns true. It returns false if `key` exists, and `value` would be ignored.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/container/gtree"
	"github.com/gogf/gf/v2/util/gconv"
	"github.com/gogf/gf/v2/util/gutil"
)

func main() {
	tree := gtree.NewRedBlackTree(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

func (*RedBlackTree) SetIfNotExistFuncLock

func (tree *RedBlackTree) SetIfNotExistFuncLock(key interface{}, f func() interface{}) bool

SetIfNotExistFuncLock sets value with return value of callback function `f`, and then returns true. It returns false if `key` exists, and `value` would be ignored.

SetIfNotExistFuncLock differs with SetIfNotExistFunc function is that it executes function `f` with mutex.Lock of the hash map.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/container/gtree"
	"github.com/gogf/gf/v2/util/gconv"
	"github.com/gogf/gf/v2/util/gutil"
)

func main() {
	tree := gtree.NewRedBlackTree(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

func (*RedBlackTree) Sets

func (tree *RedBlackTree) Sets(data map[interface{}]interface{})

Sets batch sets key-values to the tree.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/container/gtree"
	"github.com/gogf/gf/v2/util/gutil"
)

func main() {
	tree := gtree.NewRedBlackTree(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

func (*RedBlackTree) Size

func (tree *RedBlackTree) Size() int

Size returns number of nodes in the tree.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/container/gtree"
	"github.com/gogf/gf/v2/util/gconv"
	"github.com/gogf/gf/v2/util/gutil"
)

func main() {
	tree := gtree.NewRedBlackTree(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

func (*RedBlackTree) String

func (tree *RedBlackTree) String() string

String returns a string representation of container.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/container/gtree"
	"github.com/gogf/gf/v2/util/gconv"
	"github.com/gogf/gf/v2/util/gutil"
)

func main() {
	tree := gtree.NewRedBlackTree(gutil.ComparatorString)
	for i := 0; i < 6; i++ {
		tree.Set("key"+gconv.String(i), "val"+gconv.String(i))
	}

	fmt.Println(tree.String())

}
Output:

│           ┌── key5
│       ┌── key4
│   ┌── key3
│   │   └── key2
└── key1
    └── key0

func (*RedBlackTree) UnmarshalJSON

func (tree *RedBlackTree) UnmarshalJSON(b []byte) error

UnmarshalJSON implements the interface UnmarshalJSON for json.Unmarshal.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/container/gtree"
	"github.com/gogf/gf/v2/internal/json"
	"github.com/gogf/gf/v2/util/gconv"
	"github.com/gogf/gf/v2/util/gutil"
)

func main() {
	tree := gtree.NewRedBlackTree(gutil.ComparatorString)
	for i := 0; i < 6; i++ {
		tree.Set("key"+gconv.String(i), "val"+gconv.String(i))
	}
	bytes, err := json.Marshal(tree)

	otherTree := gtree.NewRedBlackTree(gutil.ComparatorString)
	err = json.Unmarshal(bytes, &otherTree)
	if err == nil {
		fmt.Println(otherTree.Map())
	}

}
Output:

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

func (*RedBlackTree) UnmarshalValue

func (tree *RedBlackTree) UnmarshalValue(value interface{}) (err error)

UnmarshalValue is an interface implement which sets any type of value for map.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/container/gtree"
	"github.com/gogf/gf/v2/util/gconv"
	"github.com/gogf/gf/v2/util/gutil"
)

func main() {
	tree := gtree.NewRedBlackTree(gutil.ComparatorString)

	type User struct {
		Uid   int
		Name  string
		Pass1 string `gconv:"password1"`
		Pass2 string `gconv:"password2"`
	}

	var (
		user = User{
			Uid:   1,
			Name:  "john",
			Pass1: "123",
			Pass2: "456",
		}
	)
	if err := gconv.Scan(user, tree); err == nil {
		fmt.Printf("%#v", tree.Map())
	}

}
Output:

map[interface {}]interface {}{"Name":"john", "Uid":1, "password1":"123", "password2":"456"}

func (*RedBlackTree) Values

func (tree *RedBlackTree) Values() []interface{}

Values returns all values in asc order based on the key.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/container/gtree"
	"github.com/gogf/gf/v2/util/gconv"
	"github.com/gogf/gf/v2/util/gutil"
)

func main() {
	tree := gtree.NewRedBlackTree(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]

type RedBlackTreeNode

type RedBlackTreeNode struct {
	Key   interface{}
	Value interface{}
	// contains filtered or unexported fields
}

RedBlackTreeNode is a single element within the tree.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL