class Heap::MultipleHeap::MultipleHeap

Multiple Heap template

Attributes

d[R]

Public Class Methods

new(d, elements = []) click to toggle source
# File lib/Heap/multiple_heap/multiple_heap.rb, line 9
def initialize(d, elements = [])
  @elements = []
  @d = d
  add(elements.pop) until elements.empty?
end

Protected Instance Methods

get_children(index) click to toggle source
# File lib/Heap/multiple_heap/multiple_heap.rb, line 15
def get_children(index)
  child_indexes = []
  (2..(@d + 1)).each { |i| child_indexes.push((index - 1) * @d + i) }
  child_indexes.delete_if { |ind| ind > count }
  return if child_indexes.empty?

  children = {}
  child_indexes.each { |ind| children[@elements[ind - 1]] = ind }
  children
end