Documentation ¶
Overview ¶
The MIT License (MIT)
Copyright (c) 2019 Travis Bischel ¶
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 ¶
- type RbIter
- type RbNode
- type RbTree
- func (t *RbTree) Clear()
- func (t *RbTree) Delete(n *RbNode)
- func (t *RbTree) Find(needle any) *RbNode
- func (t *RbTree) FindOrInsert(needle any) *RbNode
- func (t *RbTree) FindWith(cmp func(*RbNode) int) *RbNode
- func (t *RbTree) FindWithOrInsertWith(find func(*RbNode) int, insert func() any) *RbNode
- func (t *RbTree) Fix(n *RbNode) *RbNode
- func (t *RbTree) Insert(i any) *RbNode
- func (t *RbTree) Len() int
- func (t *RbTree) Max() *RbNode
- func (t *RbTree) Min() *RbNode
- func (t *RbTree) Reinsert(n *RbNode)
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 ¶
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) PeekLeft ¶
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.
type RbTree ¶
func (*RbTree) FindOrInsert ¶
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 ¶
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 ¶
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 ¶
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)).