Exemplo n.º 1
0
//need this to compare two BinaryTreePtr Objects
//bool BinaryTreePtr::operator<(const BinaryTreePtr& b) const
//{
//	return frequency < b.frequency;
//}
int main(int argc, char** argv)
{
	ifstream inf(argv[1]);
	char character;
	//BinaryTreePtr p;
	int ascii[256] = {};
	int temp[256];
	int counter = 0;
	BinaryHeap <BinaryTreePtr> heap; //not sure what size

	while (inf.get(character)) //reads each character as char
	{
		
		int num_char = (int)character; //casts char into int
		
		ascii[num_char]++; //increments array based on asci

	}

	for (int j = 0; j < 256; j++)
	{
		if (ascii[j] != 0)
		{
			
			int freq = ascii[j];
			heap.insert(BinaryTreePtr((char)j,ascii[j]));
		}
	}

	while(!heap.isEmpty())
	{
		BinaryTreePtr min_1;
		heap.deleteMin(min_1);
		//cout << (char)min_1.asci_value << min_1.frequency << endl;
		if (heap.isEmpty())
		{
			heap.insert(min_1);
			break;
		}
		else
		{
			
			BinaryTreePtr min_2;
			heap.deleteMin(min_2);
		//	cout << (char)min_2.asci_value << min_2.frequency << endl;
			BinaryTreePtr parent;
			int temp_freq = min_1.Node->getInfo() + min_2.Node->getInfo();
			char temp_char = 'Y';
			BinaryTreePtr Parent(temp_char, temp_freq, min_1.Node, min_2.Node);
			heap.insert(Parent);
		}
	}
	char* random = new char[500]();
	int i = 0;
	BinaryTreePtr last;
	heap.deleteMin(last);
	last.Node->print(random, i);
}
Exemplo n.º 2
0
int main(int argc, char **argv) {

    BinaryHeap<int> testHeap;
    std::string input;
    std::string numbers = "0123456789";
    
    std::cout << "Input vals to add to BinaryHeap (val1 val2 val3 val4 ... end)\n";

    while (std::cin >> input)
    {
	// if 'std::String::find_first_not_of' doesnt find anything, then it outputs max_int (long int)
	// since any num higher than 1 will be set to true, add 1 to max int to start at 0(false)
	 if (input.find_first_not_of(numbers)+1 ) {
		if (input == "end")
		{
		    break;
		}
		else 
		{
		    std::cerr << input << " not recognized\n";
		    continue;
		}
	     
	} // end if 	
	int num = std::atoi(input.c_str()); // transform string into number, would have normally use boost::lexical_cast though
	testHeap.insert(num);
    }
 
    std::cout << "Input vals to remove from BinaryHeap (val1 val2 val3 val4 ... end)\n";

    while (std::cin >> input)
    {
	// if 'std::String::find_first_not_of' doesnt find anything, then it outputs max_int (long int)
	    // since any num higher than 1 will be set to true, add 1 to max int to start at 0(false)
	 if (input.find_first_not_of(numbers)+1 ) {
		if (input == "end")
		    break;
		else 
		{
		    std::cerr << input << " not recognized\n";
		    continue;
		}
	     
	} // end if 	
	int num = std::atoi(input.c_str());
	testHeap.removeKey(num);
    }
    
    std::cerr << "\n";
    
    
    while (!testHeap.isEmpty())
    {
	std::cerr << testHeap.findMin() << " ";
	testHeap.deleteMin();
	
    }
    std::cerr << "\n";
        return 0;
}
Exemplo n.º 3
0
////////////////////////////////////////////////////////////////
//            FINDING MINIMUM UNKNOWN DISTANCE                //
////////////////////////////////////////////////////////////////
HeapObj Router::FindMinUnknown(BinaryHeap<HeapObj> &heap){

      while(vArray[(heap.findMin()).index]->known)
      {   //while the min vertex is unknown, we ignore it and keep popping
            heap.deleteMin();
      }      
      return heap.findMin();
}
void Encoder::generateHuffmanTree(){
  BinaryHeap<node<int> > heap;
  node<int> nodes[256];
  int numNodes = 0;
  for(int i = 0; i < 256; i++)
   if(freq[i] > 0 && i!= 10)
    cout << i << " " << freq[i] <<  endl;
  
  for(int i = 0; i < 256; i++){
    if(freq[i]>0 && i!=10){
      nodes[numNodes].probability = freq[i];
      nodes[numNodes].internal = 0;
      nodes[numNodes].value = i;
      heap.insert(nodes[numNodes]);
      numNodes++;
    }
  }
  // nodes disappear after this...
  for(int i = 0; i < numNodes; i++){
    cout << nodes[i].probability << " " << (char)nodes[i].value << endl;
  }

  node<int> lnode, rnode;
  node<int> parentNodes[300];
  
  int treeNumber = 0;
  
  while(heap.currentSize > 1) //while 2 or more elements in heap
  {
      heap.deleteMin(lnode);
      heap.deleteMin(rnode);

      cout << "Lnode = " << lnode.probability << " " << lnode.value <<  endl;
      cout << "Rnode = " << rnode.probability << " " << rnode.value <<  endl;

      parentNodes[treeNumber].internal = 1;
      parentNodes[treeNumber].probability = lnode.probability + rnode.probability;
      parentNodes[treeNumber].left = &lnode;
      parentNodes[treeNumber].right = &rnode;
      parentNodes[treeNumber].value = treeNumber;
      cout << "Tree " << treeNumber+1 << " has weight: " << lnode.probability + rnode.probability << endl;
      cout << "Left child: " << parentNodes[treeNumber].left->probability << " " << parentNodes[treeNumber].left->value << " Right child: " << parentNodes[treeNumber].right->probability << " " << parentNodes[treeNumber].right->value << endl << endl;
      heap.insert(parentNodes[treeNumber]);
      parentNodes[treeNumber].value = treeNumber++;
      
  }
  treeNumber--; // to take away last unneeded ++
  //parentNodes[treeNumber--].returnCodes(parentNodes[treeNumber--]);
  //returnCodes(&parentNodes[treeNumber--]);

  returnCodes(&parentNodes[0]);
  returnCodes(&parentNodes[1]);
  returnCodes(&parentNodes[2]);
  returnCodes(&parentNodes[3]);

}
int find_min_dist_id(BinaryHeap<HeapVt> & heap_vt, const vector<Vertex> & vt_path) {
    HeapVt tmp_vt(-1, -1);
    do{
        if(heap_vt.isEmpty()) {
            return -1;
        }
        heap_vt.deleteMin(tmp_vt);
    }while(true == vt_path[tmp_vt.id].known);
    return tmp_vt.id;
}
Exemplo n.º 6
0
int main() {

	BinaryHeap<int> H;
 

	cout << endl;

	vector<int> list = {30,17,20,15,10,12,5,7,8,5,2,9};

	for (int i : list)
	{
		H.insert(i);

	}

	H.printHeap();

	cout << " .....Inserting 1,10,6,4...." << endl;

	vector<int> list2 = {1,10,6,4};
	
	for (int i : list2)
	{
		H.insert(i);

	}

	H.printHeap();

	cout << " ......Performing 3 DeleteMin() ......" << endl;



	for (int i = 0 ; i<3; i++ )
	{
	H.deleteMin();
	}

	H.printHeap(); 

	cout << endl << H.HeapHeight();







//{30,17,20,15,10,12,5,7,8,5,2,9};



return 0;
}
Exemplo n.º 7
0
void Encoder::writeHeap(stringstream &out, BinaryHeap<HuffmanNode *> heap, int elements) const
{
  out << elements;	// store how many heap elements at beginning

  HuffmanNode *node;	// to store temporary node
  while(!heap.isEmpty())
  {
    heap.deleteMin(node);
    out << node->data;
    out << node->frequency;
  }  // traverse the entire heap

  return;
}  // writeHeap()