protected function nodeRank($key, ?RedBlackNode $node): 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());
    }