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 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193
|
#include<iostream>
#include<vector>
#include<string>
#include<sstream>
#include<fstream>
using namespace std;
// #pragma once
#define ROOT 1
typedef struct Job *job,j;
struct Job {
int jobID;
int execute_time;
int total_time;
};
//job jobs[10000];
class minHeap {
public:
job jobs[10];
int len;
int size;
const static int multiple = 2;
minHeap() {
len = 0;
for (int i = 0; i < 10; i++) {
jobs[i] = new j;
jobs[i]->jobID = jobs[i]->execute_time = jobs[i]->total_time = 0;
size++;
}
}
void ShiftDown(int);
void ShiftUp(int);
void InsertHeap(int, int);
int Parent(int);
int LeftChild(int);
int RightChild(int);
bool Empty();
job Pop();
void swap(int,int);
job top();
job empty_job() {return jobs[0];}
job ArrayDynamicAllocation(job array[], int size);
// void check_size();
private:
int _v(int);
};
job minHeap::top() {
return jobs[ROOT];
}
job minHeap::ArrayDynamicAllocation(job array[], int size)
{
job new_array[size*2];
job tmp_array[size];
for(int k=0; k<size; k++)//Initial array copying.
tmp_array[k] = array[k];
for(int i=0; i<size; i++)//Array range 0 to n-1
{
new_array[i]=tmp_array[i];
new_array[i+size] = new j;
new_array[i+size]->jobID = new_array[i+size]->execute_time = new_array[i+size]->total_time = 0;
}
delete tmp_array[size]; //Deleting old array
return new_array[size*2];
};
void minHeap::InsertHeap(int jobID, int total_time) {
job jo = new j;
jo->jobID = jobID;
jo->total_time = total_time;
jo->execute_time = 0;
if ((len+2) == size){
job tmp;
tmp = ArrayDynamicAllocation(jobs, size);
size *= multiple;
job jobs[size];
for (int i = 0; i < size; ++i ){
jobs[i] = tmp+i;
}
}
jobs[++len] = jo;
ShiftUp(len);
}
job minHeap::Pop() {
job j = jobs[ROOT];
swap(ROOT, len);
jobs[len]->jobID = jobs[len]->execute_time = jobs[len]->total_time = 0;
len--;
ShiftDown(ROOT);
return j;
}
void minHeap::swap(int a, int b) {
job t = jobs[a];
jobs[a] = jobs[b];
jobs[b] = t;
}
int minHeap::_v(int index) {
return jobs[index]->execute_time;
}
void minHeap::ShiftUp(int index) {
while (_v(index) < _v(Parent(index)) && index > 1) {
swap(index, Parent(index));
index = Parent(index);
}
}
void minHeap::ShiftDown(int index) {
while (index < len) {
int v = _v(index);
int location = index;
if (LeftChild(index) <= len) {
if (_v(LeftChild(index)) <= _v(location)) {
if (_v(LeftChild(index)) == _v(location)) {
if (jobs[LeftChild(index)]->jobID < jobs[location]->jobID) {
v = _v(LeftChild(index));
location = LeftChild(index);
}
}
else {
v = _v(LeftChild(index));
location = LeftChild(index);
}
}
}
if (RightChild(index) <= len) {
if (_v(RightChild(index)) < _v(location)) {
if (_v(RightChild(index)) == _v(location)) {
if (jobs[RightChild(index)]->jobID < jobs[location]->jobID) {
v = _v(RightChild(index));
location = RightChild(index);
}
}
else {
v = _v(RightChild(index));
location = RightChild(index);
}
}
}
if (location == index)break;
swap(index, location);
index = location;
}
}
int minHeap::Parent(int index) {
return index / 2;
}
int minHeap::LeftChild(int index) {
return index * 2;
}
int minHeap::RightChild(int index) {
return index * 2 + 1;
}
bool minHeap::Empty() {
return len == 0;
}
int main() {
minHeap min;
int sz = 11;
for (int i = 1; i < sz; ++i){
min.InsertHeap(i, i+20);
}
for (int i = 0; i < sz; ++i){
cout << min. jobs[i]->jobID << " " ;
}
cout << min.len << "," << min.size << endl;
}
|