func (n *redBlackNode) traverseWhile(condition func(*redBlackNode) bool) bool {
	if n == nil {
		return true
	}
	if !n.left.traverseWhile(condition) {
		return false
	}
	if !condition(n) {
		return false
	}
	if !n.right.traverseWhile(condition) {
		return false
	}
	return true
}
traverseWhile traverses the nodes in the tree in-order invoking the `condition`-function argument for each node.
If the condition-function returns `false` traversal is stopped and no more nodes will be
visited. Returns `true` if all nodes are visited; `false` if not.
package hashring
type redBlackTree struct {
	root *redBlackNode
	size int
}
type keytype interface {
	Compare(other interface{}) int
}
type valuetype interface{}
type redBlackNode struct {
	key   keytype
	value valuetype
	left  *redBlackNode
	right *redBlackNode
	red   bool
}
func (t *redBlackTree) Size() int {
	return t.size
}
func (n *redBlackNode) Child(right bool) *redBlackNode {
	if right {
		return n.right
	}
	return n.left
}
func (n *redBlackNode) setChild(right bool, node *redBlackNode) {
	if right {
		n.right = node
	} else {
		n.left = node
	}
}
func isRed(node *redBlackNode) bool {
	return node != nil && node.red
}
func singleRotate(oldroot *redBlackNode, dir bool) *redBlackNode {
	newroot := oldroot.Child(!dir)
	oldroot.setChild(!dir, newroot.Child(dir))
	newroot.setChild(dir, oldroot)
	oldroot.red = true
	newroot.red = false
	return newroot
}
func doubleRotate(root *redBlackNode, dir bool) *redBlackNode {
	root.setChild(!dir, singleRotate(root.Child(!dir), !dir))
	return singleRotate(root, dir)
}
func (t *redBlackTree) Insert(key keytype, value valuetype) (ret bool) {
	if t.root == nil {
		t.root = &redBlackNode{
			key:   key,
			value: value,
		}
		ret = true
	} else {
		var head = &redBlackNode{}
		var dir = true
		var last = true
		var parent *redBlackNode   
		var gparent *redBlackNode  
		var ggparent = head        
		var node = t.root
		ggparent.right = t.root
		for {
			if node == nil {
				node = &redBlackNode{
					key:   key,
					value: value,
					red:   true,
				}
				parent.setChild(dir, node)
				ret = true
			} else if isRed(node.left) && isRed(node.right) {
				node.red = true
				node.left.red, node.right.red = false, false
			}
			if isRed(node) && isRed(parent) {
				dir2 := ggparent.right == gparent
				if node == parent.Child(last) {
					ggparent.setChild(dir2, singleRotate(gparent, !last))
				} else {
					ggparent.setChild(dir2, doubleRotate(gparent, !last))
				}
			}
			cmp := node.key.Compare(key)
			if cmp == 0 {
				break
			}
			last = dir
			dir = cmp < 0
			if gparent != nil {
				ggparent = gparent
			}
			gparent = parent
			parent = node
			node = node.Child(dir)
		}
		t.root = head.right
	}
	t.root.red = false
	if ret {
		t.size++
	}
	return ret
}
func (t *redBlackTree) Delete(key keytype) bool {
	if t.root == nil {
		return false
	}
	var head = &redBlackNode{red: true}  
	var node = head
	var parent *redBlackNode   
	var gparent *redBlackNode  
	var found *redBlackNode
	var dir = true
	node.right = t.root
	for node.Child(dir) != nil {
		last := dir
		gparent = parent
		parent = node
		node = node.Child(dir)
		cmp := node.key.Compare(key)
		dir = cmp < 0
		if cmp == 0 {
			found = node
		}
		if !isRed(node) && !isRed(node.Child(dir)) {
			if isRed(node.Child(!dir)) {
				sr := singleRotate(node, dir)
				parent.setChild(last, sr)
				parent = sr
			} else {
				sibling := parent.Child(!last)
				if sibling != nil {
					if !isRed(sibling.Child(!last)) && !isRed(sibling.Child(last)) {
						parent.red = false
						sibling.red, node.red = true, true
					} else {
						dir2 := gparent.right == parent
						if isRed(sibling.Child(last)) {
							gparent.setChild(dir2, doubleRotate(parent, last))
						} else if isRed(sibling.Child(!last)) {
							gparent.setChild(dir2, singleRotate(parent, last))
						}
						gpc := gparent.Child(dir2)
						gpc.red = true
						node.red = true
						gpc.left.red, gpc.right.red = false, false
					}
				}
			}
		}
	}
	if found != nil {
		found.key = node.key
		found.value = node.value
		parent.setChild(parent.right == node, node.Child(node.left == nil))
		t.size--
	}
	t.root = head.right
	if t.root != nil {
		t.root.red = false
	}
	return found != nil
}
func (n *redBlackNode) search(key keytype) (valuetype, bool) {
	cmp := n.key.Compare(key)
	if cmp == 0 {
		return n.value, true
	} else if 0 < cmp {
		if n.left != nil {
			return n.left.search(key)
		}
	} else if n.right != nil {
		return n.right.search(key)
	}
	return nil, false
}