Skip to content

Commit 4ae94c1

Browse files
committed
remove Heap#peek; add Heap#size and #count alias
1 parent df5544b commit 4ae94c1

File tree

3 files changed

+16
-17
lines changed

3 files changed

+16
-17
lines changed

examples/heap.rb

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,28 +13,28 @@
1313
h = Heap.new(child_slots: 3)
1414

1515
puts "push: %s" % Array.new(30) { rand(99).tap { |i| h.push i } }.join(' ')
16-
puts "array: #{h.array.inspect}"
16+
puts "array: #{h.tree.array.inspect}"
1717
puts "heap: #{h.heap?}"
1818
puts h
1919
puts
2020
puts
2121

2222
puts "pop: %i" % h.pop
23-
puts "array: #{h.array.inspect}"
23+
puts "array: #{h.tree.array.inspect}"
2424
puts "heap: #{h.heap?}"
2525
puts h
2626
puts
2727
puts
2828

2929
puts "pop: %s" % Array.new(9) { h.pop }.join(' ')
30-
puts "array: #{h.array.inspect}"
30+
puts "array: #{h.tree.array.inspect}"
3131
puts "heap: #{h.heap?}"
3232
puts h
3333
puts
3434
puts
3535

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

5353
puts "push: %s" % Array.new(30) { rand(99).tap { |i| h.push i } }.join(' ')
54-
puts "array: #{h.array.inspect}"
54+
puts "array: #{h.tree.array.inspect}"
5555
puts "heap: #{h.heap?}"
5656
puts h
5757
puts
5858
puts
5959

6060
puts "pop: %i" % h.pop
61-
puts "array: #{h.array.inspect}"
61+
puts "array: #{h.tree.array.inspect}"
6262
puts "heap: #{h.heap?}"
6363
puts h
6464
puts
6565
puts
6666

6767
puts "pop: %s" % Array.new(9) { h.pop }.join(' ')
68-
puts "array: #{h.array.inspect}"
68+
puts "array: #{h.tree.array.inspect}"
6969
puts "heap: #{h.heap?}"
7070
puts h
7171
puts
7272
puts
7373

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

lib/compsci/heap.rb

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -56,12 +56,6 @@ def pop
5656
node
5757
end
5858

59-
# * return what pop would return (avoid sifting)
60-
#
61-
def peek
62-
@tree.first
63-
end
64-
6559
# * called recursively
6660
# * idx represents the node suspected to violate the heap
6761
# * intended to be O(log n) on heap size (log base child_slots)
@@ -125,5 +119,10 @@ def heap?(idx: 0)
125119
check_children.each { |cidx| return false unless self.heap?(idx: cidx) }
126120
true
127121
end
122+
123+
def size
124+
@tree.size
125+
end
126+
alias_method :count, :size
128127
end
129128
end

test/heap.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232

3333
it "must pop" do
3434
expect(@maxheap.pop).must_equal 10
35-
expect(@maxheap.peek).wont_equal 10
35+
expect(@maxheap.tree.last).wont_equal 10
3636
expect(@maxheap.heap?).must_equal true
3737
end
3838

@@ -74,7 +74,7 @@
7474

7575
it "must pop" do
7676
expect(@minheap.pop).must_equal 1
77-
expect(@minheap.peek).wont_equal 1
77+
expect(@minheap.tree.last).wont_equal 1
7878
expect(@minheap.heap?).must_equal true
7979
end
8080

@@ -117,7 +117,7 @@
117117

118118
it "must pop" do
119119
expect(@heap3.pop).must_equal 10
120-
expect(@heap3.peek).wont_equal 10
120+
expect(@heap3.tree.last).wont_equal 10
121121
expect(@heap3.heap?).must_equal true
122122
end
123123

0 commit comments

Comments
 (0)