From f8aa9328be211e9047bcca98dc5ee775f14e436b Mon Sep 17 00:00:00 2001 From: Rahul Rajeev Pillai <66192267+Ashborn-SM@users.noreply.github.com> Date: Mon, 12 Jul 2021 18:00:17 +0530 Subject: [PATCH] chore(C): add min and max heap (#386) --- algorithms/C/README.md | 4 +- algorithms/C/tree/max-heap.c | 118 +++++++++++++++++++++++++++++++++++ algorithms/C/tree/min-heap.c | 118 +++++++++++++++++++++++++++++++++++ 3 files changed, 239 insertions(+), 1 deletion(-) create mode 100644 algorithms/C/tree/max-heap.c create mode 100644 algorithms/C/tree/min-heap.c diff --git a/algorithms/C/README.md b/algorithms/C/README.md index 1badbdb4..2d00b6e0 100644 --- a/algorithms/C/README.md +++ b/algorithms/C/README.md @@ -33,11 +33,13 @@ - [Palindrome](strings/palindrome.c) - [Permutation of String](strings/Permutation-of-String.c) - ## Tree +## Tree - [Height Of Tree](tree/height-of-a-tree.c) - [Max and Min Element Of Tree](tree/min-and-max-of-tree.c) - [Binary Search Tree](tree/binary-search-tree.c) - [Avl Tree](tree/avl-tree.c) +- [Min Heap](tree/min-heap.c) +- [Max Heap](tree/max-heap.c) ## Searching - [Binary Search](searching/Binary-search.c) diff --git a/algorithms/C/tree/max-heap.c b/algorithms/C/tree/max-heap.c new file mode 100644 index 00000000..decf6580 --- /dev/null +++ b/algorithms/C/tree/max-heap.c @@ -0,0 +1,118 @@ +/* +Max-Heap is a binary tree structure such that every node in the tree will be +lesser or equal to the parent node. It is used when you need quick access to +the largest number in the array. + +Time complexity to build the heap: O(n) +Time complexity to remove max: O(log(n)) +Time complexity to remove all elements: O(n*log(n)) +*/ + +#include + +#include + +struct HEAP { + int * items, size, capacity; +}* heap; + +void swap(int * a, int * b) { + int temp = * b; + * b = * a; + * a = temp; +} + +void init(struct HEAP ** heap) { + * heap = (struct HEAP * ) malloc(sizeof(struct HEAP)); + ( * heap) -> capacity = 2; + ( * heap) -> size = 0; + ( * heap) -> items = (int * ) malloc(sizeof(int) * ( * heap) -> capacity + 1); +} + +void allocate(struct HEAP ** heap) { + ( * heap) -> capacity *= 2; + ( * heap) -> items = (int * ) realloc(( * heap) -> items, sizeof(int) * ( * heap) -> capacity + 1); +} + +void heapify_up(struct HEAP ** heap, int size) { + if (size > 1) { + if (( * heap) -> items[size / 2] < ( * heap) -> items[size]) { + swap( & ( * heap) -> items[size / 2], & ( * heap) -> items[size]); + heapify_up(heap, size / 2); + } + } +} + +void insert(struct HEAP ** heap, int val) { + if (( * heap) -> size == ( * heap) -> capacity) { + allocate(heap); + } + ( * heap) -> items[++( * heap) -> size] = val; + heapify_up(heap, ( * heap) -> size); +} + +int child(struct HEAP * heap, int index) { + int left = index * 2; + int right = index * 2 + 1; + + if (right > heap -> size) { + return left; + } else if (heap -> items[left] >= heap -> items[right]) { + return left; + } + return right; +} + +void heapify_down(struct HEAP ** heap, int index) { + int childindex = child( * heap, index); + if (index * 2 <= ( * heap) -> size) { + if (( * heap) -> items[index] < ( * heap) -> items[childindex]) { + swap( & ( * heap) -> items[index], & ( * heap) -> items[childindex]); + heapify_down(heap, childindex); + } + } +} + +void removemax(struct HEAP ** heap) { + swap( & ( * heap) -> items[1], & ( * heap) -> items[( * heap) -> size--]); + heapify_down(heap, 1); +} + +void print(struct HEAP * heap) { + for (int i = 1; i <= heap -> size; i++) { + printf("%i ", heap -> items[i]); + } + printf("\n"); +} + +void deallocate(struct HEAP ** heap) { + free(( * heap) -> items); + free( * heap); +} + +int main(void) { + init( & heap); + + insert( & heap, 4); + insert( & heap, 10); + insert( & heap, 2); + insert( & heap, 22); + insert( & heap, 45); + insert( & heap, 18); + + // Before removemax + print(heap); + + removemax( & heap); + + // After removemax + print(heap); + /* + (before removemax)Output: 45 22 18 4 10 2 + (after removemax)Output: 22 10 18 4 2 + */ + + deallocate( & heap); + + return 0; +} diff --git a/algorithms/C/tree/min-heap.c b/algorithms/C/tree/min-heap.c new file mode 100644 index 00000000..d980b18b --- /dev/null +++ b/algorithms/C/tree/min-heap.c @@ -0,0 +1,118 @@ +/* +Min-Heap is a binary tree structure such that every node in the tree will be +lesser or equal to the child node. It is used when you need quick access to +the smallest number in the array. + +Time complexity to build the heap: O(n) +Time complexity to remove min: O(log(n)) +Time complexity to remove all elements: O(n*log(n)) +*/ + +#include + +#include + +struct HEAP { + int * items, size, capacity; +}* heap; + +void swap(int * a, int * b) { + int temp = * b; + * b = * a; + * a = temp; +} + +void init(struct HEAP ** heap) { + * heap = (struct HEAP * ) malloc(sizeof(struct HEAP)); + ( * heap) -> capacity = 2; + ( * heap) -> size = 0; + ( * heap) -> items = (int * ) malloc(sizeof(int) * ( * heap) -> capacity + 1); +} + +void allocate(struct HEAP ** heap) { + ( * heap) -> capacity *= 2; + ( * heap) -> items = (int * ) realloc(( * heap) -> items, sizeof(int) * ( * heap) -> capacity + 1); +} + +void heapify_up(struct HEAP ** heap, int size) { + if (size > 1) { + if (( * heap) -> items[size / 2] > ( * heap) -> items[size]) { + swap( & ( * heap) -> items[size / 2], & ( * heap) -> items[size]); + heapify_up(heap, size / 2); + } + } +} + +void insert(struct HEAP ** heap, int val) { + if (( * heap) -> size == ( * heap) -> capacity) { + allocate(heap); + } + ( * heap) -> items[++( * heap) -> size] = val; + heapify_up(heap, ( * heap) -> size); +} + +int child(struct HEAP * heap, int index) { + int left = index * 2; + int right = index * 2 + 1; + + if (right > heap -> size) { + return left; + } else if (heap -> items[left] <= heap -> items[right]) { + return left; + } + return right; +} + +void heapify_down(struct HEAP ** heap, int index) { + int childindex = child( * heap, index); + if (index * 2 <= ( * heap) -> size) { + if (( * heap) -> items[index] > ( * heap) -> items[childindex]) { + swap( & ( * heap) -> items[index], & ( * heap) -> items[childindex]); + heapify_down(heap, childindex); + } + } +} + +void removemin(struct HEAP ** heap) { + swap( & ( * heap) -> items[1], & ( * heap) -> items[( * heap) -> size--]); + heapify_down(heap, 1); +} + +void print(struct HEAP * heap) { + for (int i = 1; i <= heap -> size; i++) { + printf("%i ", heap -> items[i]); + } + printf("\n"); +} + +void deallocate(struct HEAP ** heap) { + free(( * heap) -> items); + free( * heap); +} + +int main(void) { + init( & heap); + + insert( & heap, 4); + insert( & heap, 10); + insert( & heap, 2); + insert( & heap, 22); + insert( & heap, 45); + insert( & heap, 18); + + // Before removemin + print(heap); + + removemin( & heap); + + // After removemin + print(heap); + /* + (before removemin)Output:2 10 4 22 45 18 + (after removemin)Output: 4 10 18 22 45 + */ + + deallocate( & heap); + + return 0; +}