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;
    }