-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtree.py
More file actions
41 lines (28 loc) · 868 Bytes
/
tree.py
File metadata and controls
41 lines (28 loc) · 868 Bytes
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
# Creates a binary tree of depth 'd'
def make_tree(d):
if d > 0:
d -= 1
return (make_tree(d), make_tree(d))
return (None, None)
def check_tree(node):
(l, r) = node
if l is None:
return 1
else:
return 1 + check_tree(l) + check_tree(r)
def tree(n, min_depth=4):
if (n < 0):
return 0
max_depth = max(min_depth + 2, n)
stretch_depth = max_depth + 1
t = make_tree(stretch_depth)
check_tree(t)
long_lived_tree = make_tree(max_depth)
mmd = max_depth + min_depth
for d in range(min_depth, stretch_depth, 2):
iterations = 2 ** (mmd - d)
cs = 0
for i in range(iterations):
cs = cs + check_tree(make_tree(d))
check_tree(long_lived_tree)
return 0