bool HuffmanEncoder::createWBT(ifstream &fileIn) { if(!fileIn.is_open()) { return false; } weight.resize(NUM_CHAR); char c; fileIn.get(c); do { int index = (unsigned char) c; weight[index] ++; totalNumberOfCharsEncoded++; fileIn.get(c); } while(!fileIn.eof()); PriorityQueue< WeightedBinaryTree<unsigned char> > q; WeightedBinaryTree<unsigned char> t, *pT1 = NULL, *pT2 = NULL, *pTRoot = NULL; for(int c = 0; c < NUM_CHAR; c++) { if(weight[c] > 0) { t.setData( (unsigned char) c); t.setWeight((int) weight[c]); q.Insert(t, (int) t.getWeight()); } } while(q.getSize() > 2) { q.Remove(pT1); q.Remove(pT2); pTRoot = new WeightedBinaryTree<unsigned char>(pT1, pT2); q.Insert(pTRoot, pTRoot->getWeight()); pT1 = NULL; pT2 = NULL; pTRoot = NULL; } if(q.getSize() == 2) { q.Remove(pT1); q.Remove(pT2); hufEnc.setLow(pT1); hufEnc.setHigh(pT2); hufEnc.setWeight(pT1->getWeight() + pT2->getWeight()); } else { q.Peek(hufEnc); } return true; }
int main() { // push , pop , top test { PriorityQueue<int> p; assert(p.getSize() == 0); assert(p.getCapacity() == 21); p.push(3);p.push(2);p.push(7);p.push(11);p.push(23); p.pop();p.pop(); p.push(3);p.push(2);p.push(43);p.push(21);p.push(25); p.pop();p.pop(); vector<int> vcmp = {7, 11, 21, 23, 25, 43}; assert(p.getSize() == 6); assert( vcmp == print_queue<int>(p)); assert(p.getCapacity() == 21); } // size , capacity , clear , destroy test { PriorityQueue<int> p; assert(p.getSize() == 0); assert(p.getCapacity() == 21); for(auto i = 0; i<21; i++){ p.push(i); } assert(p.getSize() == 21); assert(p.getCapacity() == 42); p.clear(); assert(p.getSize() == 0); assert(p.getCapacity() == 42); p.destroy(); assert(p.getSize() == 0); assert(p.getCapacity() == 0); } // heapsort test { vector<int> v = {1, 16, 12, 2, 25, 5, 6}; vector<int> cmpv = {1, 2, 5, 6, 12, 16, 25}; heap_sort(v.begin(), v.end()); assert(v == cmpv); } cout<< "All TestCases Pass."<<endl; return 0; }
int main() { cout << "\n\nLab 12a, PriorityQueueDriver.cpp\n"; cout << "Programmer: Aysin Oruz \n"; cout << "Editor(s) used: JNotePad and Xcode \n"; cout << "Compiler(s) used: Xcode and Terminal \n"; cout << "Description: The purpose of this lab is for you to learn how to create and apply our PriorityQueue file."; cout << "File: " << __FILE__ << endl; cout << "Compiled: " << __DATE__ << " at " << __TIME__ << endl; PriorityQueue<int> PQlist; cout << "Building a Priority Queue\n\n"; cout << "1-)Testing getsize function when queue is empty\n"; cout << "Expected >> 0\n"; assert(PQlist.getSize() == 0); cout << "Actual >> " << PQlist.getSize() << endl; cout << "\n2-)Testing enqueue function by adding new values\n"; for (int i = 0; i < 8; i++) PQlist.enqueue(i); assert(PQlist.getSize()); cout << "Expected >> 8\n"; cout << "Actual >> " << PQlist.getSize() << endl; cout << "\n3-) Testing Dequeue Function by removing largest value\n"; cout << "Removed value >> " << PQlist.dequeue() << endl; cout << "\n4-)Testing getsize function after dequeue\n"; cout << "Expected >> 7\n"; assert(PQlist.getSize() == 7); cout << "Actual >> " << PQlist.getSize() << endl; cout << "\n5-) Testing Dequeue Function by removing largest value\n"; cout << "Removed value >> " << PQlist.dequeue() << endl; cout << "\n6-)Testing getsize function after dequeue\n"; cout << "Expected >> 6\n"; assert(PQlist.getSize() == 6); cout << "Actual >> " << PQlist.getSize() << endl; cout << "\n7-) Testing Dequeue Function by removing largest values\n"; cout << "Removed value >> " << PQlist.dequeue() << endl; cout << "Removed value >> " << PQlist.dequeue() << endl; cout << "Removed value >> " << PQlist.dequeue() << endl; cout << "Removed value >> " << PQlist.dequeue() << endl; cout << "Removed value >> " << PQlist.dequeue() << endl; cout << "Removed value >> " << PQlist.dequeue() << endl; cout << "\n8-)Testing getsize function after dequeue\n"; cout << "Expected >> 0\n"; assert(PQlist.getSize() == 0); cout << "Actual >> " << PQlist.getSize() << endl; cout << "\nValues added back to PQ\n"; for (int i = 0; i < 8; i++) PQlist.enqueue(i); assert(PQlist.getSize()); //Const object copy testing with assignment UPON declaration { const PriorityQueue<int> copy= PQlist; cout << "\nCONST object copy testing with assignment UPON declaration\n"; cout << "\nTesting getsize()\nExpected >> 8\n"; cout << "Actual >> " << copy.getSize() << endl; assert(copy.getSize() == 8); } //object copy testing with assignment UPON declaration { PriorityQueue<int> copy = PQlist; cout << "\nobject copy testing with assignment UPON declaration\n"; cout << "\nTesting getsize()\nExpected >> 8\n"; cout << "Actual >> " << copy.getSize() << endl; assert(copy.getSize() == 8); cout << "\nDequeue all the values in the PQ\n"; assert(copy.dequeue() == 7); assert(copy.dequeue() == 6); assert(copy.dequeue() == 5); assert(copy.dequeue() == 4); assert(copy.dequeue() == 3); assert(copy.dequeue() == 2); assert(copy.dequeue() == 1); assert(copy.dequeue() == 0); cout << "\nGetsize() after dequeue\n"; cout << "Expected >> 0\n"; cout << "Actual >> " << copy.getSize() << endl; } // object copy testing with assignment Afterdeclaration { PriorityQueue<int> copy; copy = PQlist; cout << "\nobject copy testing with assignment AFTER declaration\n"; cout << "\nTesting getsize()\nExpected >> 8\n"; cout << "Actual >> " << copy.getSize() << endl; assert(copy.getSize() == 8); cout << "\nDequeue all the values in the PQ\n"; assert(copy.dequeue() == 7); assert(copy.dequeue() == 6); assert(copy.dequeue() == 5); assert(copy.dequeue() == 4); assert(copy.dequeue() == 3); assert(copy.dequeue() == 2); assert(copy.dequeue() == 1); assert(copy.dequeue() == 0); cout << "\nGetsize() after dequeue\n"; cout << "Expected >> 0\n"; cout << "Actual >> " << copy.getSize() << endl; } cout << "\nDequeue() - Empty the list\n"; for (int i = 0; i < 8; i++) PQlist.dequeue(); assert(PQlist.getSize() == 0); cout << "List is empty!\n"; }
int main() { // print my name and this assignment's title cout << "Lab 12b, The \"PriorityQueueBigOh.cpp\" Program \n"; cout << "Programmer: JEREMY THOMPSON\n"; cout << "Editor(s) used: JNotePad\n"; cout << "Compiler(s) used: VC++ 2013\n"; cout << "File: " << __FILE__ << endl; cout << "Complied: " << __DATE__ << " at " << __TIME__ << endl; int N = 8000000; const int REPS = (N / 100); // REPS for each cycle, less than 1% string bigOh = "O(log n)"; // YOUR PREDICTION: O(1), O(log n), O(n), O(n log n), or O(n squared) for (int iteration = 1; iteration < 3; iteration++) // 2 cycles per iteration { int n = N; int elapsedTimeTicksNorm = 0; double expectedTimeTicks = 0; if (iteration == 1) cout << "enqueue, O(log n)" << endl; if (iteration == 2) cout << "\ndequeue, O(log n)" << endl; for (int cycle = 0; cycle < 4; cycle++, n *= 2) // runs 4 cycles and doubles n each time { // Creation of test object PriorityQueue<double> pQueue; for (int i = n; i > 0; i--) pQueue.enqueue(i); // assigning of decreasing values assert(pQueue.getSize() == n); // assertion to confirm n is the size of the data struct PriorityQueue<double> pQueueCopy(pQueue); // default priority queue // START TIMER clock_t startTime = clock(); if (iteration == 1) for (int reps = 0; reps < REPS; reps++) pQueue.enqueue(n + reps); if (iteration == 2) for (int reps = 0; reps < REPS; reps++) pQueue.dequeue(); // STOP TIMER clock_t endTime = clock(); if (iteration == 1) assert(pQueue.getSize() == (n + REPS)); // assertion for size if (iteration == 1) assert(pQueue.getSize() == (n + REPS)); // compute timing results long elapsedTimeTicks = (long)(endTime - startTime); double factor = pow(2.0, cycle); if (cycle == 0) elapsedTimeTicksNorm = elapsedTimeTicks; else if (bigOh == "O(1)") expectedTimeTicks = elapsedTimeTicksNorm; else if (bigOh == "O(log n)") expectedTimeTicks = log(double(n)) / log(n / factor) * elapsedTimeTicksNorm; else if (bigOh == "O(n)") expectedTimeTicks = factor * elapsedTimeTicksNorm; else if (bigOh == "O(n log n)") expectedTimeTicks = factor * log(double(n)) / log(n / factor) * elapsedTimeTicksNorm; else if (bigOh == "O(n squared)") expectedTimeTicks = factor * factor * elapsedTimeTicksNorm; // reporting block cout << "\n " << elapsedTimeTicks; if (cycle == 0) cout << " (expected " << bigOh << ')'; else cout << " (expected " << expectedTimeTicks << ')'; cout << " for n=" << n; } } cout << "\nPress ENTER to continue..."; cin.get(); }
int main() { cout << "\n\nLab 12b, PriorityQueueBigOh.cpp\n"; cout << "Programmer: Aysin Oruz \n"; cout << "Editor(s) used: JNotePad and Xcode \n"; cout << "Compiler(s) used: Xcode and Terminal \n"; cout << "Description: The purpose of this lab is for you to learn how to create and apply 12b, PriorityQueueBigOh and get values with BigO().\n"; cout << "File: " << __FILE__ << endl; cout << "Compiled: " << __DATE__ << " at " << __TIME__ << endl; const int REPS = 100000; // for timing fast operations, use REPS up to 100th of the starting n int n = 10000000; // THE STARTING PROBLEM SIZE (MAX 250 MILLION) string bigOh = "O(log n)"; // YOUR PREDICTION: O(1), O(log n), O(n), O(n log n), or O(n squared) int elapsedTimeTicksNorm = 0; double expectedTimeTicks = 0; cout << "\nEnqueue() O(log n)\n" << endl; for (int cycle = 0; cycle < 4; cycle++, n*= 2) { //Declare PQ PriorityQueue<int> List; //Go thru loop to enter values for (int i = n; i > 0; i--) List.enqueue(i); clock_t startTime = clock(); // start the timer assert(List.getSize() == n); for(int j = 0; j < REPS; j++ ) List.enqueue(n + j); assert(List.getSize() == n + REPS); clock_t endTime = clock(); //stop time for (int i = 0; i < List.getSize(); i++) { int first = List.dequeue(); int second = List.dequeue(); assert(first >= second); } // compute timing results long elapsedTimeTicks = (long)(endTime - startTime); double factor = pow(2.0, cycle); if (cycle == 0) elapsedTimeTicksNorm = elapsedTimeTicks; else if (bigOh == "O(1)") expectedTimeTicks = elapsedTimeTicksNorm; else if (bigOh == "O(log n)") expectedTimeTicks = log(double(n)) / log(n / factor) * elapsedTimeTicksNorm; else if (bigOh == "O(n)") expectedTimeTicks = factor * elapsedTimeTicksNorm; else if (bigOh == "O(n log n)") expectedTimeTicks = factor * log(double(n)) / log(n / factor) * elapsedTimeTicksNorm; else if (bigOh == "O(n squared)") expectedTimeTicks = factor * factor * elapsedTimeTicksNorm; cout << elapsedTimeTicks;; if (cycle == 0) cout << " (expected " << bigOh << ')'; else cout << " (expected " << expectedTimeTicks << ')'; cout << " for n = " << n << endl; } { const int REPS = 10000; // for timing fast operations, use REPS up to 100th of the starting n int n = 1000000; // THE STARTING PROBLEM SIZE (MAX 250 MILLION) cout << "\nDequeue() O(log n)\n" << endl; for (int cycle = 0; cycle < 4; cycle++, n*= 2) { PriorityQueue<int> List; for(int i = n; i > 0; i--) List.enqueue(i); assert(List.getSize() == n); //start timing clock_t startTime = clock(); for(int j = 0; j < REPS; j++) List.dequeue(); clock_t endTime = clock(); //stop timign assert(List.getSize() == n - REPS); for (int i = 0; i < List.getSize(); i++) { int first = List.dequeue(); int second = List.dequeue(); assert(first >= second); } // compute timing results long elapsedTimeTicks = (long)(endTime - startTime); double factor = pow(2.0, cycle); if (cycle == 0) elapsedTimeTicksNorm = elapsedTimeTicks; else if (bigOh == "O(1)") expectedTimeTicks = elapsedTimeTicksNorm; else if (bigOh == "O(log n)") expectedTimeTicks = log(double(n)) / log(n / factor) * elapsedTimeTicksNorm; else if (bigOh == "O(n)") expectedTimeTicks = factor * elapsedTimeTicksNorm; else if (bigOh == "O(n log n)") expectedTimeTicks = factor * log(double(n)) / log(n / factor) * elapsedTimeTicksNorm; else if (bigOh == "O(n squared)") expectedTimeTicks = factor * factor * elapsedTimeTicksNorm; // reporting block cout << elapsedTimeTicks;; if (cycle == 0) cout << " (expected " << bigOh << ')'; else cout << " (expected " << expectedTimeTicks << ')'; cout << " for n = " << n << endl; }} }