问题
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
- to add/insert an element
- to remove an element
- 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