- simplify avl tree by exposing its comparator , i.e. del getter for comparator

pull/51/head
Emir Pasic 9 years ago
parent 6f20e11a99
commit f480e9419a

@ -21,7 +21,7 @@ func assertTreeImplementation() {
type Tree struct { type Tree struct {
Root *Node Root *Node
size int size int
comparator utils.Comparator Comparator utils.Comparator
} }
// Node is a single element within the tree // Node is a single element within the tree
@ -35,27 +35,22 @@ type Node struct {
// NewWith instantiates an AVL tree with the custom comparator. // NewWith instantiates an AVL 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}
} }
// NewWithIntComparator instantiates an AVL tree with the IntComparator, i.e. keys are of type int. // NewWithIntComparator instantiates an AVL 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}
} }
// NewWithStringComparator instantiates an AVL tree with the StringComparator, i.e. keys are of type string. // NewWithStringComparator instantiates an AVL 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}
}
// Comparator returns the comparator function for the tree.
func (t *Tree) Comparator() utils.Comparator {
return t.comparator
} }
// New returns a new empty tree with the same comparator. // New returns a new empty tree with the same comparator.
func (t *Tree) New() trees.Tree { func (t *Tree) New() trees.Tree {
return &Tree{comparator: t.comparator} return &Tree{Comparator: t.Comparator}
} }
// Size returns the number of elements stored in the tree. // Size returns the number of elements stored in the tree.
@ -80,7 +75,7 @@ func (t *Tree) Clear() {
func (t *Tree) Get(key interface{}) (value interface{}, found bool) { func (t *Tree) Get(key interface{}) (value interface{}, found bool) {
n := t.Root n := t.Root
for n != nil { for n != nil {
cmp := t.comparator(key, n.Key) cmp := t.Comparator(key, n.Key)
switch { switch {
case cmp == 0: case cmp == 0:
return n.Value, true return n.Value, true
@ -105,7 +100,7 @@ func (t *Tree) Floor(key interface{}) (floor *Node, found bool) {
found = false found = false
n := t.Root n := t.Root
for n != nil { for n != nil {
c := t.comparator(key, n.Key) c := t.Comparator(key, n.Key)
switch { switch {
case c == 0: case c == 0:
return n, true return n, true
@ -134,7 +129,7 @@ func (t *Tree) Ceiling(key interface{}) (floor *Node, found bool) {
found = false found = false
n := t.Root n := t.Root
for n != nil { for n != nil {
c := t.comparator(key, n.Key) c := t.Comparator(key, n.Key)
switch { switch {
case c == 0: case c == 0:
return n, true return n, true
@ -163,7 +158,7 @@ func (t *Tree) Put(key interface{}, value interface{}) {
return true return true
} }
c := t.comparator(key, q.Key) c := t.Comparator(key, q.Key)
if c == 0 { if c == 0 {
q.Key = key q.Key = key
q.Value = value q.Value = value
@ -197,7 +192,7 @@ func (t *Tree) Remove(key interface{}) {
return false return false
} }
c := t.comparator(key, q.Key) c := t.Comparator(key, q.Key)
if c == 0 { if c == 0 {
t.size-- t.size--
if q.c[1] == nil { if q.c[1] == nil {

Loading…
Cancel
Save