File tree Expand file tree Collapse file tree 3 files changed +16
-17
lines changed Expand file tree Collapse file tree 3 files changed +16
-17
lines changed Original file line number Diff line number Diff line change 13
13
h = Heap . new ( child_slots : 3 )
14
14
15
15
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 } "
17
17
puts "heap: #{ h . heap? } "
18
18
puts h
19
19
puts
20
20
puts
21
21
22
22
puts "pop: %i" % h . pop
23
- puts "array: #{ h . array . inspect } "
23
+ puts "array: #{ h . tree . array . inspect } "
24
24
puts "heap: #{ h . heap? } "
25
25
puts h
26
26
puts
27
27
puts
28
28
29
29
puts "pop: %s" % Array . new ( 9 ) { h . pop } . join ( ' ' )
30
- puts "array: #{ h . array . inspect } "
30
+ puts "array: #{ h . tree . array . inspect } "
31
31
puts "heap: #{ h . heap? } "
32
32
puts h
33
33
puts
34
34
puts
35
35
36
36
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 } "
38
38
puts "heap: #{ h . heap? } "
39
39
puts h
40
40
puts
51
51
h = Heap . new ( child_slots : 2 )
52
52
53
53
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 } "
55
55
puts "heap: #{ h . heap? } "
56
56
puts h
57
57
puts
58
58
puts
59
59
60
60
puts "pop: %i" % h . pop
61
- puts "array: #{ h . array . inspect } "
61
+ puts "array: #{ h . tree . array . inspect } "
62
62
puts "heap: #{ h . heap? } "
63
63
puts h
64
64
puts
65
65
puts
66
66
67
67
puts "pop: %s" % Array . new ( 9 ) { h . pop } . join ( ' ' )
68
- puts "array: #{ h . array . inspect } "
68
+ puts "array: #{ h . tree . array . inspect } "
69
69
puts "heap: #{ h . heap? } "
70
70
puts h
71
71
puts
72
72
puts
73
73
74
74
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 } "
76
76
puts "heap: #{ h . heap? } "
77
77
puts h
78
78
puts
Original file line number Diff line number Diff line change @@ -56,12 +56,6 @@ def pop
56
56
node
57
57
end
58
58
59
- # * return what pop would return (avoid sifting)
60
- #
61
- def peek
62
- @tree . first
63
- end
64
-
65
59
# * called recursively
66
60
# * idx represents the node suspected to violate the heap
67
61
# * intended to be O(log n) on heap size (log base child_slots)
@@ -125,5 +119,10 @@ def heap?(idx: 0)
125
119
check_children . each { |cidx | return false unless self . heap? ( idx : cidx ) }
126
120
true
127
121
end
122
+
123
+ def size
124
+ @tree . size
125
+ end
126
+ alias_method :count , :size
128
127
end
129
128
end
Original file line number Diff line number Diff line change 32
32
33
33
it "must pop" do
34
34
expect ( @maxheap . pop ) . must_equal 10
35
- expect ( @maxheap . peek ) . wont_equal 10
35
+ expect ( @maxheap . tree . last ) . wont_equal 10
36
36
expect ( @maxheap . heap? ) . must_equal true
37
37
end
38
38
74
74
75
75
it "must pop" do
76
76
expect ( @minheap . pop ) . must_equal 1
77
- expect ( @minheap . peek ) . wont_equal 1
77
+ expect ( @minheap . tree . last ) . wont_equal 1
78
78
expect ( @minheap . heap? ) . must_equal true
79
79
end
80
80
117
117
118
118
it "must pop" do
119
119
expect ( @heap3 . pop ) . must_equal 10
120
- expect ( @heap3 . peek ) . wont_equal 10
120
+ expect ( @heap3 . tree . last ) . wont_equal 10
121
121
expect ( @heap3 . heap? ) . must_equal true
122
122
end
123
123
You can’t perform that action at this time.
0 commit comments