rbtree

package
v0.8.0 Latest Latest
Warning

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

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

Documentation

Index

Constants

View Source
const (
	Reset   = "\033[0m"
	RedTxt  = "\033[31m"
	BlkTxt  = "\033[30m"
	RedBg   = "\033[41;37m"
	BlackBg = "\033[40;37m"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Node

type Node[K comparable, V any] struct {
	Key   K
	Value V

	Left   *Node[K, V]
	Right  *Node[K, V]
	Parent *Node[K, V]
	// contains filtered or unexported fields
}

Node represents a node in red-black tree.

func (*Node[K, V]) Color

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

Color returns a string representation of the node's color.

func (*Node[K, V]) Size

func (n *Node[K, V]) Size() int

Size returns the number of elements in the subtree rooted at n.

func (*Node[K, V]) String

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

String returns a string representation of the node's key.

type Option

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

Option is a functional option type for configuring a red-black tree.

func WithColorfulString

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

WithColorfulString creates a option that make the red-black tree colorful output.

func WithNodeFormatter

func WithNodeFormatter[K comparable, V any](fn func(K, V) string) Option[K, V]

WithNodeFormatter creates a option that sets the node formatter when call tree.String(). Example usage:

tree.WithNodeFormatter(func(k string, v int) string {
	return fmt.Sprintf("%s:%d ", k, v)
})

func WithSafe

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

WithSafe creates a option that make the red-black tree safe for concurrent use.

type Tree

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

Tree represents a generic red-black 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 red-black 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 red-black 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 red-black 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 red-black 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 red-black 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]) BlackCount

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

func (*Tree[K, V]) Ceiling

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

Ceiling returns the smallest node with a key greater than or equal to the given key. If such a node exists, it is returned along with true; otherwise, nil and false are returned.

func (*Tree[K, V]) Clear

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

Clear clears 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.

func (*Tree[K, V]) Floor

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

Floor returns the largest node with a key less than or equal to the given key. If such a node exists, it is returned along with true; otherwise, nil and false are returned.

func (*Tree[K, V]) Get

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

Get returns the value associated with the given key.

func (*Tree[K, V]) InOrder

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

InOrder call function "fn" on each node in inorder traversal order. The traversal follows: left subtree → node → right subtree,

func (*Tree[K, V]) IsEmpty

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

IsEmpty reports whether the tree is empty.

func (*Tree[K, V]) Keys

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

Keys returns a slice containing all keys in sorted order.

func (*Tree[K, V]) LeafCount

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

func (*Tree[K, V]) LevelOrder

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

LevelOrder call function "fn" on each node in levelorder traversal order

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 node in the tree. If the tree is empty, it returns the nil.

func (*Tree[K, V]) MaxDepth

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

func (*Tree[K, V]) Min

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

Min returns the minimum node in the tree. If the tree is empty, it returns the nil.

func (*Tree[K, V]) MinDepth

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

func (*Tree[K, V]) PostOrder

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

PostOrder call function "fn" on each node in postorder traversal order. The traversal follows: left subtree → right subtree → node

func (*Tree[K, V]) PreOrder

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

PreOrder call function "fn" on each node in preorder traversal order. The traversal starts from the root and follows: node → left subtree → right subtree

func (*Tree[K, V]) Put

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

Put inserts a key-value pair into the tree. if the key already exists, its value will be updated.

func (*Tree[K, V]) RedCount

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

func (*Tree[K, V]) Size

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

Size returns the number of nodes in the tree.

func (*Tree[K, V]) String

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

String returns a visual representation of the Red-Black Tree. If t.color is true, nodes will be displayed in terminal colors.

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 containing all values in sorted order.

Jump to

Keyboard shortcuts

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