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