From c9b6b9a5ca082fe7c1b6f58d7713f785a9eb6a5c Mon Sep 17 00:00:00 2001 From: Martial Simon Date: Mon, 15 Sep 2025 01:08:27 +0200 Subject: add: graphs et rushs --- rushs/tinyprintf/heap/add.c | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 rushs/tinyprintf/heap/add.c (limited to 'rushs/tinyprintf/heap/add.c') diff --git a/rushs/tinyprintf/heap/add.c b/rushs/tinyprintf/heap/add.c new file mode 100644 index 0000000..78a4db8 --- /dev/null +++ b/rushs/tinyprintf/heap/add.c @@ -0,0 +1,39 @@ +#include + +#include "heap.h" + +void heapify(int arr[], int n, int i) +{ + if (i == 0) + return; + int parent = (i - 1) / 2; + if (parent >= 0) + { + if (arr[i] > arr[parent]) + { + int tmp = arr[i]; + arr[i] = arr[parent]; + arr[parent] = tmp; + heapify(arr, n, parent); + } + } +} + +void add(struct heap *heap, int val) +{ + if (heap->size == heap->capacity) + { + heap->array = realloc(heap->array, heap->capacity * 2 * sizeof(int)); + if (heap->array == NULL) + { + free(heap); + return; + } + heap->capacity *= 2; + } + + heap->array[heap->size] = val; + heap->size++; + + heapify(heap->array, heap->size, heap->size - 1); +} -- cgit v1.2.3