We will implement a binary tree using a PHP array with a simple recursive function-based traversal. We are just rewriting the functionality using one function instead of a class. Here is the code to do this:
function treeTraverse(array &$tree, int $index = 0,
int $level = 0, &$outputStr = "") : ?bool {
if(isset($tree[$index])) {
$outputStr .= str_repeat("-", $level);
$outputStr .= $tree[$index] . "\n";
treeTraverse($tree, 2 * $index + 1, $level+1,$outputStr);
treeTraverse($tree, 2 * ($index + 1), $level+1,$outputStr);
} else {
return false;
}
return null;
}
$nodes = [];
$nodes[] = "Final";
$nodes[] = "Semi Final 1";
$nodes[] = "Semi Final 2";
$nodes[] = "Quarter Final 1";
$nodes[...