Using heaps as a priority queue


One of the main ways to use the heap data structure is to create a priority queue. As we have seen in Chapter 4, Constructing Stacks and Queues, priority queues are special queues where the FIFO behavior depends on the priority of the element rather than the way items are added to the queue. We have already seen the implementation using Linked list and SPL. Now we are going to explore the priority queue implementation using heap and especially max-heap.


Now we are going to implement the priority queue using MaxHeap. Here, the maximum priority item is removed from the queue first. Our implementation will be similar to our last implementation of MinHeap with a little difference. Instead of starting the root ...

Get PHP 7 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.