2021-02-07 22:31:04 +03:00
|
|
|
### Heap
|
2020-12-27 16:02:35 +03:00
|
|
|
|
|
|
|
### Overview
|
|
|
|
|
|
|
|
- Min-heap implementation, it can be used as Max-heap/priority queue as well.
|
|
|
|
|
2021-02-07 22:31:04 +03:00
|
|
|
### Usage
|
2020-12-27 16:02:35 +03:00
|
|
|
|
|
|
|
```c
|
|
|
|
|
|
|
|
#include "sc_heap.h"
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
|
|
|
|
|
|
|
int main(int argc, char *argv[])
|
|
|
|
{
|
2021-04-27 20:19:25 +03:00
|
|
|
struct data {
|
|
|
|
int priority;
|
|
|
|
char *data;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct data n[] = {{1, "first"},
|
|
|
|
{4, "fourth"},
|
|
|
|
{5, "fifth"},
|
|
|
|
{3, "third"},
|
|
|
|
{2, "second"}};
|
|
|
|
|
|
|
|
struct sc_heap_data *elem;
|
|
|
|
struct sc_heap heap;
|
|
|
|
|
|
|
|
sc_heap_init(&heap, 0);
|
|
|
|
|
|
|
|
// Min-heap usage
|
|
|
|
for (int i = 0; i < 5; i++) {
|
|
|
|
sc_heap_add(&heap, n[i].priority, n[i].data);
|
|
|
|
}
|
|
|
|
|
|
|
|
while ((elem = sc_heap_pop(&heap)) != NULL) {
|
2022-02-05 00:11:52 +03:00
|
|
|
printf("key = %d, data = %s \n",
|
|
|
|
(int) elem->key, (char*) elem->data);
|
2021-04-27 20:19:25 +03:00
|
|
|
}
|
|
|
|
printf("---------------- \n");
|
|
|
|
|
|
|
|
// Max-heap usage, negate when adding into heap and negate back after
|
|
|
|
// pop :
|
|
|
|
for (int i = 0; i < 5; i++) {
|
|
|
|
sc_heap_add(&heap, -(n[i].priority), n[i].data);
|
|
|
|
}
|
|
|
|
|
|
|
|
while ((elem = sc_heap_pop(&heap)) != NULL) {
|
2022-02-05 00:11:52 +03:00
|
|
|
printf("key = %d, data = %s \n",
|
|
|
|
(int) elem->key, (char*) elem->data);
|
2021-04-27 20:19:25 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
sc_heap_term(&heap);
|
|
|
|
|
|
|
|
return 0;
|
2020-12-27 16:02:35 +03:00
|
|
|
}
|
|
|
|
```
|