protected function nodeSelect(int $rank, RedBlackNode $node): RedBlackNode
    {
        $size = $this->nodeSize($node->left());

        if ($size > $rank) {
            return $this->nodeSelect($rank, $node->left());
        }
        if ($size < $rank) {
            return $this->nodeSelect($rank - $size - 1, $node->right());
        }

        return $node;
    }