Esempio n. 1
0
func heapSort(a []int) {
	arr := Array(a)
	heap.Init(arr)

	for i := len(arr) - 1; i > 0; i-- {
		arr.Swap(0, i)
		heap.Down(arr, 0, i)
	}
}
Esempio n. 2
0
func TestPriorityQueue(t *testing.T) {
	items := map[string]int{
		"apple":   1,
		"orange":  5,
		"banana":  4,
		"banana2": 6,
		"grass":   0,
	}
	pq := &PriorityQueue{}
	pq.List = make([]*Item, len(items))

	i := 0
	for key, val := range items {
		pq.List[i] = &Item{key, val}
		i++
	}

	heap.Init(pq)
	fmt.Println(pq)
	if pq.Get(0).Priority != 0 {
		t.Fail()
	}

	pq.Push(&Item{"earth", -1})
	pq.Push(&Item{"tree", 8})
	fmt.Println(pq)

	pq.Update(3, "sky", 10)
	fmt.Println(pq)

	item := pq.Pop().(*Item)
	if item.Priority != -1 {
		t.Fail()
	}

	fmt.Println(pq)
}