Building a binary heap ADT
A binary heap is a completely binary tree that is usually used to implement a priority queue. Please look at the following binary tree which is representing the priority queue:
As we can see, each node has its own key and there's also a number below each node to indicate the priority of the element (in this example, the maximum element has higher priority). The priority queue is usually represented in an array, so we can have the following array as a representation of the preceding priority queue tree:
To create a binary heap in C++, we will have the heapSize
variable, which will be increased when an element is inserted and will be decreased when an element is removed. There are four basic operations in a priority queue, and they are as follows:
IsEmpty()
is used to check whether the queue is emptyInsert()
, similar to theÂEnqueue()
operation in a Queue data structure, is used to insert a new element into the queueGetMax()
, similar to theÂPeek()
operation in a Queue...