summaryrefslogtreecommitdiff
path: root/rushs/tinyprintf/heap/add.c
diff options
context:
space:
mode:
authorMartial Simon <msimon_fr@hotmail.com>2025-09-15 01:08:27 +0200
committerMartial Simon <msimon_fr@hotmail.com>2025-09-15 01:08:27 +0200
commitc9b6b9a5ca082fe7c1b6f58d7713f785a9eb6a5c (patch)
tree3e4f42f93c7ae89a364e4d51fff6e5cec4e55fa9 /rushs/tinyprintf/heap/add.c
add: graphs et rushs
Diffstat (limited to 'rushs/tinyprintf/heap/add.c')
-rw-r--r--rushs/tinyprintf/heap/add.c39
1 files changed, 39 insertions, 0 deletions
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 <stdlib.h>
+
+#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);
+}