-
Notifications
You must be signed in to change notification settings - Fork 143
/
Copy path1.rb
85 lines (71 loc) · 1.85 KB
/
1.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
79
80
81
82
83
84
85
class Node
hash = nil
value = nil
left = nil
right = nil
def initialize(value, left, right)
@value = value
@left = left
@right = right
end
def check
if @hash == nil
return false
end
if @value != nil
return true
end
if @left != nil && @right != nil
return @left.check && @right.check
end
return false
end
def cal_hash
if @hash == nil
if @value != nil
@hash = @value
else
if @left != nil && @right != nil
@left.cal_hash
@right.cal_hash
@hash = @left.get_hash + @right.get_hash
end
end
end
end
def get_hash
if @hash == nil
return -1
else
return @hash
end
end
end
def createNode(n)
if n == 0
return Node.new(1, nil, nil)
end
d = n - 1
return Node.new(nil, createNode(d), createNode(d))
end
max_depth = ARGV.size > 0 ? ARGV[0].to_i : 10
min_depth = 4
max_depth = min_depth + 2 if min_depth + 2 > max_depth
stretch_depth = max_depth + 1
stretch_tree = createNode(stretch_depth)
stretch_tree.cal_hash
puts "stretch tree of depth #{stretch_depth}\t root hash: #{stretch_tree.get_hash} check: #{stretch_tree.check}"
stretch_tree = nil
long_lived_tree = createNode(max_depth)
min_depth.step(to: max_depth, by: 2) do |depth|
iterations = 2**(max_depth - depth + min_depth)
sum = 0
(1..iterations).each do |i|
temp_tree = createNode(depth)
temp_tree.cal_hash
sum += temp_tree.get_hash
end
puts "#{iterations}\t trees of depth #{depth}\t root hash sum: #{sum}"
end
long_lived_tree.cal_hash
puts "long lived tree of depth #{max_depth}\t root hash: #{long_lived_tree.get_hash} check: #{long_lived_tree.check}"