When studying balanced trees, I ran into a problem - I can’t write / find an algorithm that works faster than O (N) to find the i-th ascending element of the tree.
There are many explanations on the net about how to do this in the general case — take the size of the left subtree and apply a series of operations, for example, discussed here: https://stackoverflow.com/questions/2329171/find-kth-smallest-element- in-a-binary-search-tree-in-optimum-way / 2329236 # 2329236
However, the general case is considered everywhere and there is an explanation that for a balanced tree this can be done as a logarithm.
Here I have a problem - when turning subtrees, I have to recalculate the sizes. Any ideas how to do this by saving the desired asymptotics?
Here is a fragment of the balancing itself:
private void rebalancing(Node node) { while (node != null) { updateNodeHeight(node); if (checkNodeHeight(node.left) >= 2 + checkNodeHeight(node.right)) { if (checkNodeHeight(node.left.left) >= checkNodeHeight(node.left.right)) { rightRotation(node); } else { leftRotation(node.left); rightRotation(node); } } else if (checkNodeHeight(node.right) >= 2 + checkNodeHeight(node.left)) { if (checkNodeHeight(node.right.right) >= checkNodeHeight(node.right.left)) { leftRotation(node); } else { rightRotation(node.right); leftRotation(node); } } node = node.parent; } }