func (tree *RbTree) Min() (RbKey, interface{}) {
    if tree.root != nil {
        result := min(tree.root)
        return result.key, result.value
    }
    return nil, nil
}
Min returns the smallest key in the tree.
package rbt
type KeyComparison int8
const (
    KeyIsLess KeyComparison = iota - 1 
    KeysAreEqual
    KeyIsGreater
)
const (
    red byte = byte(0)
    black byte = byte(1)
    zeroOrEqual = int8(0)
)
func (tree KeyComparison) String() string {
    switch tree {
    case KeyIsLess:
        return "lessThan"
    case KeyIsGreater:
        return "greaterThan"
    default:
        return "equalTo"
    }
}
type RbKey interface {
    ComparedTo(key RbKey) KeyComparison
}
type rbNode struct {
    key RbKey
    value interface{}
    color byte
    left, right *rbNode
}
type RbTree struct {
    root *rbNode
    count int
    version uint32
    onInsert InsertEvent
    onDelete DeleteEvent
}
type DeleteEvent func(key RbKey, oldValue interface{}) (updatedValue interface{})
type InsertEvent func(key RbKey, oldValue interface{}, newValue interface{}) (updatedValue interface{})
func NewRbTree() *RbTree {
    return &RbTree{}
}
func NewRbTreeWithEvents(onInsert InsertEvent, onDelete DeleteEvent) *RbTree {
    return &RbTree{
        onInsert: onInsert,
        onDelete: onDelete,
    }
}
func newRbNode(key RbKey, value interface{}) *rbNode {
    result := &rbNode{
        key: key,
        value: value,
        color: red,
    }
    return result
}
func isRed(node *rbNode) bool {
    return node != nil && node.color == red
}
func isBlack(node *rbNode) bool { 
    return node != nil && node.color == black 
}
func min(node *rbNode) *rbNode {
    if node != nil {
        for node.left != nil {
            node = node.left
        }
    }
    return node
}
func max(node *rbNode) *rbNode {
    if node != nil {
        for node.right != nil {
            node = node.right
        }
    }
    return node
}
func floor(node *rbNode, key RbKey) *rbNode {
    if node == nil {
        return nil
    }
    switch key.ComparedTo(node.key) {
    case KeysAreEqual:
        return node
    case KeyIsLess:
        return floor(node.left, key)
    default:
        fn := floor(node.right, key)
        if fn != nil {
            return fn
        }
        return node
    }
}
func ceiling(node *rbNode, key RbKey) *rbNode {  
    if node == nil {
        return nil
    }
    switch key.ComparedTo(node.key) {
    case KeysAreEqual:
        return node
    case KeyIsGreater:
        return ceiling(node.right, key)
    default:
        cn := ceiling(node.left, key)
        if cn != nil {
            return cn
        }
        return node
    }
}
func flipColor(node *rbNode) {
    if node.color == black {
        node.color = red
    } else {
        node.color = black
    }
}
func colorFlip(node *rbNode) {
    flipColor(node)
    flipColor(node.left)
    flipColor(node.right)
}
func rotateLeft(node *rbNode) *rbNode {
    child := node.right
    node.right = child.left
    child.left = node
    child.color = node.color
    node.color = red
    return child
}
func rotateRight(node *rbNode) *rbNode {
    child := node.left
    node.left = child.right
    child.right = node
    child.color = node.color
    node.color = red
    return child
}
func moveRedLeft(node *rbNode) *rbNode {
    colorFlip(node)
    if isRed(node.right.left) {
        node.right = rotateRight(node.right)
        node = rotateLeft(node)
        colorFlip(node)
    }
    return node
}
func moveRedRight(node *rbNode) *rbNode {
    colorFlip(node)
    if isRed(node.left.left) {
        node = rotateRight(node)
        colorFlip(node)
    }
    return node
}
func balance(node *rbNode) *rbNode {
    if isRed(node.right) {
        node = rotateLeft(node)
    }
    if isRed(node.left) && isRed(node.left.left) {
        node = rotateRight(node)
    }
    if isRed(node.left) && isRed(node.right) {
        colorFlip(node)
    }
    return node
}
func deleteMin(node *rbNode) *rbNode {
    if node.left == nil {
        return nil
    }    
    if isBlack(node.left) && !isRed(node.left.left) {
        node = moveRedLeft(node)
    }
    node.left = deleteMin(node.left)
    return balance(node)
}
func (tree *RbTree) Count() int {
    return tree.count
}
func (tree *RbTree) IsEmpty() bool {
    return tree.root == nil
}