Search an element in a heap

You need to search through every element in the heap in order to determine if an element is inside.

One optimization is possible, though (we assume a max heap here). If you have reached a node with a lower value than the element you are searching for, you don’t need to search further from that node. However, even with this optimization, search is still O(N) (need to check N/2 nodes in average).

Leave a Comment