class Crystalg::DataStructures::PriorityQueue(A)
- Crystalg::DataStructures::PriorityQueue(A)
- Reference
- Object
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.crConstructors
-
.new(order : Symbol = :min)
Creates a new
PriorityQueue
.
Instance Method Summary
-
#empty? : Bool
Returns true if PriorityQueue is empty.
-
#pop
Removes the highest priority value based on
order
property. -
#pop! : A | Nil
Removes the highest priority value based on
order
property and returns the removed value. -
#push(val : A)
Append.
- #size : UInt32
-
#top : A | Nil
Returns the highest priority value without removing the value.
Constructor Detail
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)
Instance Method Detail
Removes the highest priority value based on order
property. This method returns self, so several calls can be chained.
Removes the highest priority value based on order
property and returns the removed value. Returns nil if PriorityQueue is empty.
Returns the highest priority value without removing the value. Returns nil if PriorityQueue is empty.