From 19457929089c556ec252e5aaf15a8029ff508940 Mon Sep 17 00:00:00 2001 From: Karthik Yandrapu Date: Fri, 25 Oct 2024 23:37:47 +0530 Subject: [PATCH] Added Max Heap --- Heaps/Max Heap/Program.c | 119 +++++++++++++++++++++++++++++++++++++++ Heaps/Max Heap/README.md | 86 ++++++++++++++++++++++++++++ 2 files changed, 205 insertions(+) create mode 100644 Heaps/Max Heap/Program.c create mode 100644 Heaps/Max Heap/README.md diff --git a/Heaps/Max Heap/Program.c b/Heaps/Max Heap/Program.c new file mode 100644 index 00000000..6bf3c8d1 --- /dev/null +++ b/Heaps/Max Heap/Program.c @@ -0,0 +1,119 @@ +#include +#include +#include + +typedef struct max_heap { + int *p; + int size; + int count; +} Heap; + +Heap *create_heap(Heap *heap); // Initializes a new heap +void down_heapify(Heap *heap, int index); // Restores max-heap property from the specified index downwards +void up_heapify(Heap *heap, int index); // Restores max-heap property from the specified index upwards +void push(Heap *heap, int x); // Inserts a new element into the heap +void pop(Heap *heap); // Removes the maximum element from the heap +int top(Heap *heap); // Retrieves the maximum element of the heap +int empty(Heap *heap); // Checks if the heap is empty +int size(Heap *heap); // Returns the current number of elements in the heap + +int main() { + Heap *head = create_heap(head); + push(head, 10); + printf("Pushing element : 10\n"); + push(head, 3); + printf("Pushing element : 3\n"); + push(head, 2); + printf("Pushing element : 2\n"); + push(head, 8); + printf("Pushing element : 8\n"); + printf("Top element = %d \n", top(head)); + push(head, 1); + printf("Pushing element : 1\n"); + push(head, 7); + printf("Pushing element : 7\n"); + printf("Top element = %d \n", top(head)); + pop(head); + printf("Popping an element.\n"); + printf("Top element = %d \n", top(head)); + pop(head); + printf("Popping an element.\n"); + printf("Top element = %d \n", top(head)); + printf("\n"); + return 0; +} + +Heap *create_heap(Heap *heap) { + // Allocate memory for the heap and initialize its properties + heap = (Heap *)malloc(sizeof(Heap)); + heap->size = 2; // Initial capacity of the heap array + heap->p = (int *)malloc(heap->size * sizeof(int)); + heap->count = 0; + return heap; +} + +void down_heapify(Heap *heap, int index) { + // Reorder the heap starting from the specified index downwards + if (index >= heap->count) return; + int left = index * 2 + 1; + int right = index * 2 + 2; + int largest = index; + + if (left < heap->count && heap->p[left] > heap->p[largest]) + largest = left; + if (right < heap->count && heap->p[right] > heap->p[largest]) + largest = right; + if (largest != index) { + int temp = heap->p[largest]; + heap->p[largest] = heap->p[index]; + heap->p[index] = temp; + down_heapify(heap, largest); + } +} + +void up_heapify(Heap *heap, int index) { + // Move element at index up to restore heap property + int parent = (index - 1) / 2; + if (parent >= 0 && heap->p[index] > heap->p[parent]) { + int temp = heap->p[index]; + heap->p[index] = heap->p[parent]; + heap->p[parent] = temp; + up_heapify(heap, parent); + } +} + +void push(Heap *heap, int x) { + // Insert an element into the heap, resizing if necessary + if (heap->count == heap->size) { + heap->size *= 2; + heap->p = (int *)realloc(heap->p, heap->size * sizeof(int)); + } + heap->p[heap->count++] = x; + up_heapify(heap, heap->count - 1); +} + +void pop(Heap *heap) { + // Remove the top (max) element and reorder the heap + if (heap->count == 0) return; + heap->count--; + heap->p[0] = heap->p[heap->count]; + down_heapify(heap, 0); + // Shrink array if necessary + if (heap->count > 0 && heap->count <= heap->size / 4) { + heap->size /= 2; + heap->p = (int *)realloc(heap->p, heap->size * sizeof(int)); + } +} + +int top(Heap *heap) { + // Return max element or INT_MIN if heap is empty + return heap->count != 0 ? heap->p[0] : INT_MIN; +} + +int empty(Heap *heap) { + return heap->count == 0; +} + +int size(Heap *heap) { + return heap->count; +} \ No newline at end of file diff --git a/Heaps/Max Heap/README.md b/Heaps/Max Heap/README.md new file mode 100644 index 00000000..c4e4a693 --- /dev/null +++ b/Heaps/Max Heap/README.md @@ -0,0 +1,86 @@ + +# Heap Implementation in C + +This document provides a detailed explanation of a max-heap data structure implemented in C. This implementation includes functions to insert, remove, and retrieve the maximum element, along with other utility functions. + +## Code Overview + +This code defines a struct `max_heap` which is used to represent a max-heap. A max-heap is a complete binary tree in which each node is greater than or equal to its child nodes. The implementation uses an array for efficient indexing. + +### Struct Definition +```c +typedef struct max_heap { + int *p; + int size; + int count; +} Heap; +``` +- `p`: Pointer to an array representing the heap. +- `size`: Current allocated size of the array. +- `count`: Current number of elements in the heap. + +### Functions + +#### 1. `create_heap(Heap *heap)` +Creates a new max-heap with an initial capacity of 2. Returns a pointer to the heap structure. + +#### 2. `down_heapify(Heap *heap, int index)` +Restores the max-heap property by moving the element at the given `index` downwards. + +#### 3. `up_heapify(Heap *heap, int index)` +Moves the element at the given `index` upwards to maintain the max-heap property. + +#### 4. `push(Heap *heap, int x)` +Inserts a new element `x` into the heap. Resizes the heap array if the current capacity is reached. + +#### 5. `pop(Heap *heap)` +Removes the top (maximum) element from the heap and reorders the remaining elements. + +#### 6. `top(Heap *heap)` +Returns the maximum element in the heap. Returns `INT_MIN` if the heap is empty. + +#### 7. `empty(Heap *heap)` +Checks if the heap is empty. + +#### 8. `size(Heap *heap)` +Returns the number of elements currently in the heap. + +### Example Usage + +```c +Heap *head = create_heap(head); +push(head, 10); +push(head, 3); +push(head, 2); +push(head, 8); +printf("Top element = %d +", top(head)); +pop(head); +printf("After popping, Top element = %d +", top(head)); +``` + +### Complexity Analysis + +#### Time Complexity + +| Operation | Time Complexity | +|-----------|-----------------| +| `push` | O(log n) | +| `pop` | O(log n) | +| `top` | O(1) | + +- `push` and `pop` involve heapification, which takes `O(log n)` time as elements are moved up or down in the heap. +- `top` is `O(1)` because it only retrieves the maximum element. + +#### Space Complexity +- The space complexity is `O(n)`, where `n` is the number of elements in the heap. Additional memory is used only if the array needs to be resized. + +### Notes +- The max-heap property is maintained by comparing elements while inserting or deleting nodes. +- Dynamic resizing allows efficient memory usage while maintaining performance. + +## Conclusion + +This implementation provides efficient methods for a max-heap in C, ideal for priority queue operations where fast access to the maximum element is required. +