class Crystalg::DataStructures::PriorityQueue(A)

Overview

A PriorityQueue is a queue ordered by priority of elements.

min_heap = PriorityQueue(Int32).new
[10, 8 , 2, 11].each { |e| min_heap.push(e) }
min_heap.top # => 2
min_heap.pop().top() # => 8

min_heap.pop! # => 8
min_heap.top # => 10

max_heap = PriorityQueue(Int32).new(:max)
[10, 8 , 2, 11].each { |e| max_heap.push(e) }
max_heap.pop! # => 11
max_heap.pop! # => 10
max_heap.pop! # => 8
max_heap.pop! # => 2

Defined in:

crystalg/data_structures/priority_queue.cr

Constructors

Instance Method Summary

Constructor Detail

def self.new(order : Symbol = :min) #

Creates a new PriorityQueue. The order parameter must be :min or :max.

queue = PriorityQueue(Int32).new
queue = PriorityQueue(Int32).new(:min)
queue = PriorityQueue(Int32).new(:max)

[View source]

Instance Method Detail

def empty? : Bool #

Returns true if PriorityQueue is empty.


[View source]
def pop #

Removes the highest priority value based on order property. This method returns self, so several calls can be chained.


[View source]
def pop! : A | Nil #

Removes the highest priority value based on order property and returns the removed value. Returns nil if PriorityQueue is empty.


[View source]
def push(val : A) #

Append. This method returns self, so several calls can be chained.


[View source]
def size : UInt32 #

[View source]
def top : A | Nil #

Returns the highest priority value without removing the value. Returns nil if PriorityQueue is empty.


[View source]