private function removeIn(RedBlackTree $t, $key) { if ($this->compare($key, $t->key()) < 0) { if (!$this->isRed($t->left()) && !$this->isRed($t->left()->left())) { $t = $this->moveRedLeft($t); } $t = new self($t->key(), $t->value(), $t->left()->remove($key), $t->right(), $t->color(), $t->isRoot()); } else { if ($this->isRed($t->left())) { $t = $this->rotateRight($t); } if ($this->compare($key, $t->key()) === 0 && $t->right()->isEmpty()) { return self::createEmpty(); } if (!$this->isRed($t->right()) && !$this->isRed($t->right()->left())) { $t = $this->moveRedRight($t); } if ($this->compare($key, $t->key()) === 0) { $min = $this->minIn($t->right()); $t = new self($min->key(), $min->value(), $t->left(), $this->removeMinIn($t->right()), $t->color(), $t->isRoot()); } else { $t = new self($t->key(), $t->value(), $t->left(), $t->right()->remove($key), $t->color(), $t->isRoot()); } } return $this->balance($t); }