add ceiling and floor function to redblacktree

pull/4/head
otnt 10 years ago
parent 6fb76b553f
commit f7f48e7628

@ -116,7 +116,7 @@ func (tree *Tree) Put(key interface{}, value interface{}) {
}
// Searches the node in the tree by key and returns its value or nil if key is not found in tree.
// Second return parameter is true if key was found, otherwise false.
// return parameter is true if key was found, otherwise false.
// Key should adhere to the comparator's type assertion, otherwise method panics.
func (tree *Tree) Get(key interface{}) (value interface{}, found bool) {
node := tree.lookup(key)
@ -126,6 +126,64 @@ func (tree *Tree) Get(key interface{}) (value interface{}, found bool) {
return nil, false
}
// Find ceiling node of the input key, return its key and value or nil if no ceiling is found.
// Third return parameter is true if ceiling was found, otherwise false.
// Ceiling node is defined as the smallest node that is larger than or equal to the given node.
// A ceiling node may not be found, either because the tree is empty, or because
// all nodes in the tree is smaller than the given node.
// Key should adhere to the comparator's type assertion, otherwise method panics.
func (tree *Tree) Ceiling(key interface{}) (ceilingKey interface{}, value interface{}, found bool) {
ceiling := &Node{}
found = false
node := tree.Root
for node != nil {
compare := tree.comparator(key, node.Key)
switch {
case compare == 0:
return node.Key, node.Value, true
case compare < 0:
ceiling, found = node, true
node = node.Left
case compare > 0:
node = node.Right
}
}
if found {
return ceiling.Key, ceiling.Value, true
}
return nil, nil, false
}
// Find floor node of the input key, return its key and value or nil if no ceiling is found.
// Third return parameter is true if floor was found, otherwise false.
// Floor node is defined as the largest node that is smaller than or equal to the given node.
// A floor node may not be found, either because the tree is empty, or because
// all nodes in the tree is larger than the given node.
// Key should adhere to the comparator's type assertion, otherwise method panics.
func (tree *Tree) Floor(key interface{}) (floorKey interface{}, value interface{}, found bool) {
floor := &Node{}
found = false
node := tree.Root
for node != nil {
compare := tree.comparator(key, node.Key)
switch {
case compare == 0:
return node.Key, node.Value, true
case compare < 0:
node = node.Left
case compare > 0:
floor, found = node, true
node = node.Right
}
}
if found {
return floor.Key, floor.Value, true
}
return nil, nil, false
}
// Remove the node from the tree by key.
// Key should adhere to the comparator's type assertion, otherwise method panics.
func (tree *Tree) Remove(key interface{}) {

@ -160,6 +160,70 @@ func TestRedBlackTree(t *testing.T) {
}
func TestCeiling(t *testing.T) {
tree := NewWithIntComparator()
//key-value to insert
tree.Put(1, "a")
tree.Put(2, "b")
tree.Put(4, "d")
tree.Put(6, "f")
tree.Put(7, "g")
//ceiling map
ceilingMap := [][]interface{}{
{0, 1, true},
{1, 1, true},
{2, 2, true},
{3, 4, true},
{4, 4, true},
{5, 6, true},
{6, 6, true},
{7, 7, true},
{8, nil, false},
}
for _, test := range ceilingMap {
actualKey, _, actualFound := tree.Ceiling(test[0])
if actualKey != test[1] || actualFound != test[2] {
t.Errorf("Got (%v, %v) expected (%v, %v)",
actualKey, actualFound, test[1], test[2])
}
}
}
func TestFloor(t *testing.T) {
tree := NewWithIntComparator()
//key-value to insert
tree.Put(1, "a")
tree.Put(2, "b")
tree.Put(4, "d")
tree.Put(6, "f")
tree.Put(7, "g")
//ceiling map
ceilingMap := [][]interface{}{
{0, nil, false},
{1, 1, true},
{2, 2, true},
{3, 2, true},
{4, 4, true},
{5, 4, true},
{6, 6, true},
{7, 7, true},
{8, 7, true},
}
for _, test := range ceilingMap {
actualKey, _, actualFound := tree.Floor(test[0])
if actualKey != test[1] || actualFound != test[2] {
t.Errorf("Got (%v, %v) expected (%v, %v)",
actualKey, actualFound, test[1], test[2])
}
}
}
func BenchmarkRedBlackTree(b *testing.B) {
for i := 0; i < b.N; i++ {
tree := NewWithIntComparator()

Loading…
Cancel
Save