forked from whymirror/potion
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbinarytrees.pn
More file actions
41 lines (35 loc) · 1017 Bytes
/
binarytrees.pn
File metadata and controls
41 lines (35 loc) · 1017 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
n = 20
mindepth = 4
maxdepth = mindepth + 2
if (maxdepth < n): maxdepth = n.
new_tree = class (item, depth):
/item = item
if (depth > 0):
i = item + item, depth--
/left = new_tree (i - 1, depth)
/right = new_tree (i, depth).
.
item_check = (tree):
if (tree /left):
tree /item + item_check (tree /left) - item_check (tree /right).
else:
tree /item.
.
stretch_depth = maxdepth + 1
check = item_check(new_tree(0, stretch_depth))
("stretch tree of depth ", stretch_depth,
"\t check: ", check, "\n") join print
longlivedtree = new_tree(0, maxdepth)
depth = mindepth, while (depth <= maxdepth):
iter = 1 << (maxdepth - depth + mindepth)
check = 0
i = 1
while (i <= iter):
check = check + item_check(new_tree(1, depth)) +
item_check(new_tree(-1, depth))
i++.
(iter * 2, "\t trees of depth ", depth,
"\t check: ", check, "\n") join print
depth = depth + 2.
("long lived tree of depth ", maxdepth, "\t check: ",
item_check(longlivedtree), "\n") join print