Copyright (c) 2015, Emir Pasic. All rights reserved. Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.
Package binaryheap implements a binary heap backed by array list. Comparator defines this heap as either min or max heap. Structure is not thread safe. References: http://en.wikipedia.org/wiki/Binary_heap
package binaryheap

import (
	
	
	
	
	
)

func () {
	var  trees.Tree = (*Heap)(nil)
}
Heap holds elements in an array-list
NewWith instantiates a new empty heap tree with the custom comparator.
func ( utils.Comparator) *Heap {
	return &Heap{list: arraylist.New(), Comparator: }
}
NewWithIntComparator instantiates a new empty heap with the IntComparator, i.e. elements are of type int.
NewWithStringComparator instantiates a new empty heap with the StringComparator, i.e. elements are of type string.
Push adds a value onto the heap and bubbles it up accordingly.
func ( *Heap) ( ...interface{}) {
	if len() == 1 {
		.list.Add([0])
		.bubbleUp()
Reference: https://en.wikipedia.org/wiki/Binary_heap#Building_a_heap
		for ,  := range  {
			.list.Add()
		}
		 := .list.Size()/2 + 1
		for  := ;  >= 0; -- {
			.bubbleDownIndex()
		}
	}
}
Pop removes top element on heap and returns it, or nil if heap is empty. Second return parameter is true, unless the heap was empty and there was nothing to pop.
func ( *Heap) () ( interface{},  bool) {
	,  = .list.Get(0)
	if ! {
		return
	}
	 := .list.Size() - 1
	.list.Swap(0, )
	.list.Remove()
	.bubbleDown()
	return
}
Peek returns top element on the heap without removing it, or nil if heap is empty. Second return parameter is true, unless the heap was empty and there was nothing to peek.
func ( *Heap) () ( interface{},  bool) {
	return .list.Get(0)
}
Empty returns true if heap does not contain any elements.
func ( *Heap) () bool {
	return .list.Empty()
}
Size returns number of elements within the heap.
func ( *Heap) () int {
	return .list.Size()
}
Clear removes all elements from the heap.
func ( *Heap) () {
	.list.Clear()
}
Values returns all elements in the heap.
func ( *Heap) () []interface{} {
	return .list.Values()
}
String returns a string representation of container
func ( *Heap) () string {
	 := "BinaryHeap\n"
	 := []string{}
	for ,  := range .list.Values() {
		 = append(, fmt.Sprintf("%v", ))
	}
	 += strings.Join(, ", ")
	return 
}
Performs the "bubble down" operation. This is to place the element that is at the root of the heap in its correct place so that the heap maintains the min/max-heap order property.
func ( *Heap) () {
	.bubbleDownIndex(0)
}
Performs the "bubble down" operation. This is to place the element that is at the index of the heap in its correct place so that the heap maintains the min/max-heap order property.
func ( *Heap) ( int) {
	 := .list.Size()
	for  := <<1 + 1;  < ;  = <<1 + 1 {
		 := <<1 + 2
		 := 
		,  := .list.Get()
		,  := .list.Get()
		if  <  && .Comparator(, ) > 0 {
			 = 
		}
		,  := .list.Get()
		,  := .list.Get()
		if .Comparator(, ) > 0 {
			.list.Swap(, )
		} else {
			break
		}
		 = 
	}
}
Performs the "bubble up" operation. This is to place a newly inserted element (i.e. last element in the list) in its correct place so that the heap maintains the min/max-heap order property.
func ( *Heap) () {
	 := .list.Size() - 1
	for  := ( - 1) >> 1;  > 0;  = ( - 1) >> 1 {
		,  := .list.Get()
		,  := .list.Get()
		if .Comparator(, ) <= 0 {
			break
		}
		.list.Swap(, )
		 = 
	}
}
Check that the index is within bounds of the list
func ( *Heap) ( int) bool {
	return  >= 0 &&  < .list.Size()