Documentation ¶
Overview ¶
Package redblacktree provides a pure Golang implementation of a red-black tree as described by Thomas H. Cormen's et al. in their seminal Algorithms book (3rd ed). This data structure is not multi-goroutine safe.
Index ¶
- Constants
- Variables
- func IntComparator(o1, o2 interface{}) int
- func SetOutput(w io.Writer)
- func StringComparator(o1, o2 interface{}) int
- func TraceOff()
- func TraceOn()
- type Color
- type Comparator
- type Direction
- type InorderVisitor
- type Node
- type Tree
- func (t *Tree) Delete(key interface{})
- func (t *Tree) Get(key interface{}) (bool, interface{})
- func (t *Tree) GetParent(key interface{}) (found bool, parent *Node, dir Direction)
- func (t *Tree) Has(key interface{}) bool
- func (t *Tree) Put(key interface{}, data interface{}) error
- func (t *Tree) RotateLeft(x *Node)
- func (t *Tree) RotateRight(y *Node)
- func (t *Tree) Size() uint64
- func (t *Tree) Walk(visitor Visitor)
- type Visitable
- type Visitor
Constants ¶
Variables ¶
var ( ErrorKeyIsNil = errors.New("The literal nil not allowed as keys") ErrorKeyDisallowed = errors.New("Disallowed key type") )
Functions ¶
func IntComparator ¶
func IntComparator(o1, o2 interface{}) int
Default comparator expects keys to be of type `int`. Warning: if either one of `o1` or `o2` cannot be asserted to `int`, it panics.
func StringComparator ¶
func StringComparator(o1, o2 interface{}) int
Keys of type `string`. Warning: if either one of `o1` or `o2` cannot be asserted to `string`, it panics.
Types ¶
type Comparator ¶
type Comparator func(o1, o2 interface{}) int
Keys must be comparable. It's mandatory to provide a Comparator, which returns zero if o1 == o2, -1 if o1 < o2, 1 if o1 > o2
type InorderVisitor ¶
type InorderVisitor struct {
// contains filtered or unexported fields
}
InorderVisitor walks the tree in inorder fashion. This visitor maintains internal state; thus do not reuse after the completion of a walk.
func (*InorderVisitor) Eq ¶
func (v *InorderVisitor) Eq(other *InorderVisitor) bool
func (*InorderVisitor) String ¶
func (v *InorderVisitor) String() string
func (*InorderVisitor) Visit ¶
func (v *InorderVisitor) Visit(node *Node)
type Node ¶
type Node struct {
// contains filtered or unexported fields
}
A node needs to be able to answer the query: (i) Who is my parent node ? (ii) Who is my grandparent node ? The zero value for Node has color Red.
type Tree ¶
type Tree struct {
// contains filtered or unexported fields
}
Tree encapsulates the data structure.
func NewTree ¶
func NewTree() *Tree
NewTree returns an empty Tree with default comparator `IntComparator`. `IntComparator` expects keys to be type-assertable to `int`.
func NewTreeWith ¶
func NewTreeWith(c Comparator) *Tree
NewTreeWith returns an empty Tree with a supplied `Comparator`.
func (*Tree) Delete ¶
func (t *Tree) Delete(key interface{})
Delete removes the item identified by the supplied key. Delete is a noop if the supplied key doesn't exist.
func (*Tree) Get ¶
Get looks for the node with supplied key and returns its mapped payload. Return value in 1st position indicates whether any payload was found.
func (*Tree) GetParent ¶
GetParent looks for the node with supplied key and returns the parent node.
func (*Tree) Put ¶
Put saves the mapping (key, data) into the tree. If a mapping identified by `key` already exists, it is overwritten. Constraint: Not everything can be a key.
func (*Tree) RotateLeft ¶
Side-effect: red-black tree properties is maintained.