Inserting node dynamically in Complete Binary Search Tree

佐手、 提交于 2020-01-06 19:24:53

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!