
Русская версия
Heap (ruby heapsort)
Gem is using for making Heaps.
Installation
Add this line to your application's Gemfile:
gem 'ruby-heap'
And then execute:
$ bundle
Or install it yourself as:
$ gem install ruby-heap
Usage
Binary Heaps
Binary Heap with min root
While Heap initialize you can add any comparable object in it (not numbers only).
Objects must have compare functions (>, >=, <, <=).
require 'Heap'
b_heap = Heap::BinaryHeap::MinHeap.new([2, 3, 1, -1])
b_heap.elements
b_heap.sort
b_heap.count
b_heap.extract_min
b_heap.extract_min!
b_heap.count
b_heap.elements
b_heap.add -1
b_heap.elements
b_heap.add [0, 9, 200, -15, 6]
b_heap.elements
b_heap.sort
Same practice with max root
require 'Heap'
b_heap = Heap::BinaryHeap::MaxHeap.new([2, 3, 1, -1])
b_heap.elements
b_heap.sort
b_heap.count
b_heap.extract_max
b_heap.extract_max!
b_heap.count
b_heap.elements
b_heap.add -1
b_heap.elements
b_heap.add [0, 9, 200, -15, 6]
b_heap.elements
b_heap.sort
Heap merging
require 'Heap'
min_heap = Heap::BinaryHeap::MinHeap.new [1, 2, 3]
max_heap = Heap::BinaryHeap::MaxHeap.new [9, -1, 4]
min_heap.add max_heap
min_heap.count
min_heap.sort
Multiple Heaps
Multiple (d-ary) heaps have same methods as binary. But initialize differs:
require 'Heap'
min_heap = Heap::MultipleHeap::MinHeap.new(5, [10, 20, 30])
max_heap = Heap::MultipleHeap::MaxHeap.new(7)
Contributing
Bug reports and pull requests are welcome on GitHub at Project page.
License
The gem is available as open source under the terms of the MIT License.