rbtree

package
v1.2.0 Latest Latest
Warning

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

Go to latest
Published: Apr 19, 2023 License: MIT Imports: 5 Imported by: 0

Documentation

Index

Constants

View Source
const (
	RED   = false
	BLACK = true
)

Define node 's colors

Variables

View Source
var ErrorNotFound = errors.New("not found")

Functions

This section is empty.

Types

type Color

type Color bool

Color defines node color type

type Node

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

Node is a tree node

func (*Node[K, V]) Key

func (n *Node[K, V]) Key() K

Key returns node's key

func (*Node[K, V]) Next

func (n *Node[K, V]) Next() *Node[K, V]

Next returns the Node's successor as an iterator.

func (*Node[K, V]) Prev

func (n *Node[K, V]) Prev() *Node[K, V]

Prev returns the Node's predecessor as an iterator.

func (*Node[K, V]) SetValue

func (n *Node[K, V]) SetValue(val V)

SetValue sets node's value

func (*Node[K, V]) Value

func (n *Node[K, V]) Value() V

Value returns node's value

type RbTree

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

RbTree is a kind of self-balancing binary search tree in computer science. Each node of the binary tree has an extra bit, and that bit is often interpreted as the color (red or black) of the node. These color bits are used to ensure the tree remains approximately balanced during insertions and deletions.

func New

func New[K, V any](cmp comparator.Comparator[K]) *RbTree[K, V]

New creates a new RbTree

func (*RbTree[K, V]) Begin

func (t *RbTree[K, V]) Begin() *Node[K, V]

Begin returns the node with minimum key in the RbTree

func (*RbTree[K, V]) Clear

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

Clear clears the RbTree

func (*RbTree[K, V]) Delete

func (t *RbTree[K, V]) Delete(node *Node[K, V])

Delete deletes node from the RbTree

func (*RbTree[K, V]) Empty

func (t *RbTree[K, V]) Empty() bool

Empty returns true if Tree is empty,otherwise returns false.

func (*RbTree[K, V]) Find

func (t *RbTree[K, V]) Find(key K) (V, error)

Find finds the first node that the key is equal to the passed key, and returns its value

func (*RbTree[K, V]) FindLowerBoundNode

func (t *RbTree[K, V]) FindLowerBoundNode(key K) *Node[K, V]

FindLowerBoundNode finds the first node that its key is equal or greater than the passed key, and returns it

func (*RbTree[K, V]) FindNode

func (t *RbTree[K, V]) FindNode(key K) *Node[K, V]

FindNode the first node that the key is equal to the passed key and return it

func (*RbTree[K, V]) FindUpperBoundNode added in v1.1.0

func (t *RbTree[K, V]) FindUpperBoundNode(key K) *Node[K, V]

FindUpperBoundNode finds the first node that its key is greater than the passed key, and returns it

func (*RbTree[K, V]) First

func (t *RbTree[K, V]) First() *Node[K, V]

First returns the node with minimum key in the RbTree

func (*RbTree[K, V]) Insert

func (t *RbTree[K, V]) Insert(key K, value V)

Insert inserts a key-value pair into the RbTree.

func (*RbTree[K, V]) IsRbTree

func (t *RbTree[K, V]) IsRbTree() (bool, error)

IsRbTree is a function use to test whether t is a RbTree or not

func (*RbTree[K, V]) IterFirst

func (t *RbTree[K, V]) IterFirst() *RbTreeIterator[K, V]

IterFirst returns the iterator of first node

func (*RbTree[K, V]) IterLast

func (t *RbTree[K, V]) IterLast() *RbTreeIterator[K, V]

IterLast returns the iterator of first node

func (*RbTree[K, V]) Last

func (t *RbTree[K, V]) Last() *Node[K, V]

Last returns the Node with maximum key in the RbTree

func (*RbTree[K, V]) RBegin

func (t *RbTree[K, V]) RBegin() *Node[K, V]

RBegin returns the Node with maximum key in the RbTree

func (*RbTree[K, V]) Size

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

Size returns the size of the rbtree.

func (*RbTree[K, V]) Traversal

func (t *RbTree[K, V]) Traversal(visitor visitor.KvVisitor[K, V])

Traversal traversals elements in the RbTree, it will not stop until to the end of RbTree or the visitor returns false

type RbTreeIterator

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

RbTreeIterator is an iterator implementation of RbTree

func NewIterator

func NewIterator[K, V any](node *Node[K, V]) *RbTreeIterator[K, V]

NewIterator creates a RbTreeIterator from the passed node

func (*RbTreeIterator[K, V]) Clone

func (iter *RbTreeIterator[K, V]) Clone() iterator.ConstIterator[V]

Clone clones the iterator into a new RbTreeIterator

func (*RbTreeIterator[K, V]) Equal

func (iter *RbTreeIterator[K, V]) Equal(other iterator.ConstIterator[V]) bool

Equal returns true if the iterator is equal to the passed iterator

func (*RbTreeIterator[K, V]) IsValid

func (iter *RbTreeIterator[K, V]) IsValid() bool

IsValid returns true if the iterator is valid, otherwise returns false

func (*RbTreeIterator[K, V]) Key

func (iter *RbTreeIterator[K, V]) Key() K

Key returns the node's key of the iterator point to

func (*RbTreeIterator[K, V]) Next

func (iter *RbTreeIterator[K, V]) Next() iterator.ConstIterator[V]

Next moves the pointer of the iterator to the next node, and returns itself

func (*RbTreeIterator[K, V]) Prev

func (iter *RbTreeIterator[K, V]) Prev() iterator.ConstBidIterator[V]

Prev moves the pointer of the iterator to the previous node, and returns itself

func (*RbTreeIterator[K, V]) SetValue

func (iter *RbTreeIterator[K, V]) SetValue(val V) error

SetValue sets the node's value of the iterator point to

func (*RbTreeIterator[K, V]) Value

func (iter *RbTreeIterator[K, V]) Value() V

Value returns the node's value of the iterator point to

Jump to

Keyboard shortcuts

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