function itemCheck(&$treeNode) { if (!isset($treeNode[0])) { return $treeNode[2]; } else { return $treeNode[2] + itemCheck($treeNode[0]) - itemCheck($treeNode[1]); } }
function itemCheck($treeNode) { $check = 0; do { $check += $treeNode[2]; if (NULL == $treeNode[0]) { return $check; } $check -= itemCheck($treeNode[1]); $treeNode = $treeNode[0]; } while (TRUE); }
function binary_trees($n) { $minDepth = 4; $maxDepth = max($minDepth + 2, $n); $stretchDepth = $maxDepth + 1; $stretchTree = bottomUpTree(0, $stretchDepth); unset($stretchTree); $longLivedTree = bottomUpTree(0, $maxDepth); $iterations = 1 << $maxDepth; do { $check = 0; for ($i = 1; $i <= $iterations; ++$i) { $t = bottomUpTree($i, $minDepth); $check += itemCheck($t); unset($t); $t = bottomUpTree(-$i, $minDepth); $check += itemCheck($t); unset($t); } $minDepth += 2; $iterations >>= 2; } while ($minDepth <= $maxDepth); }
function itemCheck($treeNode) { return $treeNode[2] + ($treeNode[0][0] === null ? itemCheck($treeNode[0]) : $treeNode[0][2]) - ($treeNode[1][0] === null ? itemCheck($treeNode[1]) : $treeNode[1][2]); }
function binary_trees($n = 12) { $minDepth = 4; $maxDepth = max($minDepth + 2, $n); $stretchDepth = $maxDepth + 1; $stretchTree = bottomUpTree(0, $stretchDepth); printf("stretch tree of depth %d\t check: %d\n", $stretchDepth, itemCheck($stretchTree)); unset($stretchTree); $longLivedTree = bottomUpTree(0, $maxDepth); $iterations = 1 << $maxDepth; do { $check = 0; for ($i = 1; $i <= $iterations; ++$i) { $t = bottomUpTree($i, $minDepth); $check += itemCheck($t); unset($t); $t = bottomUpTree(-$i, $minDepth); $check += itemCheck($t); unset($t); } printf("%d\t trees of depth %d\t check: %d\n", $iterations << 1, $minDepth, $check); $minDepth += 2; $iterations >>= 2; } while ($minDepth <= $maxDepth); printf("long lived tree of depth %d\t check: %d\n", $maxDepth, itemCheck($longLivedTree)); }
function itemCheck($treeNode) { return $treeNode->item + ($treeNode->l->l === null ? itemCheck($treeNode->l) : $treeNode->l->item) - ($treeNode->r->l === null ? itemCheck($treeNode->r) : $treeNode->r->item); }