heap

package
v0.1.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 29, 2024 License: MIT Imports: 1 Imported by: 0

README

Binary heap

func Example() {
	pq := New2[int, string](3)
	pq.Push(1, "1")
	pq.Push(9, "9")
	pq.Push(9, "9")
	pq.Push(7, "7")
	for pq.Size() > 0 {
		fmt.Print(pq.Pop())
	}
	fmt.Println()

	pq.Push(100, "1")
	pq.Push(9, "9")
	pq.Push(9, "9")
	pq.Push(7, "7")
	pq.Push(0, "x")
	pq.Del("x")
	pq.Fix(1, "1")
	for pq.Size() > 0 {
		fmt.Print(pq.Pop())
	}

	// Output:
	// 1799
	// 1799
}

Documentation

Overview

Example
pq := New2[int, string](3)
pq.Push(1, "1")
pq.Push(9, "9")
pq.Push(9, "9")
pq.Push(7, "7")
for pq.Size() > 0 {
	fmt.Print(pq.Pop())
}
fmt.Println()

pq.Push(100, "1")
pq.Push(9, "9")
pq.Push(9, "9")
pq.Push(7, "7")
pq.Push(0, "x")
pq.Del("x")
pq.Fix(1, "1")
for pq.Size() > 0 {
	fmt.Print(pq.Pop())
}
Output:

1799
1799

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Elem

type Elem[O constraints.Ordered, T any] struct {
	// contains filtered or unexported fields
}

type Heap

type Heap[O constraints.Ordered, T any] struct {
	// contains filtered or unexported fields
}

func New

func New[O constraints.Ordered, T any](cap uint) *Heap[O, T]

New make an empty heap. Specify a proper cap to reduce times of re-allocate elems slice

func (*Heap[O, T]) Cap

func (h *Heap[O, T]) Cap() int

func (*Heap[O, T]) FixTop added in v0.1.1

func (h *Heap[O, T]) FixTop(p O)

func (*Heap[O, T]) Pop

func (h *Heap[O, T]) Pop() T

func (*Heap[O, T]) Push

func (h *Heap[O, T]) Push(p O, v T)

func (*Heap[O, T]) Size

func (h *Heap[O, T]) Size() int

func (*Heap[O, T]) Top added in v0.1.1

func (h *Heap[O, T]) Top() T

func (*Heap[O, T]) TopPtr added in v0.1.1

func (h *Heap[O, T]) TopPtr() *Elem[O, T]

type Heap2

type Heap2[P constraints.Ordered, V comparable] struct {
	Heap[P, V]
}

func New2

func New2[P constraints.Ordered, V comparable](cap uint) *Heap2[P, V]

func (*Heap2[P, V]) Del

func (h *Heap2[P, V]) Del(v V)

func (*Heap2[P, V]) Fix

func (h *Heap2[P, V]) Fix(p P, v V)

Fix priority

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL