You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
37 lines
716 B
37 lines
716 B
3 months ago
|
Binary heaps
|
||
|
============
|
||
|
|
||
|
* <<intro,Introduction>>
|
||
|
* <<macros,Macros>>
|
||
|
* <<example,Example>>
|
||
|
|
||
|
!!ucw/heap.h
|
||
|
|
||
|
[[example]]
|
||
|
Example
|
||
|
-------
|
||
|
|
||
|
static uint n;
|
||
|
static int heap[4];
|
||
|
|
||
|
// Create an empty heap
|
||
|
n = 0;
|
||
|
#define MY_CMP(x, y) ((x) < (y))
|
||
|
|
||
|
// Insert 20, 10, 30
|
||
|
HEAP_INSERT(int, heap, n, MY_CMP, HEAP_SWAP, 20);
|
||
|
HEAP_INSERT(int, heap, n, MY_CMP, HEAP_SWAP, 10);
|
||
|
HEAP_INSERT(int, heap, n, MY_CMP, HEAP_SWAP, 30);
|
||
|
|
||
|
// Remove the minimum (10)
|
||
|
HEAP_DELETE_MIN(int, heap, n, MY_CMP, HEAP_SWAP);
|
||
|
|
||
|
// Print the new minimum (20)
|
||
|
printf("%d", heap[1]);
|
||
|
|
||
|
// Increase the minimum to 40
|
||
|
HEAP_INCREASE(int, heap, n, MY_CMP, HEAP_SWAP, 1, 40);
|
||
|
|
||
|
// Print the new minimum (30)
|
||
|
printf("%d", heap[1]);
|