heap

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Jan 1, 2025 License: MIT Imports: 3 Imported by: 0

Documentation

Overview

Package heap implements various heap data structures

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type BinomialHeap

type BinomialHeap struct {
	// contains filtered or unexported fields
}

BinomialHeap represents a binomial heap data structure

func NewBinomialHeap

func NewBinomialHeap() *BinomialHeap

NewBinomialHeap creates and returns a new instance of BinomialHeap

func (*BinomialHeap) Extract

func (bh *BinomialHeap) Extract() (int, error)

Extract removes and returns the minimum element from the heap

func (*BinomialHeap) Insert

func (bh *BinomialHeap) Insert(value int)

Insert adds a new value to the binomial heap

func (*BinomialHeap) IsEmpty

func (bh *BinomialHeap) IsEmpty() bool

IsEmpty returns true if the heap contains no elements

func (*BinomialHeap) Peek

func (bh *BinomialHeap) Peek() (int, error)

Peek returns the minimum element without removing it

func (*BinomialHeap) Size

func (bh *BinomialHeap) Size() int

Size returns the number of elements in the heap

type BinomialNode

type BinomialNode struct {
	// contains filtered or unexported fields
}

BinomialNode represents a node in the binomial heap

type FibNode

type FibNode struct {
	// contains filtered or unexported fields
}

FibNode represents a node in the Fibonacci heap

type FibonacciHeap

type FibonacciHeap struct {
	// contains filtered or unexported fields
}

FibonacciHeap represents a Fibonacci heap data structure

func NewFibonacciHeap

func NewFibonacciHeap() *FibonacciHeap

NewFibonacciHeap creates and returns a new instance of FibonacciHeap

func (*FibonacciHeap) DecreaseKey

func (fh *FibonacciHeap) DecreaseKey(node *FibNode, newKey int) error

DecreaseKey decreases the key of a node

func (*FibonacciHeap) Extract

func (fh *FibonacciHeap) Extract() (int, error)

Extract removes and returns the minimum element from the heap

func (*FibonacciHeap) Insert

func (fh *FibonacciHeap) Insert(value int)

Insert adds a new value to the Fibonacci heap

func (*FibonacciHeap) IsEmpty

func (fh *FibonacciHeap) IsEmpty() bool

IsEmpty returns true if the heap contains no elements

func (*FibonacciHeap) Peek

func (fh *FibonacciHeap) Peek() (int, error)

Peek returns the minimum element without removing it

func (*FibonacciHeap) Size

func (fh *FibonacciHeap) Size() int

Size returns the number of elements in the heap

type HeapType

type HeapType int

HeapType represents the type of heap (MinHeap or MaxHeap)

const (
	// MinHeap represents a min heap where the root is the minimum element
	MinHeap HeapType = iota
	// MaxHeap represents a max heap where the root is the maximum element
	MaxHeap
)

type IHeap

type IHeap interface {
	// Insert adds a new element to the heap
	Insert(value int)

	// Extract removes and returns the root element (min for MinHeap, max for MaxHeap)
	Extract() (int, error)

	// Peek returns the root element without removing it
	Peek() (int, error)

	// Size returns the number of elements in the heap
	Size() int

	// IsEmpty returns true if the heap has no elements
	IsEmpty() bool
}

IHeap interface defines the basic operations that all heap implementations must provide

type Item

type Item struct {
	Value    interface{}
	Priority int
	Index    int // Insertion order for FIFO behavior
}

Item represents an element in the priority queue with a value and priority

type MaxHeapImpl

type MaxHeapImpl struct {
	// contains filtered or unexported fields
}

MaxHeapImpl represents a binary heap data structure that maintains the max-heap property

func NewMaxHeap

func NewMaxHeap() *MaxHeapImpl

NewMaxHeap creates and returns a new instance of MaxHeapImpl

func (*MaxHeapImpl) Extract

func (h *MaxHeapImpl) Extract() (int, error)

Extract removes and returns the maximum element from the heap

func (*MaxHeapImpl) Insert

func (h *MaxHeapImpl) Insert(value int)

Insert adds a new value to the heap while maintaining the max-heap property

func (*MaxHeapImpl) IsEmpty

func (h *MaxHeapImpl) IsEmpty() bool

IsEmpty returns true if the heap contains no elements

func (*MaxHeapImpl) Peek

func (h *MaxHeapImpl) Peek() (int, error)

Peek returns the maximum element without removing it

func (*MaxHeapImpl) Size

func (h *MaxHeapImpl) Size() int

Size returns the number of elements in the heap

type MinHeapImpl

type MinHeapImpl struct {
	// contains filtered or unexported fields
}

MinHeapImpl represents a binary heap data structure that maintains the min-heap property

func NewMinHeap

func NewMinHeap() *MinHeapImpl

NewMinHeap creates and returns a new instance of MinHeapImpl

func (*MinHeapImpl) Extract

func (h *MinHeapImpl) Extract() (int, error)

Extract removes and returns the minimum element from the heap

func (*MinHeapImpl) Insert

func (h *MinHeapImpl) Insert(value int)

Insert adds a new value to the heap while maintaining the min-heap property

func (*MinHeapImpl) IsEmpty

func (h *MinHeapImpl) IsEmpty() bool

IsEmpty returns true if the heap contains no elements

func (*MinHeapImpl) Peek

func (h *MinHeapImpl) Peek() (int, error)

Peek returns the minimum element without removing it

func (*MinHeapImpl) Size

func (h *MinHeapImpl) Size() int

Size returns the number of elements in the heap

type PriorityQueue

type PriorityQueue struct {
	// contains filtered or unexported fields
}

PriorityQueue implements a priority queue using a min heap

func NewPriorityQueue

func NewPriorityQueue() *PriorityQueue

NewPriorityQueue creates and returns a new instance of PriorityQueue

func (*PriorityQueue) Dequeue

func (pq *PriorityQueue) Dequeue() (interface{}, error)

Dequeue removes and returns the highest priority item

func (*PriorityQueue) Enqueue

func (pq *PriorityQueue) Enqueue(value interface{}, priority int)

Enqueue adds a new item to the priority queue

func (*PriorityQueue) IsEmpty

func (pq *PriorityQueue) IsEmpty() bool

IsEmpty returns true if the priority queue contains no elements

func (*PriorityQueue) Peek

func (pq *PriorityQueue) Peek() (interface{}, error)

Peek returns the highest priority item without removing it

func (*PriorityQueue) Size

func (pq *PriorityQueue) Size() int

Size returns the number of elements in the priority queue

Jump to

Keyboard shortcuts

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