/** * Simple recursive visualisation. * The first node should be the root. * Colors are for linux console, sorry windows guys :p * * @param Node $node * @param string $indent * @return string The max indentation */ protected function infixeRender(Node $node, $indent = '') { if (!$node->isLeaf() && $node->haveChild(Node::POSITION_LEFT)) { // Show left side first $this->infixeRender($node->getChild(Node::POSITION_LEFT), $indent . ' '); } echo sprintf('%s%s#%d', $indent, $node->getPosition() == 0 ? '-' : ($node->getPosition() > 0 ? '\\' : '/'), $node->getId()); echo ' L#' . ($node->haveChild(Node::POSITION_LEFT) ? $node->getChild(Node::POSITION_LEFT)->getId() : '-'); echo ' R#' . ($node->haveChild(Node::POSITION_RIGHT) ? $node->getChild(Node::POSITION_RIGHT)->getId() : '-'); echo ' P#' . (null !== $node->getParent() ? $node->getParent()->getId() : '-'); echo ' C:' . (Node::COLOR_RED === $node->getColor() ? "[0;31mRED[0m" : "[0;32mBLACK[0m"); echo PHP_EOL; if (!$node->isLeaf() && $node->haveChild(Node::POSITION_RIGHT)) { // Show left side first $this->infixeRender($node->getChild(Node::POSITION_RIGHT), $indent . ' '); } }
/** * Recursive search of the parent node of $node in $hierarchy * First call must be with $this->root or course. * * @param Node $hierarchy * @param int $id * @return Node * @throws \Exception */ protected function searchClosestNode(Node $hierarchy, $id) { $position = $this->compare($hierarchy->getId(), $id); if ($hierarchy->isLeaf() || 0 === $position || !$hierarchy->haveChild($position)) { return $hierarchy; } return $this->searchClosestNode($hierarchy->getChild($position), $id); }