From fc49ec5ece1aa361fd119f00a00e9e2f2b7b7199 Mon Sep 17 00:00:00 2001 From: sarthaxtic <128150513+sarthaxtic@users.noreply.github.com> Date: Sat, 2 Nov 2024 18:49:20 +0530 Subject: [PATCH] Delete Priority Queue directory --- Priority Queue/PriorityQueue.c | 96 ---------------------------------- Priority Queue/Readme.md | 26 --------- 2 files changed, 122 deletions(-) delete mode 100644 Priority Queue/PriorityQueue.c delete mode 100644 Priority Queue/Readme.md diff --git a/Priority Queue/PriorityQueue.c b/Priority Queue/PriorityQueue.c deleted file mode 100644 index b8d4e19c..00000000 --- a/Priority Queue/PriorityQueue.c +++ /dev/null @@ -1,96 +0,0 @@ -#include -#include - -#define MAX 100 // Maximum number of elements in the priority queue - -typedef struct { - int data[MAX]; - int size; -} PriorityQueue; - -// Function to swap elements in the heap -void swap(int *a, int *b) { - int temp = *a; - *a = *b; - *b = temp; -} - -// Heapify function for maintaining the min-heap property -void minHeapify(PriorityQueue *pq, int index) { - int smallest = index; - int left = 2 * index + 1; - int right = 2 * index + 2; - - if (left < pq->size && pq->data[left] < pq->data[smallest]) - smallest = left; - - if (right < pq->size && pq->data[right] < pq->data[smallest]) - smallest = right; - - if (smallest != index) { - swap(&pq->data[index], &pq->data[smallest]); - minHeapify(pq, smallest); - } -} - -// Insert element into the priority queue -void enqueue(PriorityQueue *pq, int value) { - if (pq->size >= MAX) { - printf("Priority queue overflow\n"); - return; - } - - // Insert the new element at the end - int i = pq->size++; - pq->data[i] = value; - - // Fix the min-heap property if it's violated - while (i != 0 && pq->data[(i - 1) / 2] > pq->data[i]) { - swap(&pq->data[i], &pq->data[(i - 1) / 2]); - i = (i - 1) / 2; - } -} - -// Remove element with highest priority (smallest element in min-heap) -int dequeue(PriorityQueue *pq) { - if (pq->size <= 0) { - printf("Priority queue underflow\n"); - return -1; - } - if (pq->size == 1) { - return pq->data[--pq->size]; - } - - // Store the minimum value, remove it, and fix the heap - int root = pq->data[0]; - pq->data[0] = pq->data[--pq->size]; - minHeapify(pq, 0); - - return root; -} - -// Peek at the highest-priority element -int peek(PriorityQueue *pq) { - if (pq->size <= 0) { - printf("Priority queue is empty\n"); - return -1; - } - return pq->data[0]; -} - -// Main function to demonstrate priority queue -int main() { - PriorityQueue pq; - pq.size = 0; - - enqueue(&pq, 10); - enqueue(&pq, 20); - enqueue(&pq, 5); - enqueue(&pq, 7); - - printf("Highest priority element: %d\n", peek(&pq)); - printf("Dequeued element: %d\n", dequeue(&pq)); - printf("Highest priority element after dequeue: %d\n", peek(&pq)); - - return 0; -} diff --git a/Priority Queue/Readme.md b/Priority Queue/Readme.md deleted file mode 100644 index 8e3891d9..00000000 --- a/Priority Queue/Readme.md +++ /dev/null @@ -1,26 +0,0 @@ -# Definition - -In C, a priority queue is a special type of queue where each element has a priority associated with it. Elements with higher priority are served before elements with lower priority. If two elements have the same priority, they are served based on their order in the queue (depending on whether it's a min-heap or max-heap). - -A priority queue can be implemented in various ways, but one of the most efficient implementations is using a binary heap: -1) Max-Heap: In a max-heap, the element with the highest priority (maximum value) is at the root of the heap, making it easy to access and remove. -2) Min-Heap: In a min-heap, the element with the lowest priority (minimum value) is at the root. - -# Operations in a Priority Queue -1) Insert (Enqueue): Insert an element into the priority queue while maintaining the heap property. -2) Remove (Dequeue): Remove the element with the highest priority (root element in a max-heap or min-heap). -3) Peek: Access the highest-priority element without removing it. - - - -# Explanation of Code -1) enqueue: Inserts a new element at the end of the heap array and then "bubbles up" to restore the min-heap property. -2) dequeue: Removes the root (smallest) element and replaces it with the last element in the array, then "bubbles down" to maintain the min-heap. -3) peek: Returns the highest-priority element without removing it. - -# Complexity Analysis -1) Insertion (enqueue): O(logn) because it may require "bubbling up." -2) Deletion (dequeue): O(logn) due to "bubbling down." -3) Peek: O(1) since the root is always the highest-priority element. - -This implementation offers a simple, efficient way to manage a priority queue in C using a min-heap.