Skip to content

Commit

Permalink
remove Heap#peek; add Heap#size and #count alias
Browse files Browse the repository at this point in the history
  • Loading branch information
rickhull committed Jun 20, 2024
1 parent df5544b commit 4ae94c1
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 17 deletions.
16 changes: 8 additions & 8 deletions examples/heap.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,28 +13,28 @@
h = Heap.new(child_slots: 3)

puts "push: %s" % Array.new(30) { rand(99).tap { |i| h.push i } }.join(' ')
puts "array: #{h.array.inspect}"
puts "array: #{h.tree.array.inspect}"
puts "heap: #{h.heap?}"
puts h
puts
puts

puts "pop: %i" % h.pop
puts "array: #{h.array.inspect}"
puts "array: #{h.tree.array.inspect}"
puts "heap: #{h.heap?}"
puts h
puts
puts

puts "pop: %s" % Array.new(9) { h.pop }.join(' ')
puts "array: #{h.array.inspect}"
puts "array: #{h.tree.array.inspect}"
puts "heap: #{h.heap?}"
puts h
puts
puts

puts "push: %s" % Array.new(30) { rand(99).tap { |i| h.push i } }.join(' ')
puts "array: #{h.array.inspect}"
puts "array: #{h.tree.array.inspect}"
puts "heap: #{h.heap?}"
puts h
puts
Expand All @@ -51,28 +51,28 @@
h = Heap.new(child_slots: 2)

puts "push: %s" % Array.new(30) { rand(99).tap { |i| h.push i } }.join(' ')
puts "array: #{h.array.inspect}"
puts "array: #{h.tree.array.inspect}"
puts "heap: #{h.heap?}"
puts h
puts
puts

puts "pop: %i" % h.pop
puts "array: #{h.array.inspect}"
puts "array: #{h.tree.array.inspect}"
puts "heap: #{h.heap?}"
puts h
puts
puts

puts "pop: %s" % Array.new(9) { h.pop }.join(' ')
puts "array: #{h.array.inspect}"
puts "array: #{h.tree.array.inspect}"
puts "heap: #{h.heap?}"
puts h
puts
puts

puts "push: %s" % Array.new(30) { rand(99).tap { |i| h.push i } }.join(' ')
puts "array: #{h.array.inspect}"
puts "array: #{h.tree.array.inspect}"
puts "heap: #{h.heap?}"
puts h
puts
11 changes: 5 additions & 6 deletions lib/compsci/heap.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,6 @@ def pop
node
end

# * return what pop would return (avoid sifting)
#
def peek
@tree.first
end

# * called recursively
# * idx represents the node suspected to violate the heap
# * intended to be O(log n) on heap size (log base child_slots)
Expand Down Expand Up @@ -125,5 +119,10 @@ def heap?(idx: 0)
check_children.each { |cidx| return false unless self.heap?(idx: cidx) }
true
end

def size
@tree.size
end
alias_method :count, :size
end
end
6 changes: 3 additions & 3 deletions test/heap.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@

it "must pop" do
expect(@maxheap.pop).must_equal 10
expect(@maxheap.peek).wont_equal 10
expect(@maxheap.tree.last).wont_equal 10
expect(@maxheap.heap?).must_equal true
end

Expand Down Expand Up @@ -74,7 +74,7 @@

it "must pop" do
expect(@minheap.pop).must_equal 1
expect(@minheap.peek).wont_equal 1
expect(@minheap.tree.last).wont_equal 1
expect(@minheap.heap?).must_equal true
end

Expand Down Expand Up @@ -117,7 +117,7 @@

it "must pop" do
expect(@heap3.pop).must_equal 10
expect(@heap3.peek).wont_equal 10
expect(@heap3.tree.last).wont_equal 10
expect(@heap3.heap?).must_equal true
end

Expand Down

0 comments on commit 4ae94c1

Please sign in to comment.