Given a sequence of numbers, I want to insert numbers into a balanced binary tree, so when I traverse the tree, it returns me the sequence.
How can I build an insertion method that meets this requirement?
Remember that the tree must be balanced, so there is no absolutely trivial solution. I tried to do this with a modified version of the AVL tree, but I'm not sure if this might work.
I also want to be able to implement the delete operation. Delete should delete the item at the i-th position in the list.
Edit: Explanation:
I want to have: Insert (i, e), which inserts one element e directly in front of the i-th element in the sequence. Delete (i), which deletes the ith element of the sequence.
If I insert (0, 5), insert (0, 4), insert (0, 7), then my saved sequence is now 7, 4, 5, and traversing in the binary tree should give me 7, 4, 5.
If I delete (1), then traversing the binary tree in order should give me 7, 5. As you can see, the sequence order is maintained after removing the ith element of the sequence with i = 1 in this case (as I indexed my sequence from 0).