





Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!






#include
class arrayheap { private: int *heap; int mxsize; int currsize; int mach; public: arrayheap(int size, int m) { mach = m; mxsize = size; heap = new int[mxsize]; currsize = 0; }
~arrayheap() { }
void push(int num) { heap[currsize] = num; pushheap(0, currsize); currsize++; }
void pushheap(int root, int bottom) { int parent; int temp;
if (bottom > root) { parent = (bottom - 1) / 2; if (heap[parent] > heap[bottom]) { temp = heap[parent]; heap[parent] = heap[bottom]; heap[bottom] = temp; pushheap(root, parent); } }
void print() { for (int i = 0; i< currsize; i++) { cout << heap[i] << endl; } }
void pop() { int temp = heap[0]; currsize--;
heap[0] = heap[currsize]; popheap(0, currsize - 1); }
void popheap(int root, int bottom) { int max; int right; int left; int temp;
left = root * 2 + 1; right = root * 2 + 2;
if (left <= bottom) { if (left == bottom) { max = left; } else { if (heap[left] >= heap[right]) { max = right; } else { max = left; } } if (heap[root] > heap[max]) {
temp = heap[root]; heap[root] = heap[max]; heap[max] = temp;
num[place] = num[place] + t; string Result; ostringstream convert; convert << t; Result = convert.str(); out[place] = out[place] + Result + " "; i++; }
int max = 0;
for (int i = 0; i < mach; i++) { if (max < num[i]) { max = num[i]; } }
cout << "Min Heap Finishing Time: " << max << endl; cout << "Schedule: " << endl; for (int i = 0; i < mach; i++) { cout << "Machine " << i + 1 << ": "<< out[i] << endl; }
class lefttree { private: struct node { int num; struct node *left = NULL; struct node *right = NULL; int s; }; int size; node *root; int mach; int mxsize; public: lefttree(int z, int m) { mxsize = z; mach = m; root = NULL; size = 0; }
~lefttree() { }
void push(int element)
{ struct node *p; p = new(struct node); p->num = element; meld(root, p); size++; }
void meld(node* &a, node* b)
{
if (!b) { return; } if (!a) { a = b; return; }
if (a->num > b->num) { struct node *temp; temp = new(struct node); temp = a; a = b; b = temp; } meld(a->right, b); if (!a->left) { a->left = a->right; a->right = 0; a->s = 1; } else { if (a->left->s < a->right->s) { struct node *temp; temp = new(struct node); temp = a->left; a->left = a->right;
//postorder if (p) { traverse(p->left); traverse(p->right); cout << p->num << " ";
void LPT() { int *temp = new int[mxsize]; int j = mxsize - 1; while (j >= 0) { temp[j] = root->num; pop(); j--; } int *num = new int[mach]; string *out = new string[mach];
for (int i = 0; i < mach; i++) { out[i] = ""; num[i] = 0; }
int i = 0;
while (i < mxsize) { int place = 0; int j = 1; int t = temp[i]; int comp = num[0]; while (j < mach) { if (comp > num[j]) { place = j; comp = num[j]; } j++; } num[place] = num[place] + t; string Result; ostringstream convert; convert << t; Result = convert.str();
out[place] = out[place] + Result + " "; i++; }
int max = 0;
for (int i = 0; i < mach; i++) { if (max < num[i]) { max = num[i]; } }
cout << "Height Biased Leftist Tree: " << max << endl; cout << "Schedule: " << endl; for (int i = 0; i < mach; i++) { cout << "Machine " << i + 1 << ": " << out[i] << endl; } } };
int main() { int j; int m; int in; cout << "Enter number of job(s):" << endl; cin >> j; cout << "Enter number of machine(s):" << endl; cin >> m; arrayheap *theHeap = new arrayheap(j, m); lefttree *tree = new lefttree(j, m); cout << "Enter task size(s):" << endl; for (int i = 0; i < j; i++) { cin >> in; theHeap->push(in); tree->push(in); }
using std::chrono::duration_cast; using std::chrono::nanoseconds; typedef std::chrono::high_resolution_clock clock; auto start = clock::now(); theHeap->LPT(); auto end = clock::now(); cout << "Time Elapsed: " << duration_cast