コード例 #1
0
ファイル: RedBlackSearchTree.php プロジェクト: novuso/system
 /**
  * Restores red-black tree invariant on insert
  *
  * @codeCoverageIgnore
  *
  * @param RedBlackNode $node The subtree root
  *
  * @return RedBlackNode
  */
 protected function balanceOnInsert(RedBlackNode $node) : RedBlackNode
 {
     if ($this->isRed($node->right()) && !$this->isRed($node->left())) {
         $node = $this->rotateLeft($node);
     }
     if ($this->isRed($node->left()) && $this->isRed($node->left()->left())) {
         $node = $this->rotateRight($node);
     }
     if ($this->isRed($node->left()) && $this->isRed($node->right())) {
         $this->flipColors($node);
     }
     $node->setSize(1 + $this->nodeSize($node->left()) + $this->nodeSize($node->right()));
     return $node;
 }
コード例 #2
0
ファイル: RedBlackNodeTest.php プロジェクト: novuso/system
 public function test_that_clone_includes_linked_nodes()
 {
     $monday = WeekDay::MONDAY();
     $node = new RedBlackNode($monday, 'Monday', 1, RedBlackNode::RED);
     $sunday = WeekDay::SUNDAY();
     $left = new RedBlackNode($sunday, 'Sunday', 1, RedBlackNode::RED);
     $node->setLeft($left);
     $tuesday = WeekDay::TUESDAY();
     $right = new RedBlackNode($tuesday, 'Tuesday', 1, RedBlackNode::RED);
     $node->setRight($right);
     $copy = clone $node;
     $node->setLeft(null);
     $node->setRight(null);
     $this->assertTrue($copy->left()->key()->equals($sunday));
 }