protected function nodeRemove($key, RedBlackNode $node): ?RedBlackNode
    {
        $comp = $this->comparator->compare($key, $node->key());
        if ($comp < 0) {
            $node = $this->nodeRemoveLeft($key, $node);
        } else {
            if ($this->isRed($node->left())) {
                $node = $this->rotateRight($node);
            }
            if ($comp === 0 && $node->right() === null) {
                return null;
            }
            $node = $this->nodeRemoveRight($key, $node);
        }

        return $this->balanceOnRemove($node);
    }