Post

Created by @adamvaughn
 at November 6th 2023, 1:53:54 am.

Post 3: Heaps and Priority Queues

Welcome to the third post in our series on advanced data structures! In this post, we will explore the concept of heaps and their applications in priority queues. Heaps are binary trees that follow a specific ordering property, making them efficient for certain operations like inserting and retrieving the maximum or minimum element. Priority queues, on the other hand, are abstract data types that allow elements to be inserted with a priority value, where the highest priority element can be accessed first. Let's dive in!

Heaps

A heap is a complete binary tree that satisfies the heap property. The heap property can be defined in two ways:

  1. Max Heap Property: For every node i, the value of i is greater than or equal to the values of its children.
  2. Min Heap Property: For every node i, the value of i is less than or equal to the values of its children.

This property allows us to efficiently retrieve the maximum (in a max heap) or minimum (in a min heap) element in constant time.

Structure of a Heap

Heaps are typically implemented using arrays. The array representation of a heap has the following properties:

  • The root element is stored at index 1.
  • For a node at index i, its left child is at index 2i and its right child is at index 2i + 1.

Operations on Heaps

  1. Insertion: When inserting an element into a heap, we first place it at the bottom-rightmost position to maintain the complete binary tree property. Then, we compare the inserted element with its parent and swap if necessary to satisfy the heap property. This process is known as up-heapify or bubble-up.

  2. Deletion: To delete the maximum (in a max heap) or minimum (in a min heap) element, we swap it with the last element in the heap. We then remove the swapped element from the heap and down-heapify or bubble-down the new root to maintain the heap property. The down-heapify operation compares the new root with its children and swaps with the larger (in a max heap) or smaller (in a min heap) child if necessary.

Example: Max Heap

Let's consider the following max heap as an example:

       15
     /    \
    10     8
   /  \   /
  7    4  6

To insert the element 12 into the heap, we would place it in the bottom-leftmost position:

       15
     /    \
    10     8
   /  \   / \
  7    4  6  12

Now, we compare 12 with its parent, 8, and swap them as 12 is greater:

       15
     /    \
    10     12
   /  \   / \
  7    4  6   8

The heap is now properly maintained.

Priority Queues

Priority queues allow elements to be inserted with a priority value and accessed in a certain order. A common implementation of a priority queue is using a heap data structure.

Example: Max Priority Queue

Let's say we have a max priority queue with the following elements and priorities:

Element Priority
A 10
B 5
C 15
D 20

The priority queue would maintain the elements in the following order based on priorities:

20 (D) -> 15 (C) -> 10 (A) -> 5 (B)

Here, D has the highest priority, followed by C, A, and B.

Operations on Priority Queues

  1. Insertion: When inserting an element into a priority queue, we first assign it a priority value. Then, we insert the element into the heap according to the required heap property (either max heap or min heap).

  2. Deletion: To delete the element with the highest priority, we remove it from the heap. This will be the root element in a max heap or min heap, depending on the implementation.

That's all for this post on heaps and priority queues! In the next post, we will delve into the world of graphs and graph algorithms. Stay tuned!