Data Structures Tutorial

Data Structures Tutorial Asymptotic Notation Structure and Union Array Data Structure Linked list Data Structure Type of Linked list Advantages and Disadvantages of linked list Queue Data Structure Implementation of Queue Stack Data Structure Implementation of Stack Sorting Insertion sort Quick sort Selection sort Heap sort Merge sort Bucket sort Count sort Radix sort Shell sort Tree Traversal of the binary tree Binary search tree Graph Spanning tree Linear Search Binary Search Hashing Collision Resolution Techniques

Misc Topic:

Priority Queue in Data Structure Deque in Data Structure Difference Between Linear And Non Linear Data Structures Queue Operations In Data Structure About Data Structures Data Structures Algorithms Types of Data Structures Big O Notations Introduction to Arrays Introduction to 1D-Arrays Operations on 1D-Arrays Introduction to 2D-Arrays Operations on 2D-Arrays Strings in Data Structures String Operations Application of 2D array Bubble Sort Insertion Sort Sorting Algorithms What is DFS Algorithm What Is Graph Data Structure What is the difference between Tree and Graph What is the difference between DFS and BFS Bucket Sort Dijkstra’s vs Bellman-Ford Algorithm Linear Queue Data Structure in C Stack Using Array Stack Using Linked List Recursion in Fibonacci Stack vs Array What is Skewed Binary Tree Primitive Data Structure in C Dynamic memory allocation of structure in C Application of Stack in Data Structures Binary Tree in Data Structures Heap Data Structure Recursion - Factorial and Fibonacci What is B tree what is B+ tree Huffman tree in Data Structures Insertion Sort vs Bubble Sort Adding one to the number represented an array of digits Bitwise Operators and their Important Tricks Blowfish algorithm Bubble Sort vs Selection Sort Hashing and its Applications Heap Sort vs Merge Sort Insertion Sort vs Selection Sort Merge Conflicts and ways to handle them Difference between Stack and Queue AVL tree in data structure c++ Bubble sort algorithm using Javascript Buffer overflow attack with examples Find out the area between two concentric circles Lowest common ancestor in a binary search tree Number of visible boxes putting one inside another Program to calculate the area of the circumcircle of an equilateral triangle Red-black Tree in Data Structures Strictly binary tree in Data Structures 2-3 Trees and Basic Operations on them Asynchronous advantage actor-critic (A3C) Algorithm Bubble Sort vs Heap Sort Digital Search Tree in Data Structures Minimum Spanning Tree Permutation Sort or Bogo Sort Quick Sort vs Merge Sort

Priority Queue in Data Structure

Priority Queue

A priority queue is a special kind of queue, in priority queue we give some priority to an element and according to this priority an element can be served in the queue.

Generally, the value of element is treated as the priority of an element. Like, the element has highest value treated as the highest priority element as well as we assume that if element has lowest value treated as the highest priority element in the other case, we can give the priorities to elements according to our requirements.

Difference between in normal queue and a Priority Queue

If we talk about the normal queue works on first in first out rule whereas, in a priority queue element is removed according to the priority, an element with the highest priority is removed first.

Types of Priority Queue

There are two types of priority queue :

a Max-Priority Queue and a Min- Priority Queue. In these types, the priority queue stores the collections of elements and it is only able to provide the most “Extreme” element.

Implementation of Priority Queue

There are many ways to implement priority queue like, array, linked list, binary heap etc. If we talk about heap data structure then it provides an efficient way of implementing priority queues.

Complexity of Priority Queue : -

OperationsInsertPeekDelete
Linked ListO(1)O(n)O(1)
Binary HeapO(1)O(log n)O(log n)

Operations on Priority Queue

The operations of priority queues are inserting element, removing element, getting element. We are using Binary heap data structure for implementing of priority queue.

1. Add an new element into Priority Queue :

Here we are using max heap data structure for priority queue

  • Add new element at the end of tree.
Priority Queue in Data Structure
  • Heapify the tree
Priority Queue in Data Structure

2. Removing an element from the Priority Queue : -

  • Select the element which you want to delete
Priority Queue in Data Structure
  • Swap that element with last element
Priority Queue in Data Structure
  • Remove the last element and heapify the tree
Priority Queue in Data Structure

3. Getting an element from the Priority Queue :

Return the root node without deleting the element of tree

Priority Queue Implementation in C language

#include <stdio.h>
 int len = 0;
 void swap(int *a, int *b)
 {
                 int temp = *b;
                 *b = *a;
                 *a = temp;
 }
 void heapify_Tree(int queue[] , int len , int i) // For Re-Balance the tree
 {
 if (len == 1)
 {
 printf("the heap has only one element");
                                                             }
 else
 {
        int largest_ele= i;
        int l = 2 * i + 1;
        int r = 2 * i + 2;
          if (l < len && queue[l] > queue[largest_ele])
         largest_ele = l;
                       if (r < len && queue[r] > queue[largest_ele])
                        largest_ele = r;
         if (largest_ele != i)
 {
                  swap(&queue[i] , &queue[largest_ele]);
                           heapify_Tree(queue , len , largest_ele);
                                                                          }
                                                                 }
 }
 void insert(int queue[] , int newNum) // For adding new element in to tree
 {
           if (len == 0)
  {
               queue[0] = newNum;
              len += 1;
              }
 else
  {
            queue[len] = newNum;
               len += 1;
 for (int i = len / 2-1 ; i >= 0 ; i--)
 {
       heapify_Tree(queue , len , i);
  }
      }
 }
 void deleteElement(int queue[] , int num) // For removing desired element from the tree
 {
   int i;
   for (i = 0 ; i < len ; i++)
 {
    if (num == queue[i])
     break ;
    }
 swap(&queue[i] , &queue[len - 1]);
      len -= 1;
   for (int i = len / 2-1 ; i >= 0 ; i--)
 {
     heapify_Tree(queue , len , i);
                                                             }
 }
 void delete(int queue[]) // For extracting max element (root node) from the tree
 {
             swap(&queue[0],&queue[len-1]);
             printf("Removed highest priority element : %d\n" , queue[len-1]);
             len -= 1;
     for (int i = len-1  ; i >= 0 ; i--)
 {
             heapify_Tree(queue , len , i);
                                                             }                      
 }
 void printQueue(int queue[] , int len) // For printing the elements
 {
 for (int i = 0 ; i < len ; ++i)
   printf("%d " , queue[i]);
   printf("\n");
 }
 int main() // Driver code
 {
   int queue[10];
   insert(queue , 3);
   insert(queue , 4);
 insert(queue , 9);
 insert(queue , 5);
 insert(queue , 2);
 insert(queue , 1);
 insert(queue , 0);
  printf("Max-Heap queue : ");
  printQueue(queue , len);
  deleteElement(queue , 4);
  printf("After deleting an element : ");
  printQueue(queue , len);
  delete(queue);
  printf("After deleting the highest priority element : ");
  printQueue(queue , len);
 } 

Output :

Priority Queue in Data Structure

Applications of Priority Queue

  • In Hauffman Coding
  • For Implementing Stack
  • In Dijkstra’s Algorithm
  • In Prim’s Minimum Spanning Tree
  • Process Scheduling in OS
  • All Queue Applications where Priority Involved



ADVERTISEMENT
ADVERTISEMENT