File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -27,6 +27,11 @@ def peek
2727 def size
2828 @contents . size
2929 end
30+
31+ # @returns [Boolean] true if the heap is empty, false otherwise.
32+ def empty?
33+ @contents . empty?
34+ end
3035
3136 # Removes and returns the smallest element in the heap, or nil if the heap is empty.
3237 #
Original file line number Diff line number Diff line change 2020 it "should report its size as zero" do
2121 expect ( priority_heap . size ) . to be ( :zero? )
2222 end
23+
24+ it "should report as empty" do
25+ expect ( priority_heap ) . to be ( :empty? )
26+ end
2327 end
2428
2529 it "returns the same element after inserting a single element" do
2933 expect ( priority_heap . size ) . to be ( :zero? )
3034 end
3135
36+ with "#empty?" do
37+ it "should return false when heap contains elements" do
38+ priority_heap . push ( 1 )
39+ expect ( priority_heap ) . not . to be ( :empty? )
40+ end
41+
42+ it "should return true after popping all elements" do
43+ priority_heap . push ( 1 )
44+ priority_heap . push ( 2 )
45+ expect ( priority_heap ) . not . to be ( :empty? )
46+
47+ priority_heap . pop
48+ expect ( priority_heap ) . not . to be ( :empty? )
49+
50+ priority_heap . pop
51+ expect ( priority_heap ) . to be ( :empty? )
52+ end
53+ end
54+
3255 it "should return inserted elements in ascending order no matter the insertion order" do
3356 ( 1 ..10 ) . to_a . shuffle . each do |e |
3457 priority_heap . push ( e )
You can’t perform that action at this time.
0 commit comments