Although the time complexity of building a min heap looks like O(nlogn), it can be proved that it is O(n).
Why can't we apply the same logic and say the time complexity of a balanced binary search tree is also O(n).
Although the time complexity of building a min heap looks like O(nlogn), it can be proved that it is O(n).
Why can't we apply the same logic and say the time complexity of a balanced binary search tree is also O(n).
Beside the fact that BST provides order, while heap only ensures that element is greater than those below (for max-heap), the complexity of building the heap depends on the building strategy. This wiki image demonstrates it clearly.
If you use siftDown
(bottom-up) approach, complexity is O(n)
, while with siftUp
is O(nlogn)
, just like in BST.
Why can't we apply the same logic and say the time complexity of a balanced binary search tree is also O(n)?
Bottom-up approach for building a heap is not applicable for BST unless the input list is already sorted, but in this case you already have O(nlogn)
complexity because of sorting.