Exemplo n.º 1
0
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;
}
Exemplo n.º 2
0
int main()
{
	PriorityQueue pqueue;

	int f = 0;
	int key, i;

	cout << "1.insert a element" << endl;
	cout << "2.return the max" << endl;
	cout << "3.return and delete the max"<< endl;
	cout << "4.return the length" << endl;
	cout << "5.increase i element to key" << endl;
	cout << "6.decrease i element to key" << endl;
	cout << "7.exit" << endl;

	while(f != 7)
	{
		cin >> f;
		switch(f)
		{
		case 1:
			cout<<"input a value:";
			cin >> key;
			pqueue.Insert(key);
			break;
		case 2:
			cout <<"The max value is:" << pqueue.Maximum() << endl;
			break;
		case 3:
			cout << "The max and deleted is:" << pqueue.ExtractMax() << endl;
			break;
		case 4:
			cout << "The length is:" << pqueue.Length() << endl;
			break;
		case 5:
			cout << "input the i and key:";
			cin >> i >> key;
			pqueue.IncreaseKey(i,key);
			break;
		case 6:
			cout << "input the i and key:";
			cin >> i >> key;
			pqueue.DecreaseKey(i,key);
			break;
		default:
			break;
		}
		cout << endl;
	}

	return 0;
}
Exemplo n.º 3
0
void Prim::mst(int root,Color color){
	Q.clear();
	int size=G->V();  // number of vertices
	for(int i=0;i<size;i++){
		dist[i]=1000.0;
		edges[i]=NO_EDGE;
		if(G->get_node_value(i)==color){  // only choose the same color
		    	//dist[i]=INF;    // equal to infinite
			//cout<<"insert node!"<<i<<" and dist is "<<dist[i]<<endl;
		Q.Insert(Type_Queue_Element (i,dist[i]));  // assign V[G] to Q
		}
	}
	dist[root]=0.0;
	if(!Q.contains(root)) cout<<"not include root !!"<<endl;
	else {
		Q.chgPrioirity(root,dist[root]);   //dist[i] and priority value in priority queue must be synchronized
		edges[root]=ROOT_EDGE;
		while(!Q.empty()){
			Type_Queue_Element currElement=Q.top();
            Q.minPrioirty();  // remove from priority queue
			int currNode=currElement.first;
			if(edges[currNode]!=NO_EDGE){
				dist[currNode]=currElement.second;
				vector<int> neibs=G->neighbors(currNode,color);
				for(unsigned int i=0;i<neibs.size();i++){
					if(Q.contains(neibs[i]) && (G->get_edge_value(neibs[i],currNode)<dist[neibs[i]]) ){
						edges[neibs[i]]=currNode;
						dist[neibs[i]]=G->get_edge_value(neibs[i],currNode);
						Q.chgPrioirity(neibs[i],dist[neibs[i]]);
					}
				}
			}

		}
	}
}