Documentation
¶
Overview ¶
Package tree provides an implementation of a red-black tree.
Index ¶
- type Bytes
- type Int
- type Item
- type RedBlackTree
- func (t *RedBlackTree) Ascend(fn func(Item) bool)
- func (t *RedBlackTree) AscendGreaterOrEqual(than Item, fn func(Item) bool)
- func (t *RedBlackTree) AscendLess(thanItem Item, fn func(Item) bool)
- func (t *RedBlackTree) AscendRange(greaterOrEqual, lessThan Item, fn func(Item) bool)
- func (t *RedBlackTree) Delete(item Item) Item
- func (t *RedBlackTree) DeleteMax() Item
- func (t *RedBlackTree) DeleteMin() Item
- func (t *RedBlackTree) Descend(fn func(Item) bool)
- func (t *RedBlackTree) Exists(item Item) bool
- func (t *RedBlackTree) Get(item Item) Item
- func (t *RedBlackTree) Max() Item
- func (t *RedBlackTree) Min() Item
- func (t *RedBlackTree) Size() int
- func (t *RedBlackTree) Upsert(item Item) Item
- type String
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Bytes ¶
type Bytes []byte
Bytes represents a slice of bytes that implements the Item interface.
type Item ¶
Item is the interface that wraps the Less method.
Less should return 'true' if the instance is "less than" the provided Item. Items are considered equal if neither are less than each other. E.g. Items 'a' & 'b' are considered equal if: (!a.Less(b) && !b.Less(a))
type RedBlackTree ¶
type RedBlackTree struct {
// contains filtered or unexported fields
}
RedBlackTree is an in-memory implementation of a red-black tree.
The internal data structure will automatically re-balance, and therefore allow for O(log(n)) retrieval, insertion, and deletion.
Note: While read-only operations may occur concurrently, any write operation must be serially executed (typically protected with a mutex).
func (*RedBlackTree) Ascend ¶
func (t *RedBlackTree) Ascend(fn func(Item) bool)
Ascend starts at the first Item and calls 'fn' for each Item until no Items remain or fn returns 'false'.
O(log(n) + m) where n is the total number of items in the tree and m is the number of items ranged over.
func (*RedBlackTree) AscendGreaterOrEqual ¶
func (t *RedBlackTree) AscendGreaterOrEqual(than Item, fn func(Item) bool)
AscendGreaterOrEqual starts at the first Item greater or equal to the provided Item and calls 'fn' for each Item until no Items remain in the tree or fn returns 'false'.
O(log(n) + m) where n is the total number of items in the tree and m is the number of items ranged over.
func (*RedBlackTree) AscendLess ¶
func (t *RedBlackTree) AscendLess(thanItem Item, fn func(Item) bool)
AscendLess starts at the first Item and calls 'fn' for each Item less than the provided Item or when fn returns 'false'.
O(log(n) + m) where n is the total number of items in the tree and m is the number of items ranged over.
func (*RedBlackTree) AscendRange ¶
func (t *RedBlackTree) AscendRange(greaterOrEqual, lessThan Item, fn func(Item) bool)
AscendRange starts at the first Item greater or equal to 'greaterOrEqual' and calls 'fn' for each Item less than 'lessThan' or when fn returns 'false'.
O(log(n) + m) where n is the total number of items in the tree and m is the number of items ranged over.
func (*RedBlackTree) Delete ¶
func (t *RedBlackTree) Delete(item Item) Item
Delete deletes an item in the RedBlackTree equal to the provided item. If an item was deleted, it is returned. Otherwise, nil is returned.
Note: equality for items a & b is: (!a.Less(b) && !b.Less(a)).
O(log(n))
func (*RedBlackTree) DeleteMax ¶
func (t *RedBlackTree) DeleteMax() Item
DeleteMax deletes the maximum item in the RedBlackTree, returning it. If the tree is empty, nil is returned.
O(log(n))
func (*RedBlackTree) DeleteMin ¶
func (t *RedBlackTree) DeleteMin() Item
DeleteMin deletes the minimum item in the RedBlackTree, returning it. If the tree is empty, nil is returned.
O(log(n))
func (*RedBlackTree) Descend ¶
func (t *RedBlackTree) Descend(fn func(Item) bool)
Descend starts at the last Item and calls 'fn' for each Item until no Items remain or fn returns 'false'.
O(log(n) + m) where n is the total number of items in the tree and m is the number of items ranged over.
func (*RedBlackTree) Exists ¶
func (t *RedBlackTree) Exists(item Item) bool
Exists returns 'true' if an item equal to the provided item exists in the RedBlackTree.
Note: equality for items a & b is: (!a.Less(b) && !b.Less(a)).
O(log(n))
func (*RedBlackTree) Get ¶
func (t *RedBlackTree) Get(item Item) Item
Get retrieves an item in the RedBlackTree equal to the provided item. If an item was found, it is returned. Otherwise, nil is returned.
Note: equality for items a & b is: (!a.Less(b) && !b.Less(a)).
O(log(n))
func (*RedBlackTree) Max ¶
func (t *RedBlackTree) Max() Item
Max returns the maximum item in the RedBlackTree. If the tree is empty, nil is returned.
O(log(n))
func (*RedBlackTree) Min ¶
func (t *RedBlackTree) Min() Item
Min returns the minimum item in the RedBlackTree. If the tree is empty, nil is returned.
O(log(n))
func (*RedBlackTree) Size ¶
func (t *RedBlackTree) Size() int
Size returns the number of items in the RedBlackTree.
O(1)
func (*RedBlackTree) Upsert ¶
func (t *RedBlackTree) Upsert(item Item) Item
Upsert inserts (or replaces) an item into the RedBlackTree. If an item was replaced, it is returned. Otherwise, nil is returned.
Note: equality for items a & b is: (!a.Less(b) && !b.Less(a)).
O(log(n))