Class: Heap::BinaryHeap::MaxHeap

Inherits:
BinaryHeap show all
Defined in:
lib/Heap/binary_heap/binary_heap_max.rb

Overview

Binary Heap with max root

Instance Attribute Summary

Attributes inherited from BinaryHeap

#elements

Instance Method Summary collapse

Methods inherited from BinaryHeap

#add, #count, #initialize

Constructor Details

This class inherits a constructor from Heap::BinaryHeap::BinaryHeap

Instance Method Details

#extract_maxObject



5
6
7
# File 'lib/Heap/binary_heap/binary_heap_max.rb', line 5

def extract_max
  @elements[0]
end

#extract_max!Object



9
10
11
12
13
14
# File 'lib/Heap/binary_heap/binary_heap_max.rb', line 9

def extract_max!
  swap(1, count)
  el = @elements.pop
  swim_down(1)
  el
end

#sortObject



16
17
18
19
20
21
22
# File 'lib/Heap/binary_heap/binary_heap_max.rb', line 16

def sort
  el_temp = @elements.clone
  result = []
  result.push extract_max! while count > 0
  @elements = el_temp
  result
end