diff --git a/trees/binaryheap/binaryheap.go b/trees/binaryheap/binaryheap.go index 70b28cf..e69d4d5 100644 --- a/trees/binaryheap/binaryheap.go +++ b/trees/binaryheap/binaryheap.go @@ -13,14 +13,15 @@ package binaryheap import ( "fmt" + "strings" + + "github.com/emirpasic/gods/containers" "github.com/emirpasic/gods/lists/arraylist" - "github.com/emirpasic/gods/trees" "github.com/emirpasic/gods/utils" - "strings" ) func assertTreeImplementation() { - var _ trees.Tree = (*Heap)(nil) + var _ containers.Container = (*Heap)(nil) } // Heap holds elements in an array-list diff --git a/trees/btree/btree.go b/trees/btree/btree.go index 5f86699..9ba6f5b 100644 --- a/trees/btree/btree.go +++ b/trees/btree/btree.go @@ -19,7 +19,7 @@ package btree import ( "bytes" "fmt" - "github.com/emirpasic/gods/trees" + "github.com/spewspews/gods/trees" "github.com/emirpasic/gods/utils" "strings" ) @@ -31,7 +31,7 @@ func assertTreeImplementation() { // Tree holds elements of the B-tree type Tree struct { Root *Node // Root node - Comparator utils.Comparator // Key comparator + comparator utils.Comparator // Key comparator size int // Total number of keys in the tree m int // order (maximum number of children) } @@ -54,7 +54,7 @@ func NewWith(order int, comparator utils.Comparator) *Tree { if order < 3 { panic("Invalid order, should be at least 3") } - return &Tree{m: order, Comparator: comparator} + return &Tree{m: order, comparator: comparator} } // NewWithIntComparator instantiates a B-tree with the order (maximum number of children) and the IntComparator, i.e. keys are of type int. @@ -105,6 +105,11 @@ func (tree *Tree) Remove(key interface{}) { } } +// New returns an empty tree with the same comparator +func (tree *Tree) New() trees.Tree { + return &Tree{m: tree.m, comparator: tree.comparator} +} + // Empty returns true if tree does not contain any nodes func (tree *Tree) Empty() bool { return tree.size == 0 @@ -167,6 +172,15 @@ func (tree *Tree) LeftValue() interface{} { return nil } +// Min returns the minimum key value pair in the tree. +func (tree *Tree) Min() (interface{}, interface{}) { + n := tree.Left() + if n == nil { + return nil, nil + } + return n.Entries[0].Key, n.Entries[0].Value +} + // Right returns the right-most (max) node or nil if tree is empty. func (tree *Tree) Right() *Node { return tree.right(tree.Root) @@ -188,6 +202,16 @@ func (tree *Tree) RightValue() interface{} { return nil } +// Max returns the minimum key value pair in the tree. +func (tree *Tree) Max() (interface{}, interface{}) { + n := tree.Right() + if n == nil { + return nil, nil + } + l := len(n.Entries)-1 + return n.Entries[l].Key, n.Entries[l].Value +} + // String returns a string representation of container (for debugging purposes) func (tree *Tree) String() string { var buffer bytes.Buffer @@ -266,7 +290,7 @@ func (tree *Tree) search(node *Node, key interface{}) (index int, found bool) { var mid int for low <= high { mid = (high + low) / 2 - compare := tree.Comparator(key, node.Entries[mid].Key) + compare := tree.comparator(key, node.Entries[mid].Key) switch { case compare > 0: low = mid + 1 diff --git a/trees/btree/iterator.go b/trees/btree/iterator.go index 840db68..c01c519 100644 --- a/trees/btree/iterator.go +++ b/trees/btree/iterator.go @@ -25,8 +25,8 @@ const ( ) // Iterator returns a stateful iterator whose elements are key/value pairs. -func (tree *Tree) Iterator() Iterator { - return Iterator{tree: tree, node: nil, position: begin} +func (tree *Tree) Iterator() containers.ReverseIteratorWithKey { + return &Iterator{tree: tree, node: nil, position: begin} } // Next moves the iterator to the next element and returns true if there was a next element in the container.