Documentation
¶
Index ¶
- Constants
- func Ordered[T constraints.Ordered](value T) ordered[T]
- type Node
- type Orderable
- type RuneSet
- type Tree
- func (t *Tree[V, T]) Delete(v V) (success bool)
- func (t *Tree[V, T]) DeleteMin()
- func (t *Tree[V, T]) GetTreeLevels() [][]*Node[V, T]
- func (t *Tree[V, T]) Height() int
- func (t *Tree[V, T]) Insert(item T) error
- func (t *Tree[V, T]) Len() int
- func (t *Tree[V, T]) Max() V
- func (t *Tree[V, T]) Min() V
- func (t *Tree[V, T]) Search(k V) (bool, V)
- func (t *Tree[V, T]) SearchLower(k V) (V, error)
- func (t *Tree[V, T]) SearchUpper(k V) (V, error)
- func (t Tree[V, T]) Sorted() iter.Seq[V]
- func (t Tree[V, T]) String() string
- func (t *Tree[V, T]) ToSortedSlice() []V
- func (t *Tree[V, T]) Walk(f func(*Node[V, T]) bool, order WalkOrder)
- type WalkOrder
Constants ¶
const ( KeyExistsError = keyError("Key already exists in tree.") KeyDoesNotExistError = keyError("Key not found.") )
Variables ¶
This section is empty.
Functions ¶
func Ordered ¶
func Ordered[T constraints.Ordered](value T) ordered[T]
Types ¶
type Tree ¶
func NewTree ¶
Creates a new red-black tree from a slice of Orderable items. If ignore_duplicates is true, duplicate items will be ignored otherwise a KeyExistsError will be returned.
func (*Tree[V, T]) Delete ¶
Delete removes a node from the tree if the key is found. Returns false if the key is not found.
func (*Tree[V, T]) DeleteMin ¶
func (t *Tree[V, T]) DeleteMin()
DeleteMin removes the node with the smallest key from the tree.
func (*Tree[V, T]) GetTreeLevels ¶
Returns each level of the tree as a slice of nodes. Ordered from root to leaves.
func (*Tree[V, T]) Height ¶
Height return the height of the tree. The height of a tree is the number of edges on the longest path between the root and a leaf.
func (*Tree[V, T]) Insert ¶
Insert adds a new node to the tree if the item is not a duplicate of another item in the tree. Returns KeyExistsError if the key already exists in the tree.
func (*Tree[V, T]) Search ¶
Search returns true if the key is found in the tree and the value of the key. If the key is not found, the second return value is the key itself.
func (*Tree[V, T]) SearchLower ¶
SearchLower returns the value of the largest key in the tree that is less than or equal to the given key. Returns KeyDoesNotExistError if k < i for all i in the tree.
func (*Tree[V, T]) SearchUpper ¶
SearchUpper returns the value of the smallest key in the tree that is greater than or equal to the given key. Returns KeyDoesNotExistError if k > i for all i in the tree.
func (*Tree[V, T]) ToSortedSlice ¶
func (t *Tree[V, T]) ToSortedSlice() []V
Returns a sorted slice of the keys in the tree.
type WalkOrder ¶
type WalkOrder int
WalkOrder specifies the order in which the nodes are visited when walking the tree.
const ( // INORDER visits all left children (smaller keys), then the current node, then all right children (larger keys). // This results in keys being visited in ascending order. INORDER WalkOrder = iota // PREORDER visits the current node before its child nodes (left child before right child). PREORDER // POSTORDER visits the current node after its child nodes (left child before right child). POSTORDER // LEVELORDER visits the nodes level by level from left to right. LEVELORDER )