Checking if the heap is empty

The heapSize variable can be used to indicate whether or not the heap is empty. We just need to check if heapSize = 0 to determine that the heap is empty. The following is an implementation of the IsEmpty() operation:

bool BinaryHeap::IsEmpty(){    // Heap is empty if only    // heapSize = 0    return heapSize == 0;}

The preceding sample code will run on O(1) since it's just simply getting the heapSize value.

Get C++ Data Structures and Algorithms now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.