rbtree

package
v0.0.0-...-27647ab Latest Latest
Warning

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

Go to latest
Published: Oct 17, 2023 License: BSD-3-Clause Imports: 2 Imported by: 0

Documentation

Overview

The MIT License (MIT)

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the Software), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED AS IS, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Package rbtree provides an iterative (not recursive) red-black tree with obvious semantics and powerful, resettable iteration.

This package provides the easy ability to modify nodes on the fly, but note that this comes with the easy footgun of messing up iteration. If you modify a tree during iteration, you will likely need to reset your iterator. The node under the iterator may no longer be where you expect.

For more information about a red-black tree, and to understand the implementation, see Wikipedia:

https://en.wikipedia.org/wiki/Red%E2%80%93black_tree

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type RbIter

type RbIter struct {
	// contains filtered or unexported fields
}

Iter iterates over a tree. This returns nodes in the tree to support easy node deletion. Note that if you modify the tree during iteration, you need to reset the iterator or stop iterating.

func RbIterAt

func RbIterAt(on *RbNode) RbIter

IterAt returns an iterator for a tree starting on the given node. Note that modifications to the tree during iteration may invalidate the iterator and the iterator may need resetting.

func (RbIter) Item

func (i RbIter) Item() any

func (*RbIter) Left

func (i *RbIter) Left() *RbNode

func (RbIter) Node

func (i RbIter) Node() *RbNode

func (RbIter) Ok

func (i RbIter) Ok() bool

func (RbIter) PeekLeft

func (i RbIter) PeekLeft() *RbNode

PeekLeft peeks at the next left node the iterator can move onto without moving the iterator. This will panic if not on a node. For maximal efficiency, to move left after a left peek, use Reset with the peeked node.

func (RbIter) PeekRight

func (i RbIter) PeekRight() *RbNode

PeekRight peeks at the next right node the iterator can move onto without moving the iterator. This will panic if not on a node. For maximal efficiency, to move right after a right peek, use Reset with the peeked node.

func (*RbIter) Reset

func (i *RbIter) Reset(on *RbNode)

func (*RbIter) Right

func (i *RbIter) Right() *RbNode

type RbNode

type RbNode struct {
	Item any
	// contains filtered or unexported fields
}

func RbInto

func RbInto(n *RbNode) *RbNode

Into returns a fake node that to both the Left or the Right will be the given node. This can be used in combination with iterating to reset iteration to the given node.

type RbTree

type RbTree struct {
	Less bt.LessImpl[any]
	// contains filtered or unexported fields
}

func (*RbTree) Clear

func (t *RbTree) Clear()

func (*RbTree) Delete

func (t *RbTree) Delete(n *RbNode)

func (*RbTree) Find

func (t *RbTree) Find(needle any) *RbNode

Find finds a node in the tree equal to the needle, if any.

func (*RbTree) FindOrInsert

func (t *RbTree) FindOrInsert(needle any) *RbNode

FindOrInsert finds a node equal to the needle, if any. If the node does not exist, this inserts a new node into the tree with new and returns that node.

func (*RbTree) FindWith

func (t *RbTree) FindWith(cmp func(*RbNode) int) *RbNode

FindWith calls cmp to find a node in the tree. To continue searching left, cmp must return negative. To continue searching right, cmp must return positive. To stop at the node containing the current item to cmp, return zero. If cmp never returns zero, this returns nil. This can be used to find an arbitrary node meeting a condition.

func (*RbTree) FindWithOrInsertWith

func (t *RbTree) FindWithOrInsertWith(find func(*RbNode) int, insert func() any) *RbNode

FindWithOrInsertWith calls cmp to find a node in the tree following the semantics described in FindWith. If the cmp function never returns zero, this inserts a new node into the tree with new and returns that node.

func (*RbTree) Fix

func (t *RbTree) Fix(n *RbNode) *RbNode

Fix removes a node from the tree and reinserts it. This can be used to "fix" a node after the item has been updated, removing some minor garbage. This returns the updated node. This is shorthand for t.Reinsert(t.Delete(n)).

func (*RbTree) Insert

func (t *RbTree) Insert(i any) *RbNode

Insert inserts an item into the tree, returning the new node.

func (*RbTree) Len

func (t *RbTree) Len() int

func (*RbTree) Max

func (t *RbTree) Max() *RbNode

func (*RbTree) Min

func (t *RbTree) Min() *RbNode

func (*RbTree) Reinsert

func (t *RbTree) Reinsert(n *RbNode)

Reinsert inserts a node into the tree. This function is provided to aid in garbage reduction / ease of use when deleting, updating, and reinserting items into a tree. The only value required in the node is the item. All other fields are set inside this function.

Jump to

Keyboard shortcuts

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