Binary tree complexities

谁说我不能喝 提交于 2021-02-07 19:27:39

问题


I would like to know some complexities of binary search tree.

I can't find complete information. I want to know complexities for the following operations on a binary search tree

  1. to add/insert an element
  2. to remove an element
  3. to find an element (as I know this one is O(log(n)) )

回答1:


Insertion, deletion and searching in a binary search tree are:

  • O(N) in the worst case;
  • O(log(N)) in the average case.



回答2:


If you have balanced binary tree, all three complexities will be of O(log(N)). If you are not balancing the tree, it could be O(N).




回答3:


Search is effective. But unbalanced structure (which is often the case) can lead to O(N) for search/insert/remove operations. That is why binary heap or other kind of balanced trees are preferred with O(log n). .



来源:https://stackoverflow.com/questions/15586820/binary-tree-complexities

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