heap

package
v0.0.0-...-381b1be Latest Latest
Warning

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

Go to latest
Published: Mar 21, 2024 License: MIT Imports: 3 Imported by: 1

README

Binary Heap


Data Structure

A binary heap is a heap data struc­ture cre­ated using a binary tree.

Binary heap has two rules:
  • Shape prop­erty: Binary Heap has to be com­plete binary tree at all lev­els except the last level.
  • Heap prop­erty: All nodes are either greater than equal to (Max-Heap) or less than equal to (Min-Heap) to each of its child nodes.
Implementation:
  • Use array to store the data.
  • Start stor­ing from index 1, not 0.
  • For any given node at posi­tion [i]:
    • its Left Child is at [2*i] if available.
    • its Right Child is at [2*i+1] if available.
    • its Par­ent Node is at [i/2] if avail­able.


Heap Operations

Insert
  • Add the ele­ment at the bot­tom leaf of the Heap.
  • Per­form the Bubble-Up operation: ~ O(log n)
    • Bubble-Up a.k.a up-heap, percolate-up, sift-up, trickle-up, heapify-up, or cascade-up
    • If inserted ele­ment is smaller than its par­ent node in case of Min-Heap OR greater than its par­ent node in case of Max-Heap, swap the ele­ment with its parent.
    • Keep repeat­ing the above step, if node reaches its cor­rect posi­tion, STOP.
Extract-Min OR Extract-Max Operation
  • Take out the ele­ment from the root.( it will be min­i­mum in case of Min-Heap and max­i­mum in case of Max-Heap).
  • Take out the last ele­ment from the last level from the heap and replace the root with the element.
  • Per­form Sink-Down
Delete
  • Find the index for the ele­ment to be deleted.
  • Take out the last ele­ment from the last level from the heap and replace the index with this element.
  • Per­form Sink-Down operation: ~ O(log n)
    • If replaced ele­ment is greater than any of its child node in case of Min-Heap OR smaller than any if its child node in case of Max-Heap, swap the ele­ment with its small­est child (Min-Heap) or with its great­est child (Max-Heap).
    • Keep repeat­ing the above step, if node reaches its cor­rect posi­tion, STOP.


See http://algorithms.tutorialhorizon.com/binary-min-max-heap/

Documentation

Overview

Package heap :: heap.go

Package heap :: ixHeap.go

Package heap :: minIntHeap.go

Package heap :: pq.go

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Heap

type Heap []*Item

Heap is a list of *Item

func New

func New() *Heap

New returns a new Heap

func (*Heap) ExtractMax

func (h *Heap) ExtractMax() *Item

ExtractMax removes the maximum item from heap

func (*Heap) ExtractMin

func (h *Heap) ExtractMin() *Item

ExtractMin removes the minimum item from heap

func (*Heap) Len

func (h *Heap) Len() int

Len implements sort.Interface in heap.Interface

func (*Heap) Less

func (h *Heap) Less(x, y int) bool

Less implements sort.Interface in heap.Interface

func (*Heap) PeekMax

func (h *Heap) PeekMax() *Item

PeekMax returns the maximum item from heap

func (*Heap) PeekMin

func (h *Heap) PeekMin() *Item

PeekMin returns the minimum item from heap

func (*Heap) Pop

func (h *Heap) Pop() interface{}

Pop implements heap.Interface

func (*Heap) Push

func (h *Heap) Push(x interface{})

Push implements heap.Interface

func (*Heap) Swap

func (h *Heap) Swap(x, y int)

Swap implements sort.Interface in heap.Interface

type Item

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

Item defines a heap item

type IxHeap

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

IxHeap struct

func NewIxHeap

func NewIxHeap(v ...int) *IxHeap

NewIxHeap constructs an int heap per specific capacity and optionally limit

func (*IxHeap) Add

func (h *IxHeap) Add(i int)

Add func

func (*IxHeap) Delete

func (h *IxHeap) Delete(item int) int

Delete func

func (*IxHeap) DeleteAt

func (h *IxHeap) DeleteAt(k int) int

DeleteAt func

func (*IxHeap) ExtractMax

func (h *IxHeap) ExtractMax() int

ExtractMax func

func (*IxHeap) ExtractMin

func (h *IxHeap) ExtractMin() int

ExtractMin func

func (*IxHeap) GetSize

func (h *IxHeap) GetSize() int

GetSize func returns the position of the heap

func (*IxHeap) IsEmpty

func (h *IxHeap) IsEmpty() bool

IsEmpty checks if the heap is empty

func (*IxHeap) PeekMax

func (h *IxHeap) PeekMax() (int, error)

PeekMax func

func (*IxHeap) PeekMin

func (h *IxHeap) PeekMin() (int, error)

PeekMin func

func (*IxHeap) String

func (h *IxHeap) String() string

String func

type MinIntHeap

type MinIntHeap []int64

MinIntHeap is a min-heap of integers. - Construction : h := &MinIntHeap{3, 1, 4} - Initialization: heap.Init(h) // sink down from half to top - Add an integer: heap.Push(h, 5) // call heap.Interface.Push then bubble up - Other mehotds : heap.Pop(h), heap.Fix(h, 3) - Peek min/max : (*h)[0], (*h)[len(*h)-1]

func (*MinIntHeap) ExtractMax

func (mih *MinIntHeap) ExtractMax() int64

ExtractMax removes the maximum item from heap

func (*MinIntHeap) ExtractMin

func (mih *MinIntHeap) ExtractMin() int64

ExtractMin removes the minimum item from heap

func (*MinIntHeap) Len

func (mih *MinIntHeap) Len() int

Len implements sort.Interface in heap.Interface

func (*MinIntHeap) Less

func (mih *MinIntHeap) Less(i, j int) bool

Les implements sort.Interface in heap.Interface

func (*MinIntHeap) Pop

func (mih *MinIntHeap) Pop() interface{}

Pop implements heap.Interface

func (*MinIntHeap) Push

func (mih *MinIntHeap) Push(x interface{})

Push implements heap.Interface

func (*MinIntHeap) Swap

func (mih *MinIntHeap) Swap(i, j int)

Swap implements sort.Interface in heap.Interface

type PriorityQueue

type PriorityQueue []*Item

PriorityQueue is a list of *Item

func (PriorityQueue) Len

func (pq PriorityQueue) Len() int

Len implements sort.Interface in heap.Interface

func (PriorityQueue) Less

func (pq PriorityQueue) Less(x, y int) bool

Less implements sort.Interface in heap.Interface

func (*PriorityQueue) Pop

func (pq *PriorityQueue) Pop() interface{}

Pop implements heap.Interface

func (*PriorityQueue) Push

func (pq *PriorityQueue) Push(x interface{})

Push implements heap.Interface

func (PriorityQueue) Swap

func (pq PriorityQueue) Swap(x, y int)

Swap implements sort.Interface in heap.Interface

Jump to

Keyboard shortcuts

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