Merge pull request #9 from vlad-alexandru-ionescu/master

Add ability to get leftmost (minimum) and rightmost (maximum) keys in…
This commit is contained in:
Emir Pasic 2016-06-12 14:45:30 +02:00 committed by GitHub
commit 84af8bb166
3 changed files with 70 additions and 12 deletions

View File

@ -73,6 +73,16 @@ func (m *Map) Get(key interface{}) (value interface{}, found bool) {
return m.tree.Get(key)
}
// Returns the left-most element in the tree map (minimum).
func (m *Map) Left() (key interface{}) {
return m.tree.Left()
}
// Returns the right-most element in the tree map (maximum).
func (m *Map) Right() (key interface{}) {
return m.tree.Right()
}
// Remove the element from the map by key.
// Key should adhere to the comparator's type assertion, otherwise method panics.
func (m *Map) Remove(key interface{}) {

View File

@ -51,13 +51,23 @@ func TestTreeMap(t *testing.T) {
}
// test Keys()
if actualValue, expactedValue := fmt.Sprintf("%d%d%d%d%d%d%d", m.Keys()...), "1234567"; actualValue != expactedValue {
t.Errorf("Got %v expected %v", actualValue, expactedValue)
if actualValue, expectedValue := fmt.Sprintf("%d%d%d%d%d%d%d", m.Keys()...), "1234567"; actualValue != expectedValue {
t.Errorf("Got %v expected %v", actualValue, expectedValue)
}
// test Values()
if actualValue, expactedValue := fmt.Sprintf("%s%s%s%s%s%s%s", m.Values()...), "abcdefg"; actualValue != expactedValue {
t.Errorf("Got %v expected %v", actualValue, expactedValue)
if actualValue, expectedValue := fmt.Sprintf("%s%s%s%s%s%s%s", m.Values()...), "abcdefg"; actualValue != expectedValue {
t.Errorf("Got %v expected %v", actualValue, expectedValue)
}
// test Left()
if actualValue, expectedValue := fmt.Sprintf("%d", m.Left()), "1"; actualValue != expectedValue {
t.Errorf("Got %v expected %v", actualValue, expectedValue)
}
// test Right()
if actualValue, expectedValue := fmt.Sprintf("%d", m.Right()), "7"; actualValue != expectedValue {
t.Errorf("Got %v expected %v", actualValue, expectedValue)
}
// key,expectedValue,expectedFound
@ -88,13 +98,13 @@ func TestTreeMap(t *testing.T) {
m.Remove(5)
// Test Keys()
if actualValue, expactedValue := fmt.Sprintf("%d%d%d%d", m.Keys()...), "1234"; actualValue != expactedValue {
t.Errorf("Got %v expected %v", actualValue, expactedValue)
if actualValue, expectedValue := fmt.Sprintf("%d%d%d%d", m.Keys()...), "1234"; actualValue != expectedValue {
t.Errorf("Got %v expected %v", actualValue, expectedValue)
}
// test Values()
if actualValue, expactedValue := fmt.Sprintf("%s%s%s%s", m.Values()...), "abcd"; actualValue != expactedValue {
t.Errorf("Got %v expected %v", actualValue, expactedValue)
if actualValue, expectedValue := fmt.Sprintf("%s%s%s%s", m.Values()...), "abcd"; actualValue != expectedValue {
t.Errorf("Got %v expected %v", actualValue, expectedValue)
}
// Test Size()
@ -130,13 +140,13 @@ func TestTreeMap(t *testing.T) {
m.Remove(2)
// Test Keys()
if actualValue, expactedValue := fmt.Sprintf("%s", m.Keys()), "[]"; actualValue != expactedValue {
t.Errorf("Got %v expected %v", actualValue, expactedValue)
if actualValue, expectedValue := fmt.Sprintf("%s", m.Keys()), "[]"; actualValue != expectedValue {
t.Errorf("Got %v expected %v", actualValue, expectedValue)
}
// test Values()
if actualValue, expactedValue := fmt.Sprintf("%s", m.Values()), "[]"; actualValue != expactedValue {
t.Errorf("Got %v expected %v", actualValue, expactedValue)
if actualValue, expectedValue := fmt.Sprintf("%s", m.Values()), "[]"; actualValue != expectedValue {
t.Errorf("Got %v expected %v", actualValue, expectedValue)
}
// Test Size()

View File

@ -186,6 +186,24 @@ func (tree *Tree) Values() []interface{} {
return values
}
// Returns the left-most key.
func (tree *Tree) Left() interface{} {
left := tree.leftNode()
if left == nil {
return nil
}
return left.Key
}
// Returns the right-most key.
func (tree *Tree) Right() interface{} {
right := tree.rightNode()
if right == nil {
return nil
}
return right.Key
}
// Removes all nodes from the tree.
func (tree *Tree) Clear() {
tree.Root = nil
@ -232,6 +250,26 @@ func (tree *Tree) inOrder() []*Node {
return nodes
}
func (tree *Tree) leftNode() *Node {
var parent *Node
current := tree.Root
for current != nil {
parent = current
current = current.Left
}
return parent
}
func (tree *Tree) rightNode() *Node {
var parent *Node
current := tree.Root
for current != nil {
parent = current
current = current.Right
}
return parent
}
func output(node *Node, prefix string, isTail bool, str *string) {
if node.Right != nil {
newPrefix := prefix