-
Notifications
You must be signed in to change notification settings - Fork 0
/
binary_trees.php
82 lines (69 loc) · 1.69 KB
/
binary_trees.php
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
<?php
/**
* Binary trees benchmark for subsetphp
*
* @author Olle Härstedt
* @since 2015-08-15
*/
class TreeNode {
public $item;
public $left, $right;
/**
* @param int $item
*/
public function __construct($item) {
$this->item = $item;
}
/**
* @return int
*/
public function check() {
return $this->left === null ? $this->item : $this->left->check() - $this->right->check() + $this->item;
}
}
/**
* Recursive function creating tree nodes
*
* @param int $item
* @param int $depth
* @return TreeNode
*/
function ChildTreeNodes($item, $depth)
{
$node = new TreeNode($item);
if ($depth > 0)
{
$node->left = ChildTreeNodes(2 * $item - 1, $depth - 1);
$node->right = ChildTreeNodes(2 * $item, $depth - 1);
}
return $node;
}
/**
* @param int $item
* @param int $depth
* @return TreeNode
*/
function create($item, $depth)
{
return ChildTreeNodes($item, $depth - 1);
}
$n = $argc === 2 ? $argv[1] : 1;
$minDepth = 4;
$maxDepth = $minDepth + 2 > $n ? $minDepth + 2 : $n;
$stretchDepth = $maxDepth + 1;
$stretchTree = create(0, $stretchDepth);
$check = $stretchTree->check();
printf("stretch tree of depth " . ($maxDepth + 1) . "\t check: " . $check . "\n");
$longLivedTree = create(0, $maxDepth);
for ($depth = $minDepth; $depth <= $maxDepth; $depth += 2)
{
$iterations = 1 << ($maxDepth - $depth + $minDepth);
$check = 0;
for ($i = 1; $i <= $iterations; $i++)
{
$check += (create($i, $depth))->check();
$check += (create(-$i, $depth))->check();
}
printf("%d\t trees of depth %d\t check: %d\n", $iterations << 1, $minDepth, $check);
}
printf("long lived tree of depth " . $maxDepth . "\t check: " . $longLivedTree->check() . "\n");