- Add Min() and Max() function to the tree map with test and documentation update

pull/10/head
Emir Pasic 9 years ago
parent d13e3d6b6a
commit 16d751cd27

@ -400,6 +400,10 @@ func main() {
m.Clear() // empty m.Clear() // empty
m.Empty() // true m.Empty() // true
m.Size() // 0 m.Size() // 0
// Other:
m.Min() // Returns the minimum key and its value from map.
m.Max() // Returns the maximum key and its value from map.
} }
``` ```

@ -73,16 +73,6 @@ 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{}) {
@ -114,6 +104,24 @@ func (m *Map) Clear() {
m.tree.Clear() m.tree.Clear()
} }
// Returns the minimum key and its value from the tree map.
// Returns nil, nil if map is empty.
func (m *Map) Min() (key interface{}, value interface{}) {
if node := m.tree.Left(); node != nil {
return node.Key, node.Value
}
return nil, nil
}
// Returns the maximum key and its value from the tree map.
// Returns nil, nil if map is empty.
func (m *Map) Max() (key interface{}, value interface{}) {
if node := m.tree.Right(); node != nil {
return node.Key, node.Value
}
return nil, nil
}
func (m *Map) String() string { func (m *Map) String() string {
str := "TreeMap\n" str := "TreeMap\n"
str += m.tree.String() str += m.tree.String()

@ -60,14 +60,14 @@ func TestTreeMap(t *testing.T) {
t.Errorf("Got %v expected %v", actualValue, expectedValue) t.Errorf("Got %v expected %v", actualValue, expectedValue)
} }
// test Left() // test Min()
if actualValue, expectedValue := fmt.Sprintf("%d", m.Left()), "1"; actualValue != expectedValue { if key, value := m.Min(); key != 1 || value != "a" {
t.Errorf("Got %v expected %v", actualValue, expectedValue) t.Errorf("Got %v expected %v", key, 1)
} }
// test Right() // test Max()
if actualValue, expectedValue := fmt.Sprintf("%d", m.Right()), "7"; actualValue != expectedValue { if key, value := m.Max(); key != 7 || value != "g" {
t.Errorf("Got %v expected %v", actualValue, expectedValue) t.Errorf("Got %v expected %v", key, 7)
} }
// key,expectedValue,expectedFound // key,expectedValue,expectedFound
@ -168,6 +168,15 @@ func TestTreeMap(t *testing.T) {
t.Errorf("Got %v expected %v", actualValue, true) t.Errorf("Got %v expected %v", actualValue, true)
} }
// test Min()
if key, value := m.Min(); key != nil || value != nil {
t.Errorf("Got %v expected %v", key, nil)
}
// test Max()
if key, value := m.Max(); key != nil || value != nil {
t.Errorf("Got %v expected %v", key, nil)
}
} }
func BenchmarkTreeMap(b *testing.B) { func BenchmarkTreeMap(b *testing.B) {

Loading…
Cancel
Save