function bottomUpTree($item, $depth) { if (!$depth) { return array(null, null, $item); } $item2 = $item + $item; $depth--; return array(bottomUpTree($item2 - 1, $depth), bottomUpTree($item2, $depth), $item); }
function bottomUpTree($item, $depth) { if ($depth) { --$depth; $newItem = $item << 1; return array(bottomUpTree($newItem - 1, $depth), bottomUpTree($newItem, $depth), $item); } return array(NULL, NULL, $item); }
function bottomUpTree($item, $depth) { if ($depth > 0) { $left = bottomUpTree(2 * $item - 1, $depth - 1); $right = bottomUpTree(2 * $item, $depth - 1); return newTreeNode($left, $right, $item); } else { $treeNode = NULL; return newTreeNode($treeNode, $treeNode, $item); } }
function bottomUpTree($item, $depth) { $node = new Node(); $node->item = $item; if (!$depth) { return $node; } $item2 = $item + $item; $depth--; $node->l = bottomUpTree($item2 - 1, $depth); $node->r = bottomUpTree($item2, $depth); return $node; }
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 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)); }