]> mj.ucw.cz Git - libucw.git/blob - ucw/doc/heap.txt
605c696dadfe650d257e5dafc492fcd95a8a7a77
[libucw.git] / ucw / doc / heap.txt
1 Binary heaps
2 ============
3
4 * <<intro,Introduction>>
5 * <<macros,Macros>>
6 * <<example,Example>>
7
8 !!ucw/heap.h
9
10 [[example]]
11 Example
12 -------
13
14   static uns n;
15   static int heap[4];
16
17   // Create an empty heap
18   n = 0;
19   #define MY_CMP(x, y) ((x) < (y))
20
21   // Insert 20, 10, 30
22   HEAP_INSERT(int, heap, n, MY_CMP, HEAP_SWAP, 20);
23   HEAP_INSERT(int, heap, n, MY_CMP, HEAP_SWAP, 10);
24   HEAP_INSERT(int, heap, n, MY_CMP, HEAP_SWAP, 30);
25
26   // Remove the minimum (10)
27   HEAP_DELETE_MIN(int, heap, n, MY_CMP, HEAP_SWAP);
28
29   // Print the new minimum (20)
30   printf("%d", heap[1]);
31
32   // Increase the minimum to 40
33   HEAP_INCREASE(int, heap, n, MY_CMP, HEAP_SWAP, 1, 40);
34
35   // Print the new minimum (30)
36   printf("%d", heap[1]);