Section 8.8 Priority Queues with Binary Heaps Example
As mentioned before we can use binary heaps in order to design a priority queue, according to our preference, using a min or max heap would allow us to construct a tree in such a way that the children are always larger or smaller than the parent, respectively. In the example in this section we will be using min heap structure. That way the smallest element will always be at the root, that is the goal of using a heap for a priority queue, since this will allow for swift removal of the element with utmost priority. When inserting new elements into the heap it is important to maintain the priority structure, therefore every time we insert a new element, not only do we insert it in such a way to fill out the tree from left to right, but we also make sure that every parent node is smaller then the child node that we inserted. This way if the inserted node is the smallest node in the tree it will bubble up to the root and push everything else lower into the heap. Deletion of the items is also important, since we know that the element with utmost priority is always at the root we can simply remove the root and replace it with the righmost leaf. However now our priority structure is broken, we can fix this by swaping it with the lesser of its children, then the structure will be correct agian. This structure allows for effective maintenance of the priority of the items and greatly improves the efficiency of our priority queue.
Before we begin with the implementation of Binary Heaps, let us review a real life example of one of the things: Priority Queues. Let us consider a priority queue of homework. If we were trying to decide which homework project to complete, it would make sense to organize the order of doing any specific project based on the deadline of that homework. Let us say you were assigned homeworks in the following order and you are told the deadlines for those homeworks: 3, 10, 5, 8, 7, 2 days respectively. You can observe the insertion of these homeworks into our priority queue in Figureย 8.8.1.

A sequence of diagrams depicting the insertion process into a priority queue represented by a binary tree. Starting with a single node labeled โ3โ, the first diagram shows โInsert 10โ, leading to a new node โ10โ added as a child. The next step โInsert 5โ adds a new node โ5โ, followed by โInsert 8โ which adds node โ8โ. Another step shows โInsert 7โ with a node โ7โ being added, and the sequence progresses with each insertion maintaining the binary tree structure. The diagrams show the dynamic reordering of nodes to maintain the priority queue property, with the final structure at the bottom left displaying a balanced binary tree with node โ2โ at the top, leading to nodes โ3โ and โ7โ, which branch out to further nodes โ10โ, โ8โ, โ5โ, and โ2โ. Arrows indicate the insertion order and the adjustments within the tree.
Now that we have the priority queue of which homeworks we need to do in the order. Let us start by doing the homework with 2 day deadline. We would remove it from the root and put the left most leaf in its place and than rearange the heap back to the proper state, as shown in Figureย 8.8.2.

Two diagrams showing the process of removing the root from a binary heap that represents a priority queue. The first diagram on the left shows a binary tree with the root node โ3โ, which has two children: โ7โ on the left with its own children โ10โ and โ8โ, and โ5โ on the right. An arrow points from this tree to the second diagram on the right, indicating the removal of the root node. The second diagram shows the tree after the root has been removed and the heap has been restructured: the node โ5โ has been moved to the root position, with โ7โ as its left child and โ3โ as its right child, maintaining the heap property. The nodes โ10โ and โ8โ remain as children of โ7โ. The tree restructuring is shown with curved arrows indicating the new parent-child relationships.
As we continue to complete the homework with the closest deadline we will continue to remove the homeworks in the demostrated fashion until we are done.
You have attempted of activities on this page.
