问题
I know the concept of Binary Search Tree and Complete Binary tree. Is there a way we can write insert algorithm for Complete binary search tree or I am thinking of wrong data structure?
My objective is every time we insert a node, Tree should remain complete binary search tree.
回答1:
Of course you can. But it will be O(N) algorithm, just rebuild tree after every insertion or deletion.
You can't do this faster than O(N) time. Because:
1) Exists only 1 complete tree with given keys.
2) You can remove or insert the minimum and you will have to change the whole tree (costs you O(N) operations).
Instead of complete search binary trees, balanced binary trees are used (like RB, AVL, Cartesian, Splay and etc.).
来源:https://stackoverflow.com/questions/34835192/inserting-node-dynamically-in-complete-binary-search-tree