Example #1
0
void testPQueue(PQueue& pq){
  for(int i = 9 ; i > 0; i--){
    pq.add(i);
    }
  //  pq.add(1);
  pq.removeAll();
}
Example #2
0
void Encoding::buildEncodingForInput(ibstream& infile){


	map<int, int> charMap;
	getCharCount(infile, charMap);
	PQueue<node*> pq;
	map<int, int>::iterator iter;
	for (iter = charMap.begin(); iter != charMap.end(); iter++){
		node* tempNode = new node;
		tempNode->letter = iter->first;
		tempNode->weight = iter->second;
		tempNode->leftChild = tempNode->rightChild = NULL;
		//cout << tempNode->letter << endl;
		pq.add(tempNode, iter->second);
	}
	map <int, string> encodingMap;
	while (pq.size() != 1){
		node *parent = new node;
		parent->letter = 26;

		parent->weight = 0; //probably don't need this
		parent->leftChild = pq.extractMin();
		parent->leftChild->path +='0';

		parent->rightChild = pq.extractMin();

		parent->rightChild->path += '1';


		parent->weight = parent->leftChild->weight+parent->rightChild->weight;
		pq.add(parent, parent->weight);
	}

	//for testing

	map<int, int>::iterator it;
	node* tree = pq.extractMin();
	for (it = charMap.begin(); it != charMap.end(); it++){
		BuildEncodingMap(tree, mp[it->first], it->first);
		string temps = mp[it->first];
		char tempc = it->first;
		//cout << tempc << " has the encoding " << temps << endl;
	}

	//PrintTree(tree);
}