Files
gf/container/gtree/gtree_avltree.go
Hunk Zhu 777d7aabb5 feat(container/gtree): add generic tree feature (#4522)
add generic tree feature
improve gmap.TreeMap

---------

Co-authored-by: hailaz <739476267@qq.com>
2025-11-29 21:09:43 +08:00

372 lines
12 KiB
Go

// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
//
// This Source Code Form is subject to the terms of the MIT License.
// If a copy of the MIT was not distributed with this file,
// You can obtain one at https://github.com/gogf/gf.
package gtree
import (
"sync"
"github.com/gogf/gf/v2/container/gvar"
"github.com/gogf/gf/v2/util/gutil"
)
var _ iTree = (*AVLTree)(nil)
// AVLTree holds elements of the AVL tree.
type AVLTree struct {
*AVLKVTree[any, any]
once sync.Once
}
// AVLTreeNode is a single element within the tree.
type AVLTreeNode = AVLKVTreeNode[any, any]
// 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.
func NewAVLTree(comparator func(v1, v2 any) int, safe ...bool) *AVLTree {
return &AVLTree{
AVLKVTree: NewAVLKVTree[any, any](comparator, safe...),
}
}
// 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.
func NewAVLTreeFrom(comparator func(v1, v2 any) int, data map[any]any, safe ...bool) *AVLTree {
return &AVLTree{
AVLKVTree: NewAVLKVTreeFrom(comparator, data, safe...),
}
}
// lazyInit lazily initializes the tree.
func (tree *AVLTree) lazyInit() {
tree.once.Do(func() {
if tree.AVLKVTree == nil {
tree.AVLKVTree = NewAVLKVTree[any, any](gutil.ComparatorTStr, false)
}
})
}
// Clone clones and returns a new tree from current tree.
func (tree *AVLTree) Clone() *AVLTree {
if tree == nil {
return nil
}
tree.lazyInit()
return &AVLTree{
AVLKVTree: tree.AVLKVTree.Clone(),
}
}
// Set sets key-value pair into the tree.
func (tree *AVLTree) Set(key any, value any) {
tree.lazyInit()
tree.AVLKVTree.Set(key, value)
}
// Sets batch sets key-values to the tree.
func (tree *AVLTree) Sets(data map[any]any) {
tree.lazyInit()
tree.AVLKVTree.Sets(data)
}
// SetIfNotExist sets `value` to the map if the `key` does not exist, and then returns true.
// It returns false if `key` exists, and such setting key-value pair operation would be ignored.
func (tree *AVLTree) SetIfNotExist(key any, value any) bool {
tree.lazyInit()
return tree.AVLKVTree.SetIfNotExist(key, value)
}
// SetIfNotExistFunc sets value with return value of callback function `f`, and then returns true.
// It returns false if `key` exists, and such setting key-value pair operation would be ignored.
func (tree *AVLTree) SetIfNotExistFunc(key any, f func() any) bool {
tree.lazyInit()
return tree.AVLKVTree.SetIfNotExistFunc(key, f)
}
// SetIfNotExistFuncLock sets value with return value of callback function `f`, and then returns true.
// It returns false if `key` exists, and such setting key-value pair operation would be ignored.
//
// SetIfNotExistFuncLock differs with SetIfNotExistFunc function is that
// it executes function `f` within mutex lock.
func (tree *AVLTree) SetIfNotExistFuncLock(key any, f func() any) bool {
tree.lazyInit()
return tree.AVLKVTree.SetIfNotExistFuncLock(key, f)
}
// Get searches the `key` in the tree and returns its associated `value` or nil if key is not found in tree.
//
// Note that, the `nil` value from Get function cannot be used to determine key existence, please use Contains function
// to do so.
func (tree *AVLTree) Get(key any) (value any) {
tree.lazyInit()
return tree.AVLKVTree.Get(key)
}
// GetOrSet returns its `value` of `key`, or sets value with given `value` if it does not exist and then returns
// this value.
func (tree *AVLTree) GetOrSet(key any, value any) any {
tree.lazyInit()
return tree.AVLKVTree.GetOrSet(key, value)
}
// GetOrSetFunc returns its `value` of `key`, or sets value with returned value of callback function `f` if it does not
// exist and then returns this value.
func (tree *AVLTree) GetOrSetFunc(key any, f func() any) any {
tree.lazyInit()
return tree.AVLKVTree.GetOrSetFunc(key, f)
}
// GetOrSetFuncLock returns its `value` of `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` within mutex lock.
func (tree *AVLTree) GetOrSetFuncLock(key any, f func() any) any {
tree.lazyInit()
return tree.AVLKVTree.GetOrSetFuncLock(key, f)
}
// GetVar returns a gvar.Var with the value by given `key`.
// Note that, the returned gvar.Var is un-concurrent safe.
//
// Also see function Get.
func (tree *AVLTree) GetVar(key any) *gvar.Var {
tree.lazyInit()
return tree.AVLKVTree.GetVar(key)
}
// GetVarOrSet returns a gvar.Var with result from GetVarOrSet.
// Note that, the returned gvar.Var is un-concurrent safe.
//
// Also see function GetOrSet.
func (tree *AVLTree) GetVarOrSet(key any, value any) *gvar.Var {
tree.lazyInit()
return tree.AVLKVTree.GetVarOrSet(key, value)
}
// GetVarOrSetFunc returns a gvar.Var with result from GetOrSetFunc.
// Note that, the returned gvar.Var is un-concurrent safe.
//
// Also see function GetOrSetFunc.
func (tree *AVLTree) GetVarOrSetFunc(key any, f func() any) *gvar.Var {
tree.lazyInit()
return tree.AVLKVTree.GetVarOrSetFunc(key, f)
}
// GetVarOrSetFuncLock returns a gvar.Var with result from GetOrSetFuncLock.
// Note that, the returned gvar.Var is un-concurrent safe.
//
// Also see function GetOrSetFuncLock.
func (tree *AVLTree) GetVarOrSetFuncLock(key any, f func() any) *gvar.Var {
tree.lazyInit()
return tree.AVLKVTree.GetVarOrSetFuncLock(key, f)
}
// Search searches the tree with given `key`.
// Second return parameter `found` is true if key was found, otherwise false.
func (tree *AVLTree) Search(key any) (value any, found bool) {
tree.lazyInit()
return tree.AVLKVTree.Search(key)
}
// Contains checks and returns whether given `key` exists in the tree.
func (tree *AVLTree) Contains(key any) bool {
tree.lazyInit()
return tree.AVLKVTree.Contains(key)
}
// Size returns number of nodes in the tree.
func (tree *AVLTree) Size() int {
tree.lazyInit()
return tree.AVLKVTree.Size()
}
// IsEmpty returns true if the tree does not contain any nodes.
func (tree *AVLTree) IsEmpty() bool {
tree.lazyInit()
return tree.AVLKVTree.IsEmpty()
}
// Remove removes the node from the tree by `key`, and returns its associated value of `key`.
// The given `key` should adhere to the comparator's type assertion, otherwise method panics.
func (tree *AVLTree) Remove(key any) (value any) {
tree.lazyInit()
return tree.AVLKVTree.Remove(key)
}
// Removes batch deletes key-value pairs from the tree by `keys`.
func (tree *AVLTree) Removes(keys []any) {
tree.lazyInit()
tree.AVLKVTree.Removes(keys)
}
// Clear removes all nodes from the tree.
func (tree *AVLTree) Clear() {
tree.lazyInit()
tree.AVLKVTree.Clear()
}
// Keys returns all keys from the tree in order by its comparator.
func (tree *AVLTree) Keys() []any {
tree.lazyInit()
return tree.AVLKVTree.Keys()
}
// Values returns all values from the true in order by its comparator based on the key.
func (tree *AVLTree) Values() []any {
tree.lazyInit()
return tree.AVLKVTree.Values()
}
// Replace clears the data of the tree and sets the nodes by given `data`.
func (tree *AVLTree) Replace(data map[any]any) {
tree.lazyInit()
tree.AVLKVTree.Replace(data)
}
// Print prints the tree to stdout.
func (tree *AVLTree) Print() {
tree.lazyInit()
tree.AVLKVTree.Print()
}
// String returns a string representation of container.
func (tree *AVLTree) String() string {
tree.lazyInit()
return tree.AVLKVTree.String()
}
// MarshalJSON implements the interface MarshalJSON for json.Marshal.
func (tree *AVLTree) MarshalJSON() (jsonBytes []byte, err error) {
tree.lazyInit()
return tree.AVLKVTree.MarshalJSON()
}
// Map returns all key-value pairs as map.
func (tree *AVLTree) Map() map[any]any {
tree.lazyInit()
return tree.AVLKVTree.Map()
}
// MapStrAny returns all key-value items as map[string]any.
func (tree *AVLTree) MapStrAny() map[string]any {
tree.lazyInit()
return tree.AVLKVTree.MapStrAny()
}
// Iterator is alias of IteratorAsc.
//
// Also see IteratorAsc.
func (tree *AVLTree) Iterator(f func(key, value any) bool) {
tree.IteratorAsc(f)
}
// IteratorFrom is alias of IteratorAscFrom.
//
// Also see IteratorAscFrom.
func (tree *AVLTree) IteratorFrom(key any, match bool, f func(key, value any) bool) {
tree.IteratorAscFrom(key, match, f)
}
// IteratorAsc iterates the tree readonly in ascending order with given callback function `f`.
// If callback function `f` returns true, then it continues iterating; or false to stop.
func (tree *AVLTree) IteratorAsc(f func(key, value any) bool) {
tree.lazyInit()
tree.AVLKVTree.IteratorAsc(f)
}
// IteratorAscFrom iterates the tree readonly in ascending order with given callback function `f`.
//
// The parameter `key` specifies the start entry for iterating.
// The parameter `match` specifies whether starting iterating only if the `key` is fully matched, or else using index
// searching iterating.
// If callback function `f` returns true, then it continues iterating; or false to stop.
func (tree *AVLTree) IteratorAscFrom(key any, match bool, f func(key, value any) bool) {
tree.lazyInit()
tree.AVLKVTree.IteratorAscFrom(key, match, f)
}
// IteratorDesc iterates the tree readonly in descending order with given callback function `f`.
//
// If callback function `f` returns true, then it continues iterating; or false to stop.
func (tree *AVLTree) IteratorDesc(f func(key, value any) bool) {
tree.lazyInit()
tree.AVLKVTree.IteratorDesc(f)
}
// IteratorDescFrom iterates the tree readonly in descending order with given callback function `f`.
//
// The parameter `key` specifies the start entry for iterating.
// The parameter `match` specifies whether starting iterating only if the `key` is fully matched, or else using index
// searching iterating.
// If callback function `f` returns true, then it continues iterating; or false to stop.
func (tree *AVLTree) IteratorDescFrom(key any, match bool, f func(key, value any) bool) {
tree.lazyInit()
tree.AVLKVTree.IteratorDescFrom(key, match, f)
}
// Left returns the minimum element corresponding to the comparator of the tree or nil if the tree is empty.
func (tree *AVLTree) Left() *AVLTreeNode {
tree.lazyInit()
return tree.AVLKVTree.Left()
}
// Right returns the maximum element corresponding to the comparator of the tree or nil if the tree is empty.
func (tree *AVLTree) Right() *AVLTreeNode {
tree.lazyInit()
return tree.AVLKVTree.Right()
}
// Floor Finds floor node of the input key, returns the floor node or nil if no floor node is found.
// The second returned parameter `found` 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.
func (tree *AVLTree) Floor(key any) (floor *AVLTreeNode, found bool) {
tree.lazyInit()
return tree.AVLKVTree.Floor(key)
}
// Ceiling finds ceiling node of the input key, returns the ceiling node or nil if no ceiling node is found.
// The second return parameter `found` 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.
func (tree *AVLTree) Ceiling(key any) (ceiling *AVLTreeNode, found bool) {
tree.lazyInit()
return tree.AVLKVTree.Ceiling(key)
}
// 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`.
func (tree *AVLTree) Flip(comparator ...func(v1, v2 any) int) {
tree.lazyInit()
var t = new(AVLTree)
if len(comparator) > 0 {
t = NewAVLTree(comparator[0], tree.mu.IsSafe())
} else {
t = NewAVLTree(tree.comparator, tree.mu.IsSafe())
}
tree.IteratorAsc(func(key, value any) bool {
t.doSet(value, key)
return true
})
tree.Clear()
tree.Sets(t.Map())
}