lib/compsci/heap.rb in compsci-0.3.0.1 vs lib/compsci/heap.rb in compsci-0.3.1.1
- old
+ new
@@ -16,11 +16,11 @@
# reestablish the heap property.
# * Sift_up and sift_down are O(logb n) because they only have to check and
# swap nodes at each layer of the tree, and there are log(n, base b) layers
# to the tree.
#
-class CompSci::Heap < CompSci::CompleteNaryTree
+class CompSci::Heap < CompSci::CompleteTree
# * defaults to a MaxHeap, with the largest node at the root
# * specify a minheap with minheap: true or cmp_val: -1
#
def initialize(cmp_val: 1, minheap: false, child_slots: 2)
super(child_slots: child_slots)
@@ -63,10 +63,9 @@
# * idx represents the node suspected to violate the heap
# * intended to be O(log n) on heap size (log base child_slots)
#
def sift_up(idx)
return self if idx <= 0
- # print '.'
pidx = self.class.parent_idx(idx, @child_slots)
if !self.heapish?(pidx, idx)
@array[idx], @array[pidx] = @array[pidx], @array[idx] # swap
self.sift_up(pidx)
end