Class: Containers::MaxHeap
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
424 425 426 |
# File 'lib/containers/heap.rb', line 424 def initialize(ary=[]) super(ary) { |x, y| (x <=> y) == 1 } end |
Instance Method Details
#max ⇒ Object
436 437 438 |
# File 'lib/containers/heap.rb', line 436 def max self.next end |