So, the idea is that I have an arbitrary binary tree (with no rules regarding height balancing or ordering) and I want to insert a number 'x' at an index 'i' in the inorder traversal of the binary tree (and shift all the subsequent nodes one step to the right). This could be trivially solved in O(n), but I want to find a solution in O(logn). I'm fairly confident I'll need an AVL tree to do this. It's been almost a week and I still haven't made any progress, so I'm turning to the StackOverflow community to see if they can nudge me in the right direction.
EDIT: Here's the rough O(n) implementation if it helps. It's pretty repulsive. You can get away with just maintaining the inorder traversal if you want to do it in O(n). Here num is the number we want to insert into the binary tree, index is the index we want to insert it at, and inorder is the array that maintains the inorder traversal of the binary tree.
insert(num, index, inorder[])
if (index >= inorder.size())
1. Keep adding '-1' to the end of the inorder array until its size is 'index'
2. Add 'num' to the end of the array
else if (inorder[index] == -1)
inorder[index] = num
else
1. Right shift the inorder array from inorder[index] onwards
2. inorder[index] = num
EDIT: Also, this is actually part of a competitive programming question that someone sent me. The end goal is to be able to return any element given its index in the inorder traversal of the tree. I feel like the solution is something out-of-the-box and maybe doesn't even require building a tree at all.