Class: Algorithms::Containers::MinHeap
- Defined in:
- lib/containers/heap.rb
Overview
A MinHeap is a heap where the items are returned in ascending order of key value.
Instance Method Summary collapse
-
#initialize(ary = []) ⇒ MinHeap
constructor
call-seq: MinHeap.new(ary) -> new_heap.
-
#min ⇒ Object
call-seq: min -> value min -> nil.
-
#min! ⇒ Object
call-seq: min! -> value min! -> nil.
Methods inherited from Heap
#change_key, #clear, #delete, #empty?, #has_key?, #merge!, #next, #next_key, #pop, #push, #size
Constructor Details
#initialize(ary = []) ⇒ MinHeap
call-seq:
MinHeap.new(ary) -> new_heap
Creates a new MinHeap with an optional array parameter of items to insert into the heap. A MinHeap is created by calling Heap.new { |x, y| (x <=> y) == -1 }, so this is a convenience class.
minheap = MinHeap.new([1, 2, 3, 4])
minheap.pop #=> 1
minheap.pop #=> 2
476 477 478 |
# File 'lib/containers/heap.rb', line 476 def initialize(ary=[]) super(ary) { |x, y| (x <=> y) == -1 } end |
Instance Method Details
#min ⇒ Object
488 489 490 |
# File 'lib/containers/heap.rb', line 488 def min self.next end |