expose comparator in redblacktree

pull/5/head
Emir Pasic 10 years ago
parent 6fb76b553f
commit 351cf24f70

@ -51,7 +51,7 @@ const (
type Tree struct { type Tree struct {
Root *Node Root *Node
size int size int
comparator utils.Comparator Comparator utils.Comparator
} }
type Node struct { type Node struct {
@ -65,17 +65,17 @@ type Node struct {
// Instantiates a red-black tree with the custom comparator. // Instantiates a red-black tree with the custom comparator.
func NewWith(comparator utils.Comparator) *Tree { func NewWith(comparator utils.Comparator) *Tree {
return &Tree{comparator: comparator} return &Tree{Comparator: comparator}
} }
// Instantiates a red-black tree with the IntComparator, i.e. keys are of type int. // Instantiates a red-black tree with the IntComparator, i.e. keys are of type int.
func NewWithIntComparator() *Tree { func NewWithIntComparator() *Tree {
return &Tree{comparator: utils.IntComparator} return &Tree{Comparator: utils.IntComparator}
} }
// Instantiates a red-black tree with the StringComparator, i.e. keys are of type string. // Instantiates a red-black tree with the StringComparator, i.e. keys are of type string.
func NewWithStringComparator() *Tree { func NewWithStringComparator() *Tree {
return &Tree{comparator: utils.StringComparator} return &Tree{Comparator: utils.StringComparator}
} }
// Inserts node into the tree. // Inserts node into the tree.
@ -88,7 +88,7 @@ func (tree *Tree) Put(key interface{}, value interface{}) {
node := tree.Root node := tree.Root
loop := true loop := true
for loop { for loop {
compare := tree.comparator(key, node.Key) compare := tree.Comparator(key, node.Key)
switch { switch {
case compare == 0: case compare == 0:
node.Value = value node.Value = value
@ -263,7 +263,7 @@ func output(node *Node, prefix string, isTail bool, str *string) {
func (tree *Tree) lookup(key interface{}) *Node { func (tree *Tree) lookup(key interface{}) *Node {
node := tree.Root node := tree.Root
for node != nil { for node != nil {
compare := tree.comparator(key, node.Key) compare := tree.Comparator(key, node.Key)
switch { switch {
case compare == 0: case compare == 0:
return node return node

Loading…
Cancel
Save