diff options
Diffstat (limited to 'rushs/evalexpr/heap/add.c')
| -rw-r--r-- | rushs/evalexpr/heap/add.c | 39 |
1 files changed, 39 insertions, 0 deletions
diff --git a/rushs/evalexpr/heap/add.c b/rushs/evalexpr/heap/add.c new file mode 100644 index 0000000..78a4db8 --- /dev/null +++ b/rushs/evalexpr/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); +} |
