-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathheap.c
108 lines (93 loc) · 3.04 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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include <stdbool.h>
#include "minHeap.h"
void swapHeapElements(Heap *heap, int currentPosition, int parent){
Node temp = heap->timeHolderList[currentPosition];
heap->timeHolderList[currentPosition] = heap->timeHolderList[parent];
heap->timeHolderList[parent] = temp;
}
void init(Heap *heap){
heap->size = INITINAL_HEAP_SIZE;
heap->numberOfElements = 0;
heap->scheduledTimes=0;
heap->expiredTimes=0;
heap->timeHolderList = (Node*)malloc(sizeof(Node)*heap->size);
}
void extendHeap(Heap *heap){
heap->size = heap->size * 2;
heap->timeHolderList = realloc(heap->timeHolderList,heap->size*(sizeof(Node)));
}
int getParentIndex(int nodeIndex){
return (nodeIndex - 1) / 2;
}
int getLeftChildIndex(int nodeIndex){
return 2 * nodeIndex + 1;
}
int getRightChildIndex(int nodeIndex){
return 2 * nodeIndex + 2;
}
int findMinChild(Heap *heap, int currentPosition){
int leftChildIndex = getLeftChildIndex(currentPosition);
int rightChildIndex = getRightChildIndex(currentPosition);
if(rightChildIndex >= heap->numberOfElements){
return leftChildIndex;
}else{
if(heap->timeHolderList[leftChildIndex].time <= heap->timeHolderList[rightChildIndex].time){
return leftChildIndex;
}
return rightChildIndex;
}
}
void downHeap(Heap *heap,int currentPosition){
int minChild = findMinChild(heap,currentPosition);
if((heap->timeHolderList[currentPosition]).time > (heap->timeHolderList[minChild]).time && minChild <= heap->numberOfElements){
swapHeapElements(heap,currentPosition,minChild);
downHeap(heap, minChild);
}
}
void upHeap(Heap *heap,int nodeIndex){
int parent;
if(nodeIndex != 0){
int parentIndex = getParentIndex(nodeIndex);
if(heap->timeHolderList[parentIndex].time > heap->timeHolderList[nodeIndex].time){
swapHeapElements(heap,nodeIndex,parentIndex);
upHeap(heap,parentIndex);
}
}
}
void addElement(Heap *heap, Node* heapNode){
if(heap->numberOfElements == heap->size){
extendHeap(heap);
}
heap->scheduledTimes++;
heap->numberOfElements++;
heap->timeHolderList[heap->numberOfElements-1] = *heapNode;
upHeap(heap,heap->numberOfElements-1);
}
Node* removeTopElement(Heap *heap){
Node temp;
temp = heap->timeHolderList[0];
heap->timeHolderList[0] = heap->timeHolderList[heap->numberOfElements-1];
heap->numberOfElements--;
heap->expiredTimes++;
downHeap(heap,0);
Node *ret;
ret=(Node*)malloc(sizeof(Node));
ret->time = temp.time;
ret->roomNumber = temp.roomNumber;
return ret;
}
Node* peek(Heap *heap){
Node temp;
temp = heap->timeHolderList[0];
Node *ret;
ret=(Node*)malloc(sizeof(Node));
ret->time = temp.time;
ret->roomNumber = temp.roomNumber;
return ret;
}
bool isEmpty(Heap *heap){
return heap->numberOfElements < 1;
}