fix more travis issues

pull/47/head
Benjamin Scher Purcell 9 years ago
parent b8c9d1188f
commit c49ab09dea

@ -13,14 +13,15 @@ package binaryheap
import ( import (
"fmt" "fmt"
"strings"
"github.com/emirpasic/gods/containers"
"github.com/emirpasic/gods/lists/arraylist" "github.com/emirpasic/gods/lists/arraylist"
"github.com/emirpasic/gods/trees"
"github.com/emirpasic/gods/utils" "github.com/emirpasic/gods/utils"
"strings"
) )
func assertTreeImplementation() { func assertTreeImplementation() {
var _ trees.Tree = (*Heap)(nil) var _ containers.Container = (*Heap)(nil)
} }
// Heap holds elements in an array-list // Heap holds elements in an array-list

@ -19,7 +19,7 @@ package btree
import ( import (
"bytes" "bytes"
"fmt" "fmt"
"github.com/emirpasic/gods/trees" "github.com/spewspews/gods/trees"
"github.com/emirpasic/gods/utils" "github.com/emirpasic/gods/utils"
"strings" "strings"
) )
@ -31,7 +31,7 @@ func assertTreeImplementation() {
// Tree holds elements of the B-tree // Tree holds elements of the B-tree
type Tree struct { type Tree struct {
Root *Node // Root node Root *Node // Root node
Comparator utils.Comparator // Key comparator comparator utils.Comparator // Key comparator
size int // Total number of keys in the tree size int // Total number of keys in the tree
m int // order (maximum number of children) m int // order (maximum number of children)
} }
@ -54,7 +54,7 @@ func NewWith(order int, comparator utils.Comparator) *Tree {
if order < 3 { if order < 3 {
panic("Invalid order, should be at least 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. // 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 // Empty returns true if tree does not contain any nodes
func (tree *Tree) Empty() bool { func (tree *Tree) Empty() bool {
return tree.size == 0 return tree.size == 0
@ -167,6 +172,15 @@ func (tree *Tree) LeftValue() interface{} {
return nil 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. // Right returns the right-most (max) node or nil if tree is empty.
func (tree *Tree) Right() *Node { func (tree *Tree) Right() *Node {
return tree.right(tree.Root) return tree.right(tree.Root)
@ -188,6 +202,16 @@ func (tree *Tree) RightValue() interface{} {
return nil 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) // String returns a string representation of container (for debugging purposes)
func (tree *Tree) String() string { func (tree *Tree) String() string {
var buffer bytes.Buffer var buffer bytes.Buffer
@ -266,7 +290,7 @@ func (tree *Tree) search(node *Node, key interface{}) (index int, found bool) {
var mid int var mid int
for low <= high { for low <= high {
mid = (high + low) / 2 mid = (high + low) / 2
compare := tree.Comparator(key, node.Entries[mid].Key) compare := tree.comparator(key, node.Entries[mid].Key)
switch { switch {
case compare > 0: case compare > 0:
low = mid + 1 low = mid + 1

@ -25,8 +25,8 @@ const (
) )
// Iterator returns a stateful iterator whose elements are key/value pairs. // Iterator returns a stateful iterator whose elements are key/value pairs.
func (tree *Tree) Iterator() Iterator { func (tree *Tree) Iterator() containers.ReverseIteratorWithKey {
return Iterator{tree: tree, node: nil, position: begin} 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. // Next moves the iterator to the next element and returns true if there was a next element in the container.

Loading…
Cancel
Save