コード例 #1
0
    }
    // TODO: pretty print of tree
    public function traceAsTree()
    {
        return $this->traceSubTreeFrom($this->head);
    }
    private function traceSubTreeFrom($node)
    {
        if (is_null($node)) {
            return "";
        }
        $out = "[";
        if ($node->hasPrevious()) {
            $out .= $this->traceSubTreeFrom($node->previous()) . '/';
        }
        $out .= $node->value();
        if ($node->hasNext()) {
            $out .= '\\' . $this->traceSubTreeFrom($node->next());
        }
        $out .= ']';
        return $out;
    }
}
$list = new Lista();
for ($value = 1; $value <= 13; $value++) {
    $list->add(new Node($value));
}
echo PHP_EOL . $list->traceAsList() . PHP_EOL;
$list->toBST();
echo PHP_EOL . "ROOT IS " . $list->valueOfHead() . PHP_EOL;
echo PHP_EOL . $list->traceAsTree() . PHP_EOL;