- expose comparator in binary heap

- fix comment
pull/6/head
Emir Pasic 10 years ago
parent e23a60aaae
commit f9305332a4

@ -37,7 +37,7 @@ type Interface interface {
Values() []interface{} Values() []interface{}
} }
// Returns sorted container's elements using with respect to the passed comparator. // Returns sorted container's elements with respect to the passed comparator.
// Does not effect the ordering of elements within the container. // Does not effect the ordering of elements within the container.
// Uses timsort. // Uses timsort.
func GetSortedValues(container Interface, comparator utils.Comparator) []interface{} { func GetSortedValues(container Interface, comparator utils.Comparator) []interface{} {

@ -45,22 +45,22 @@ func assertInterfaceImplementation() {
type Heap struct { type Heap struct {
list *arraylist.List list *arraylist.List
comparator utils.Comparator Comparator utils.Comparator
} }
// Instantiates a new empty heap tree with the custom comparator. // Instantiates a new empty heap tree with the custom comparator.
func NewWith(comparator utils.Comparator) *Heap { func NewWith(comparator utils.Comparator) *Heap {
return &Heap{list: arraylist.New(), comparator: comparator} return &Heap{list: arraylist.New(), Comparator: comparator}
} }
// Instantiates a new empty heap with the IntComparator, i.e. elements are of type int. // Instantiates a new empty heap with the IntComparator, i.e. elements are of type int.
func NewWithIntComparator() *Heap { func NewWithIntComparator() *Heap {
return &Heap{list: arraylist.New(), comparator: utils.IntComparator} return &Heap{list: arraylist.New(), Comparator: utils.IntComparator}
} }
// Instantiates a new empty heap with the StringComparator, i.e. elements are of type string. // Instantiates a new empty heap with the StringComparator, i.e. elements are of type string.
func NewWithStringComparator() *Heap { func NewWithStringComparator() *Heap {
return &Heap{list: arraylist.New(), comparator: utils.StringComparator} return &Heap{list: arraylist.New(), Comparator: utils.StringComparator}
} }
// Pushes a value onto the heap and bubbles it up accordingly. // Pushes a value onto the heap and bubbles it up accordingly.
@ -129,12 +129,12 @@ func (heap *Heap) bubbleDown() {
smallerIndex := leftIndex smallerIndex := leftIndex
leftValue, _ := heap.list.Get(leftIndex) leftValue, _ := heap.list.Get(leftIndex)
rightValue, _ := heap.list.Get(rightIndex) rightValue, _ := heap.list.Get(rightIndex)
if rightIndex < size && heap.comparator(leftValue, rightValue) > 0 { if rightIndex < size && heap.Comparator(leftValue, rightValue) > 0 {
smallerIndex = rightIndex smallerIndex = rightIndex
} }
indexValue, _ := heap.list.Get(index) indexValue, _ := heap.list.Get(index)
smallerValue, _ := heap.list.Get(smallerIndex) smallerValue, _ := heap.list.Get(smallerIndex)
if heap.comparator(indexValue, smallerValue) > 0 { if heap.Comparator(indexValue, smallerValue) > 0 {
heap.list.Swap(index, smallerIndex) heap.list.Swap(index, smallerIndex)
} else { } else {
break break
@ -151,7 +151,7 @@ func (heap *Heap) bubbleUp() {
for parentIndex := (index - 1) >> 1; index > 0; parentIndex = (index - 1) >> 1 { for parentIndex := (index - 1) >> 1; index > 0; parentIndex = (index - 1) >> 1 {
indexValue, _ := heap.list.Get(index) indexValue, _ := heap.list.Get(index)
parentValue, _ := heap.list.Get(parentIndex) parentValue, _ := heap.list.Get(parentIndex)
if heap.comparator(parentValue, indexValue) <= 0 { if heap.Comparator(parentValue, indexValue) <= 0 {
break break
} }
heap.list.Swap(index, parentIndex) heap.list.Swap(index, parentIndex)

Loading…
Cancel
Save