Пример #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);
}
Пример #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;
}
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;
}
Пример #4
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()