Class: Heap::BinaryHeap::MinHeap

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

Overview

Binary Heap with min 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_minObject



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

def extract_min
  @elements[0]
end

#extract_min!Object



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

def extract_min!
  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_min.rb', line 16

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