I have an application that requires a data structure with the following characteristics:
- in-order traversal in O(n)
- lookup in O(log n)
- insertion in O(log n) with space O(log n) or less
- efficient in-place storage (= the tree can be modified in-place in a contiguous array with no holes)
- iterative, if possible
- deletion is not required
I found complete binary search trees to be a good structure for these operations. I've implemented traversal and lookup easily (they're pretty much generic) but am having a really hard time with insertion. I can't seem to insert arbitrary elements and rebalance the tree without losing either the shape property (complete tree) or the partition property (all elements on the left of a node compare strictly less than the node).
I can't find anything else online either, the only references I find are about general binary trees (with no shape property) and are not applicable in my case. Complete trees are unpopular for some reason.
Has anyone implemented insertion for complete binary trees and could give me some pointers on how to implement it robustly and efficiently? This is not homework, I need it for a real project.