-
Notifications
You must be signed in to change notification settings - Fork 0
/
heap.rb
78 lines (60 loc) · 1.29 KB
/
heap.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
require 'compsci/heap'
include CompSci
puts <<EOF
#
# display the results of ternary Heap push and pop
#
EOF
h = Heap.new(child_slots: 3)
puts "push: %s" % Array.new(30) { rand(99).tap { |i| h.push i } }.join(' ')
puts "array: #{h.tree.array.inspect}"
puts "heap: #{h.heap?}"
puts h
puts
puts
puts "pop: %i" % h.pop
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.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.tree.array.inspect}"
puts "heap: #{h.heap?}"
puts h
puts
puts <<EOF
#
# display the results of binary Heap push and pop
#
EOF
h = Heap.new(child_slots: 2)
puts "push: %s" % Array.new(30) { rand(99).tap { |i| h.push i } }.join(' ')
puts "array: #{h.tree.array.inspect}"
puts "heap: #{h.heap?}"
puts h
puts
puts
puts "pop: %i" % h.pop
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.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.tree.array.inspect}"
puts "heap: #{h.heap?}"
puts h
puts