What is the time complexity of deleting a node in a binary tree

坚强是说给别人听的谎言 提交于 2020-08-06 14:54:06

问题


For deleting a node in the binary tree, we have to search the node. That is possible in minimum O(log N) and max O(N). Depending on the node, we have to rearrange the pointers. How do we calculate the time complexity of that.


回答1:


That depends on how you're doing the deletion. The most common way involves finding the successor of the node, then replacing the node with that successor. This can be done in O(h), where h is the height of the tree. In the worst case this is O(n), but in a balanced tree is worst-case O(lg n).




回答2:


Yes best case complexity is O(logn) (when perfectly balanced) and
worst case complexity is O(n)
1 - 2 - 3 - 4

But the main problem with BST deletion (Hibbard Deletion) is that It is not symmetric. After many insertion and deletion BST become less balance. Researchers proved that after sufficiently long number of random insert and delete height of the tree becomes sqrt(n) . so now every operation (search, insert, delete) will take sqrt(n) time which is not good compare to O(logn) .

This is very long standing(around 50 years) open problem to efficient symmetric delete for BST. for guaranteed balanced tree, we have to use RedBlack Tree etc.




回答3:


Where are you getting the "worst search time as max O(N)"? That should never happen in a BST. At worst, it should be max O(h) for search and delete, where 'h' is the height of the tree. See this helpful article.




回答4:


Most of the complexity is searching for the node. Once it has been found—as long as the parent node is retained—it is only a few more assignments to delete the node. So it is a constant order.



来源:https://stackoverflow.com/questions/4582251/what-is-the-time-complexity-of-deleting-a-node-in-a-binary-tree

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