Example #1
0
 public function test_that_left_holds_reference_to_left_node()
 {
     $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);
     $this->assertSame($left, $node->left());
 }
Example #2
0
 /**
  * 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;
 }