예제 #1
0
 /**
  * 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() ? "RED" : "BLACK");
     echo PHP_EOL;
     if (!$node->isLeaf() && $node->haveChild(Node::POSITION_RIGHT)) {
         // Show left side first
         $this->infixeRender($node->getChild(Node::POSITION_RIGHT), $indent . '  ');
     }
 }
예제 #2
0
 /**
  * Do a rotation with node's parent
  *
  * @param Node $node
  * @param int $toPosition
  * @return $this
  */
 protected function rotate(Node $node, $toPosition)
 {
     // The new child of node in $toPosition
     $tmp = $node->getChild(-$toPosition);
     // Set node's child the grand son of son
     $node->setChild(-$toPosition, $tmp->getChild($toPosition));
     if ($tmp->haveChild($toPosition)) {
         $tmp->getChild($toPosition)->setParent($node);
     }
     $tmp->setParent($node->getParent());
     // If it's not the root, set parent's child
     if (null !== $node->getParent()) {
         $node->getParent()->setChild(($toPosition === $node->getPosition() ? 1 : -1) * $toPosition, $tmp);
     }
     $tmp->setChild($toPosition, $node);
     $node->setParent($tmp);
     // Rotation done, it's possible the root have changed
     if (null === $tmp->getParent()) {
         $this->setRoot($tmp);
     }
     return $this;
 }