Exemple #1
0
 public function test_that_it_allows_key_replacement()
 {
     $monday = WeekDay::MONDAY();
     $node = new RedBlackNode($monday, 'Monday', 1, RedBlackNode::RED);
     $sunday = WeekDay::SUNDAY();
     $node->setKey($sunday);
     $this->assertSame($sunday, $node->key());
 }
Exemple #2
0
 /**
  * Retrieves the rank for a key in a subtree
  *
  * @param mixed             $key  The key
  * @param RedBlackNode|null $node The subtree root
  *
  * @return int
  */
 protected function nodeRank($key, RedBlackNode $node = null) : int
 {
     if ($node === null) {
         return 0;
     }
     $comp = $this->comparator->compare($key, $node->key());
     if ($comp < 0) {
         return $this->nodeRank($key, $node->left());
     }
     if ($comp > 0) {
         return 1 + $this->nodeSize($node->left()) + $this->nodeRank($key, $node->right());
     }
     return $this->nodeSize($node->left());
 }