小顶堆
//复杂度 O(nlogn)
type MinHeap struct {
Size int
Node []int
}
func NewHeap(max int) *MinHeap {
return &MinHeap{Node: make([]int, max), Size: 0}
}
func (heap *MinHeap) Push(a int) {
i := heap.Size
heap.Size++
for {
if i <= 0 {
break
}
parent := (i - 1) / 2
if heap.Node[parent] <= a {
break
}
heap.Node[i] = heap.Node[parent]
i = parent
}
heap.Node[i] = a
}
func (heap *MinHeap) Pop() int {
if heap.Size == 0 {
return 0
}
//弹出根节点
root := heap.Node[0]
heap.Size--
last := heap.Node[heap.Size]
//fmt.Println(root, last)
i := 0
heap.Node[heap.Size] = 0
for {
left := 2*i + 1
right := 2*i + 2
//左节点超出边界 则break
if left >= heap.Size {
break
}
if right < heap.Size && heap.Node[right] < heap.Node[left] {
left = right
}
if heap.Node[left] >= last {
break
}
heap.Node[i] = heap.Node[left]
i = left
}
heap.Node[i] = last
return root
}
func (heap *MinHeap) Sort() []int {
length := len(heap.Node)
arr := make([]int, length)
for i := 0; i < length; i++ {
arr[i] = heap.Pop()
}
return arr
}
func main() {
myqueue := queue.NewQueue(100)
node1 := queue.Node{
Point: 3,
Val: "aaa",
}
node2 := queue.Node{
Point: 5,
Val: "bbb",
}
node3 := queue.Node{
Point: 10,
Val: "ccc",
}
node4 := queue.Node{
Point: 8,
Val: "ccc",
}
myqueue.Push(node1)
myqueue.Push(node2)
myqueue.Push(node3)
myqueue.Push(node4)
fmt.Println(myqueue)
fmt.Println(myqueue.Pop())
fmt.Println(myqueue)
fmt.Println(myqueue.Top())
heap := NewHeap(100)
heap.Push(3)
heap.Push(6)
heap.Push(7)
heap.Push(10)
heap.Push(1)
heap.Push(2)
heap.Push(3)
heap.Push(9)
fmt.Println(heap)
/* fmt.Println(heap.Pop())
fmt.Println(heap)
fmt.Println(heap.Pop())
fmt.Println(heap.Pop())
fmt.Println(heap.Pop())*/
/*fmt.Println(heap.Pop())
fmt.Println(heap.Pop())
fmt.Println(heap.Pop())*/
/*heap.Push(27)
heap.Push(1)
heap.Push(2)
heap.Push(3)*/
fmt.Println(heap.Sort())
}
优先级队列
//优先级队列实现
type PriorityQueueInterface interface {
GetSize() int
IsEmpty() bool
Push(node Node)
Pop() *Node
Top() *Node
}
//按照分数大小排列
type PriorityQueue struct {
Size int
Item []Node
}
type Node struct {
Point int //分数
Val string //值
}
func NewQueue(max int) PriorityQueueInterface {
return &PriorityQueue{Size: 0, Item: make([]Node, max)}
}
func (p *PriorityQueue) GetSize() int {
return p.Size
}
func (p *PriorityQueue) IsEmpty() bool {
return len(p.Item) == 0
}
func (p *PriorityQueue) Push(node Node) {
i := p.Size
p.Size++
for {
if i <= 0 {
break
}
parent := (i - 1) / 2
fmt.Println(parent, p.Item[parent], p.Item[i])
if p.Item[parent].Point >= node.Point {
break
}
p.Item[parent], p.Item[i] = p.Item[i], p.Item[parent]
i = parent
}
p.Item[i] = node
}
func (p *PriorityQueue) Pop() *Node {
if p.Size == 0 {
return nil
}
root := p.Item[0]
i := 0
p.Size--
last := p.Item[p.Size]
p.Item[p.Size] = Node{}
for {
left := 2*i + 1
right := 2*i + 2
if left >= p.Size {
break
}
if right > p.Size && p.Item[left].Point < p.Item[right].Point {
left = right
}
if p.Item[left].Point < last.Point {
break
}
p.Item[i], p.Item[left] = p.Item[left], p.Item[i]
i = left
}
p.Item[i] = last
return &root
}
func (p *PriorityQueue) Top() *Node {
return &p.Item[0]
}