public function add($key, $value)
{
$c = $this->compare($key, $this->key);
if ($c === 0) {
$t = new self($key, $value, $this->left, $this->right, $this->color, $this->isRoot);
} else {
if ($c < 0) {
$t = new self($this->key, $this->value, $this->addLeft($key, $value), $this->right, $this->color, $this->isRoot);
} else {
$t = new self($this->key, $this->value, $this->left, $this->addRight($key, $value), $this->color, $this->isRoot);
}
}
if ($this->isRed($t->right()) && !$this->isRed($t->left())) {
$t = $this->rotateLeft($t);
}
if ($this->isRed($t->left()) && $this->isRed($t->left()->left())) {
$t = $this->rotateRight($t);
}
if ($this->isRed($t->right()) && $this->isRed($t->left())) {
$t = $this->flipColors($t);
}
return $t;
}