class

Crystalg::DataStructures::PriorityQueue(A)

Inherits Reference < Object

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

Constructors

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)
Source

Instance methods

empty?

Returns true if PriorityQueue is empty.

Source
pop

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

Source
pop!

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

Source
push(val : A)

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

Source
size
Source
top

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

Source