/**************************************************************************************** * Program Name: lab3333_6_head.h * Author: Zhixiang Chen * Course: CSCI/CMPE 3333, Fall 2014 * Lab 6: Header file for Lab 6 * Date: 10/13/2014 * Description: The class template hashTableType is expanded to include a heapSort method *****************************************************************************************/ #include #include #include #include #include #include #include "arrayBasedList_head.h" using namespace std; #ifndef HS #define HS void generateData(ofstream & outFile, int S) { int num; srand(time(0)); for (int i=0; i class binaryHeapType { public: bool isEmpty ( ) const; bool isFull ( ); int getSize() {return size;} const Type & findMin ( ) const; const binaryHeapType & operator=(binaryHeapType &); void add(int, Type &); void insert ( const Type & x); void deleteMin ( ); // removes the minimum. void deleteMin ( Type & minItem); // removes the minimum and stores the removed value // in an object passed by reference. void makeEmpty ( ); void buildHeap ( ); void percolateDown ( int pos); // Internal method to percolate down in the heap. void percolateUp( int pos); // pos is the index of the percolating up process void print(); void loadData(ifstream &, int); //load data items from the input file void heapSort(ofstream &); //sort and save date items to the output file binaryHeapType(int cap = 100); binaryHeapType (binaryHeapType & rhs); private: int size; // Number of elements in heap arrayListType heapList; // The heap array }; //load data items from the input file template void binaryHeapType:: loadData(ifstream & inFile, int S) { Type x; int i=0; while(i>x; add(i+1, x); i++; } } template void binaryHeapType::heapSort(ofstream & outFile) { Type x; buildHeap(); while(!isEmpty()) { deleteMin(x); outFile< void binaryHeapType::print() { for (int i=1; i<=size; i++) cout<<"heapList["< void binaryHeapType::add(int i, Type & x) { size++; heapList.insert(i,x); } template bool binaryHeapType::isEmpty ( ) const { return size == 0; } template bool binaryHeapType::isFull ( ) { return size == heapList.getMaxSize(); } //pre-condition is that the heap is not empty template const Type & binaryHeapType::findMin ( ) const { return heapList[1]; } template const binaryHeapType & binaryHeapType::operator=( binaryHeapType & rhs) { if (this != & rhs) { size = rhs.size; heapList = rhs.heapList; } return *this; } template void binaryHeapType::insert ( const Type & x) { if (!isFull()) { size++; heapList.insert(size, x); percolateUp(size); } } template void binaryHeapType::deleteMin ( ) { percolateDown(1); } // removes the minimum and stores the removed value // in an object passed by reference. template void binaryHeapType:: deleteMin ( Type & minItem) { minItem = heapList[1]; heapList[1] = heapList[size]; size--; percolateDown(1); } template void binaryHeapType:: makeEmpty ( ) { size = 0; heapList.destroyheapList(); } template binaryHeapType:: binaryHeapType(int cap) { size = 0; heapList.initializeList(cap); } template binaryHeapType:: binaryHeapType(binaryHeapType & rhs) { if (this != & rhs) { size = rhs.size; heapList = rhs.heapList; } } // Establish a heap from an arbitrary arrangement of items. template void binaryHeapType::buildHeap ( ) { int i = size / 2; for (; i >= 1; i--) percolateDown(i); } // Internal method to percolate down in the heap. The starting position is i. template void binaryHeapType::percolateDown ( int i) { Type tmp, p; int left, right; if (i >= size) return; p = i; while (true) { left = 2 * p; right = left + 1; if (left > size) return; if (left == size) { if (heapList[left] void binaryHeapType::percolateUp ( int i) { Type tmp; int left, right, p, old; p = i; while (p > 1) { old = p; p = p/2; //old is the left child index if (old % 2 == 0) { if (heapList[p] > heapList[old]) { tmp = heapList[p]; heapList[p] = heapList[old]; heapList[old] = tmp; } } else //old is the right child index, i.e., old is odd. { if (heapList[p] > heapList[old]) { if (heapList[old] <= heapList[old - 1]) { tmp = heapList[p]; heapList[p] = heapList[old]; heapList[old] = tmp; } else { tmp = heapList[p]; heapList[p] = heapList[old - 1]; heapList[old - 1] = tmp; } } } } } #endif