]> mj.ucw.cz Git - libucw.git/blob - ucw/doc/heap.txt
Updated years in all README's.
[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[n + 1] = 20;
23   HEAP_INSERT(int, heap, n, MY_CMP, HEAP_SWAP);
24   heap[n + 1] = 10;
25   HEAP_INSERT(int, heap, n, MY_CMP, HEAP_SWAP);
26   heap[n + 1] = 30;
27   HEAP_INSERT(int, heap, n, MY_CMP, HEAP_SWAP);
28
29   // Remove the minimum (10)
30   HEAP_DELMIN(int, heap, n, MY_CMP, HEAP_SWAP);
31
32   // Print the new minimum (20)
33   printf("%d", heap[1]);
34
35   // Increase the minimum by 20 to 40
36   heap[1] += 20;
37   HEAP_INCREASE(int, heap, n, MY_CMP, HEAP_SWAP, 1);
38
39   // Print the new minimum (30)
40   printf("%d", heap[1]);