/
heap.c
56 lines (37 loc) · 1.05 KB
/
heap.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include "heap.h"
uint8_t HeapRemove( uint8_t * heapdata, uint8_t * heaplen, uint16_t * heapvals )
{
uint8_t ret = heapdata[0];
(*heaplen)--;
if( *heaplen == 0 ) ret;
heapdata[0] = heapdata[*heaplen];
uint8_t i = 0;
while( !( (i >= *heaplen/2) && (i < *heaplen) ) ) //Not a leaf...
{
int j = i*2+1; //left child.
if( j >= *heaplen ) break;
if( j < *heaplen -1 && heapvals[heapdata[j]] < heapvals[heapdata[j+1]] ) //?? is the > here right?
j++; //Get index of lowest? value.
if( heapvals[heapdata[j]] < heapvals[heapdata[i]] ) break;
uint8_t v = heapdata[j];
heapdata[j] = heapdata[i];
heapdata[i] = v;
i = j;
}
return ret;
}
void HeapAdd( uint8_t * heapdata, uint8_t * heaplen, uint8_t data, uint16_t * heapvals )
{
heapdata[*heaplen] = data;
uint8_t curr = *heaplen;
uint8_t parent = (curr-1)/2;
while( curr != 0 && heapvals[heapdata[curr]] > heapvals[heapdata[parent]] )
{
uint8_t d = heapdata[curr];
heapdata[curr] = heapdata[parent];
heapdata[parent] = d;
curr = parent;
parent = (curr-1)/2;
}
(*heaplen)++;
}