-
Notifications
You must be signed in to change notification settings - Fork 0
/
heap.cpp
123 lines (106 loc) · 1.86 KB
/
heap.cpp
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#include "heap.hpp"
heap::heap(){
size = 0;
}
heap::~heap(){
}
void heap::enqueue(string a, int b, int c){
enhelp(a, b, c, size+1);
size++;
}
void heap::dequeue(){
while(size!=0)
{
array[1]=array[size];
size--;
down(1);
}
}
void heap::print(){
cout<<"Rank "<<"patient, "<<"Priority, "<<"Treatment"<<endl;
int i=1;
while(size!=0)
{
cout<<i<<": "<<array[1].name<<", "<<array[1].priority<<", "<<array[1].treatment<<endl;
array[1]=array[size];
size--;
i++;
down(1);
}
}
void heap::enhelp(string a, int b, int c, int i){
patient tem(a, b, c);
array[i] = tem;
up(i);
}
void heap::down(int i){
if( 2*i <= size && (2*i)+1 <= size)
{
if(array[2*i].priority == array[(2*i)+1].priority)
{
if(array[2*i].treatment < array[(2*i)+1].treatment)
{
patient tem = array[i];
array[i] = array[2*i];
array[2*i] = tem;
down(2*i);
}
else
{
patient tem = array[i];
array[i] = array[(2*i)+1];
array[(2*i)+1] = tem;
down((2*i)+1);
}
}
else{
if(array[2*i].priority < array[(2*i)+1].priority)
{
patient tem = array[i];
array[i] = array[2*i];
array[2*i] = tem;
down(2*i);
}
else
{
patient tem = array[i];
array[i] = array[(2*i)+1];
array[(2*i)+1] = tem;
down((2*i)+1);
}
}
}
else{
if(2*i <= size)
{
patient tem = array[i];
array[i] = array[2*i];
array[2*i] = tem;
down(2*i);
}
}
}
void heap::up(int i){
if(i > 1){
int parent = (int)(i / 2);
if(array[i].priority == array[parent].priority)
{
if(array[i].treatment < array[parent].treatment)
{
patient tem = array[i];
array[i] = array[parent];
array[parent] = tem;
up(parent);
}
}
else{
if(array[i].priority < array[parent].priority)
{
patient tem = array[i];
array[i] = array[parent];
array[parent] = tem;
up(parent);
}
}
}
}