mirror of
https://github.com/emirpasic/gods
synced 2024-11-18 09:25:50 +00:00
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:
commit
84af8bb166
@ -73,6 +73,16 @@ func (m *Map) Get(key interface{}) (value interface{}, found bool) {
|
|||||||
return m.tree.Get(key)
|
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.
|
// Remove the element from the map by key.
|
||||||
// Key should adhere to the comparator's type assertion, otherwise method panics.
|
// Key should adhere to the comparator's type assertion, otherwise method panics.
|
||||||
func (m *Map) Remove(key interface{}) {
|
func (m *Map) Remove(key interface{}) {
|
||||||
|
@ -51,13 +51,23 @@ func TestTreeMap(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// test Keys()
|
// test Keys()
|
||||||
if actualValue, expactedValue := fmt.Sprintf("%d%d%d%d%d%d%d", m.Keys()...), "1234567"; 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, expactedValue)
|
t.Errorf("Got %v expected %v", actualValue, expectedValue)
|
||||||
}
|
}
|
||||||
|
|
||||||
// test Values()
|
// test Values()
|
||||||
if actualValue, expactedValue := fmt.Sprintf("%s%s%s%s%s%s%s", m.Values()...), "abcdefg"; 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, expactedValue)
|
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
|
// key,expectedValue,expectedFound
|
||||||
@ -88,13 +98,13 @@ func TestTreeMap(t *testing.T) {
|
|||||||
m.Remove(5)
|
m.Remove(5)
|
||||||
|
|
||||||
// Test Keys()
|
// Test Keys()
|
||||||
if actualValue, expactedValue := fmt.Sprintf("%d%d%d%d", m.Keys()...), "1234"; actualValue != expactedValue {
|
if actualValue, expectedValue := fmt.Sprintf("%d%d%d%d", m.Keys()...), "1234"; actualValue != expectedValue {
|
||||||
t.Errorf("Got %v expected %v", actualValue, expactedValue)
|
t.Errorf("Got %v expected %v", actualValue, expectedValue)
|
||||||
}
|
}
|
||||||
|
|
||||||
// test Values()
|
// test Values()
|
||||||
if actualValue, expactedValue := fmt.Sprintf("%s%s%s%s", m.Values()...), "abcd"; actualValue != expactedValue {
|
if actualValue, expectedValue := fmt.Sprintf("%s%s%s%s", m.Values()...), "abcd"; actualValue != expectedValue {
|
||||||
t.Errorf("Got %v expected %v", actualValue, expactedValue)
|
t.Errorf("Got %v expected %v", actualValue, expectedValue)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Test Size()
|
// Test Size()
|
||||||
@ -130,13 +140,13 @@ func TestTreeMap(t *testing.T) {
|
|||||||
m.Remove(2)
|
m.Remove(2)
|
||||||
|
|
||||||
// Test Keys()
|
// Test Keys()
|
||||||
if actualValue, expactedValue := fmt.Sprintf("%s", m.Keys()), "[]"; actualValue != expactedValue {
|
if actualValue, expectedValue := fmt.Sprintf("%s", m.Keys()), "[]"; actualValue != expectedValue {
|
||||||
t.Errorf("Got %v expected %v", actualValue, expactedValue)
|
t.Errorf("Got %v expected %v", actualValue, expectedValue)
|
||||||
}
|
}
|
||||||
|
|
||||||
// test Values()
|
// test Values()
|
||||||
if actualValue, expactedValue := fmt.Sprintf("%s", m.Values()), "[]"; actualValue != expactedValue {
|
if actualValue, expectedValue := fmt.Sprintf("%s", m.Values()), "[]"; actualValue != expectedValue {
|
||||||
t.Errorf("Got %v expected %v", actualValue, expactedValue)
|
t.Errorf("Got %v expected %v", actualValue, expectedValue)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Test Size()
|
// Test Size()
|
||||||
|
@ -186,6 +186,24 @@ func (tree *Tree) Values() []interface{} {
|
|||||||
return values
|
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.
|
// Removes all nodes from the tree.
|
||||||
func (tree *Tree) Clear() {
|
func (tree *Tree) Clear() {
|
||||||
tree.Root = nil
|
tree.Root = nil
|
||||||
@ -232,6 +250,26 @@ func (tree *Tree) inOrder() []*Node {
|
|||||||
return nodes
|
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) {
|
func output(node *Node, prefix string, isTail bool, str *string) {
|
||||||
if node.Right != nil {
|
if node.Right != nil {
|
||||||
newPrefix := prefix
|
newPrefix := prefix
|
||||||
|
Loading…
Reference in New Issue
Block a user