Permalink
Browse files

Add a next_key method to heap

  • Loading branch information...
1 parent 58a488d commit 2284ba91512aece08d98501bc81b68154a625438 @loganb loganb committed Apr 9, 2012
Showing with 21 additions and 0 deletions.
  1. +16 −0 lib/containers/heap.rb
  2. +5 −0 spec/heap_spec.rb
@@ -118,6 +118,22 @@ def next
end
# call-seq:
+ # next_key -> key
+ # next_key -> nil
+ #
+ # Returns the key associated with the next item in heap order, but does not remove the value.
+ #
+ # Complexity: O(1)
+ #
+ # minheap = MinHeap.new
+ # minheap.push(1, :a)
+ # minheap.next_key #=> 1
+ #
+ def next_key
+ @next && @next.key
+ end
+
+ # call-seq:
# clear -> nil
#
# Removes all elements from the heap, destructively.
View
@@ -46,6 +46,11 @@
@heap.size.should eql(@num_items)
end
+ it "should have a next value" do
+ @heap.next.should be_true
+ @heap.next_key.should be_true
+ end
+
it "should delete random keys" do
@heap.delete(@random_array[0]).should eql(@random_array[0])
@heap.delete(@random_array[1]).should eql(@random_array[1])

0 comments on commit 2284ba9

Please sign in to comment.