splaytree

package
v0.9.1 Latest Latest
Warning

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

Go to latest
Published: Oct 5, 2025 License: Apache-2.0 Imports: 6 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Node

type Node[K comparable, V any] struct {
	Key      K
	Value    V
	Parent   *Node[K, V]
	Children [2]*Node[K, V]
}

func (*Node[K, V]) String

func (n *Node[K, V]) String() string

type Option

type Option[K comparable, V any] func(*Tree[K, V]) error

Option is a functional option type for configuring a splay tree.

func WithNodeFormat

func WithNodeFormat[K comparable, V any](format string) Option[K, V]

WithNodeFormat creates a option that sets the node format when call tree.String(). Default node format is fmt.Sprintf("%v", n.Key). The format must contains two placeholders, the first is the key and the second is the value. For example: "%d:%s"

func WithSafe

func WithSafe[K comparable, V any]() Option[K, V]

WithSafe creates a option that make the splay tree safe for concurrent use.

type Tree

type Tree[K comparable, V any] struct {
	// contains filtered or unexported fields
}

Tree represents a generic splay tree. It support keys of any comparable type and value of any type. The tree use a custom comparison function to matain order.

func New

func New[K comparable, V any](cmp func(K, K) int, ops ...Option[K, V]) (*Tree[K, V], error)

New creates and returns a splay tree. The provided function "cmp" is determines the order of the keys.

func NewFromMap

func NewFromMap[K comparable, V any](m map[K]V, cmp func(K, K) int, ops ...Option[K, V]) (*Tree[K, V], error)

NewFromMap creates and returns a splay tree from a given map. The provided function "cmp" is determines the order of the keys.

func NewFromOrderedMap

func NewFromOrderedMap[K cmp.Ordered, V any](m map[K]V, ops ...Option[K, V]) (*Tree[K, V], error)

NewFromOrderedMap creates and returns a splay tree from a given map. It uses cmp.Compare[K] as the default comparison function, which is suitable for types that implement the cmp.Ordered interface, such as int, float64, and string.

func NewFromSlice

func NewFromSlice[V any](slice []V, ops ...Option[int, V]) (*Tree[int, V], error)

NewFromSlice creates and returns a splay tree from a given slice. It use the cmp.Compare[K] as the default comparsion function.

func NewOrderedKeys

func NewOrderedKeys[K cmp.Ordered, V any](ops ...Option[K, V]) (*Tree[K, V], error)

NewOrderedKeys creates and returns a splay tree. It use the cmp.Compare[K] as the default comparsion function. This is suitable for types that implement the cmp.Ordered interface, such as int, float64 and string

func (*Tree[K, V]) Ceiling

func (t *Tree[K, V]) Ceiling(key K) (K, V, bool)

Ceiling returns the smallest key in the tree that is greater than or equal to the given key. If no such key exists, returns zero values and false. If the exact key exists, returns that key-value pair and true.

This operation modifies the tree structure due to splaying if found.

func (*Tree[K, V]) Clear

func (t *Tree[K, V]) Clear()

Clear removes all elements from the tree.

func (*Tree[K, V]) Delete

func (t *Tree[K, V]) Delete(key K) (V, bool)

Delete removes the node with the given key from the tree. It returns the value associated with the key and true if found, otherwise returns the zero value and false. The operation maintains the BST property by promoting the maximum node of the left subtree when deleting a node with two children.

func (*Tree[K, V]) Floor

func (t *Tree[K, V]) Floor(key K) (K, V, bool)

Floor returns the largest key in the tree that is less than or equal to the given key. If no such key exists, returns zero values and false. If the exact key exists, returns that key-value pair and true.

This operation modifies the tree structure due to splaying if found.

func (*Tree[K, V]) Get

func (t *Tree[K, V]) Get(key K) (V, bool)

Get retrieves the value associated with the given key. It returns the value and true if the key exists, otherwise returns the zero value and false. The accessed node will be splayed to the root if found.

func (*Tree[K, V]) InOrder

func (t *Tree[K, V]) InOrder(fn func(key K, value V) bool)

InOrder traverses the tree in-order (left-root-right) fashion, calling the provided function for each node. If the function returns false, the traversal is stopped. The traversal visits nodes in ascending order of keys.

Unlike Get/Put operations, InOrder does not perform any splay operations.

func (*Tree[K, V]) IsEmpty

func (t *Tree[K, V]) IsEmpty() bool

IsEmpty returns true if the tree contains no elements, false otherwise.

func (*Tree[K, V]) Keys

func (t *Tree[K, V]) Keys() []K

Keys returns a sorted slice of all keys in the tree.

Unlink Get/Put operations, this operation does not perform any splay operations.

func (*Tree[K, V]) LevelOrder

func (t *Tree[K, V]) LevelOrder(fn func(key K, value V) bool)

LevelOrder traverses the tree in level-order (breadth-first) fashion, calling the provided function for each node. If the function returns false, the traversal is stopped.

Unlike Get/Put operations, LevelOrder does not perform any splay operations.

func (*Tree[K, V]) MarshalJSON

func (t *Tree[K, V]) MarshalJSON() ([]byte, error)

MarshalJSON implements the json.Marshaler interface.

func (*Tree[K, V]) Max

func (t *Tree[K, V]) Max() (K, V, bool)

Max returns the maximum key in the tree and its associated value. If the tree is empty, it returns the zero values for K and V, and false. Otherwise returns the key-value pair and true.

This operation modifies the tree structure due to splaying if found.

func (*Tree[K, V]) Min

func (t *Tree[K, V]) Min() (K, V, bool)

Min returns the minimum key in the tree and its associated value. If the tree is empty, it returns the zero values for K and V, and false. Otherwise returns the key-value pair and true.

This operation modifies the tree structure due to splaying if found.

func (*Tree[K, V]) PostOrder

func (t *Tree[K, V]) PostOrder(fn func(key K, value V) bool)

PostOrder traverses the tree in post-order (left-right-root) fashion, calling the provided function for each node. If the function returns false, the traversal is stopped.

Unlike Get/Put operations, PostOrder does not perform any splay operations.

func (*Tree[K, V]) PreOrder

func (t *Tree[K, V]) PreOrder(fn func(key K, value V) bool)

PreOrder traverses the tree in pre-order (root-left-right) fashion, calling the provided function for each node. If the function returns false, the traversal is stopped.

Unlike Get/Put operations, PreOrder does not perform any splay operations.

func (*Tree[K, V]) Put

func (t *Tree[K, V]) Put(key K, val V)

Put inserts or updates a key-value pair in the splay tree

func (*Tree[K, V]) Size

func (t *Tree[K, V]) Size() int

Size returns the current number of nodes in the tree.

func (*Tree[K, V]) String

func (t *Tree[K, V]) String() string

String returns a string representation of the tree.

func (*Tree[K, V]) UnmarshalJSON

func (t *Tree[K, V]) UnmarshalJSON(data []byte) error

UnmarshalJSON implements the json.Unmarshaler interface.

func (*Tree[K, V]) Values

func (t *Tree[K, V]) Values() []V

Values returns a slice of all values in the tree.

Unlike Get/Put operations, this operation does not perform any splay operations.

Jump to

Keyboard shortcuts

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