Skip to content

Commit

Permalink
PQ: min pq added
Browse files Browse the repository at this point in the history
  • Loading branch information
arnauddri committed Feb 2, 2015
1 parent e670512 commit a0ae986
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 6 deletions.
10 changes: 7 additions & 3 deletions data-structures/priority-queue/priority_queue.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package pq

import (
"fmt"
"github.com/arnauddri/algorithms/data-structures/heap"
"github.com/arnauddri/algorithms/data-structures/queue"
)
Expand All @@ -26,12 +25,18 @@ type PQ struct {
data heap.Heap
}

func New() (q *PQ) {
func NewMax() (q *PQ) {
return &PQ{
data: *heap.NewMax(),
}
}

func NewMin() (q *PQ) {
return &PQ{
data: *heap.NewMin(),
}
}

func (pq *PQ) Len() int {
return pq.data.Len()
}
Expand All @@ -50,7 +55,6 @@ func (pq *PQ) ChangePriority(val interface{}, priority int) {
popped := pq.Extract()

for val != popped.Value {
fmt.Println(val, popped.Value)
if pq.Len() == 0 {
panic("Item not found")
}
Expand Down
33 changes: 30 additions & 3 deletions data-structures/priority-queue/priority_queue_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import (
"testing"
)

func TestPriorityQueue(t *testing.T) {
h := New()
func TestMaxPriorityQueue(t *testing.T) {
h := NewMax()

h.Insert(*NewItem(8, 10))
h.Insert(*NewItem(7, 11))
Expand All @@ -32,8 +32,35 @@ func TestPriorityQueue(t *testing.T) {
}
}

func TestMinPriorityQueue(t *testing.T) {
h := NewMin()

h.Insert(*NewItem(8, 10))
h.Insert(*NewItem(7, 11))
h.Insert(*NewItem(6, 12))
h.Insert(*NewItem(3, 13))
h.Insert(*NewItem(1, 14))
h.Insert(*NewItem(0, 15))
h.Insert(*NewItem(2, 16))
h.Insert(*NewItem(4, 17))
h.Insert(*NewItem(9, 18))
h.Insert(*NewItem(5, 19))

sorted := make([]Item, 0)
for h.Len() > 0 {
sorted = append(sorted, h.Extract())
}

for i := 0; i < len(sorted)-2; i++ {
if sorted[i].Priority > sorted[i+1].Priority {
fmt.Println(sorted)
t.Error()
}
}
}

func TestChangePriority(t *testing.T) {
h := New()
h := NewMax()

h.Insert(*NewItem(8, 10))
h.Insert(*NewItem(7, 11))
Expand Down

0 comments on commit a0ae986

Please sign in to comment.