Method: CompSci::Heap#heap?

Defined in:
lib/compsci/heap.rb

#heap?(idx: 0) ⇒ Boolean

  • not used internally

  • checks that every node satisfies the heap property

  • calls heapish? on idx’s children and then heap? on them recursively

Returns:

  • (Boolean)


113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/compsci/heap.rb', line 113

def heap?(idx: 0)
  check_children = []
  self.class.children_idx(idx, @child_slots).each { |cidx|
    # cidx is arithmetically produced; the corresponding child may not exist
    if cidx < @array.size
      return false unless self.heapish?(idx, cidx)
      check_children << cidx
    end
  }
  check_children.each { |cidx| return false unless self.heap?(idx: cidx) }
  true
end