void RosettaCodeProvider::buildHuffTree()
{
    //build Node list from HuffCodeTable, no need to sort (already done on get call)
    SortedList<HuffNode*> nodeList;
    int i = 0;
    HuffData* iterate = _huffCodeTable->get(i);
    while(iterate != nullptr){
        ++i;
        nodeList.addWithoutSort(new HuffNode(iterate));
        iterate = _huffCodeTable->get(i);
    }
    //merge nodes together
    while(nodeList.size() >= 2){
        HuffNode* one = nodeList.remove(0);
        HuffNode* two = nodeList.remove(0);
        unsigned sum = one->getFrequency() + two->getFrequency();
        HuffNode* three = new HuffNode(sum);
        if(*one < *two){
            three->_leftChild = one;
            three->_rightChild = two;
        }else{
            three->_leftChild = two;
            three->_rightChild = one;
        }
        nodeList.addWithSort(three);
    }
    if(nodeList.size() == 0){
        //something went wrong
        //nothing in codeTable to start with?
        return;
    }
    if(nodeList.size() == 1){
        _huffTree = new HuffTree(nodeList.remove(0));
    }
}
Example #2
0
int main()
{
	
	#define HELP "Usage: $ palindrome [parameter]\nWhere: parameter is one of:\n\t\t-h (help)\n\t\t-k (read from keyboard)\n\t\t-f file (read from file)\n\t\t-d letter (remove all palindromes which start by this letter)\n\t\t-s (show data)\n\t\t-q (exit)\n";
	
	string input ("\0");
	string original ("\0");
	string command;

	SortedList list;

	//always print the help at the begin
	cout << HELP;	

	do
	{
		//print a new prompt until -q
		cout << "$ palindrome ";

		//read command
		getline(cin, command, '\n');

		//create our own argv
		vector<string> myArgv;
		istringstream iss(command);
		string token;
		while(getline(iss, token, ' '))
		{
			myArgv.push_back(token);
		} 

		//create our own argc
		int myArgc = myArgv.size();

		//main method
		if (myArgc == 1)
		{
			if (myArgv[0] == "-h")
			{
				cout << HELP;
			}
			else if (myArgv[0] == "-s")
			{
				//show in the screen the number of queues in the list, the total number of palindromes in each queue and their starting letter
				cout << list.toString() << endl;
			}
			else if (myArgv[0] == "-k")
			{
				int i = 0;

				cout << "(-q to exit)" << endl;

				do
				{
					cout << "(" << i << ") "; i++;
					getline(cin, original, '\n');
					input = rmSpace(original);

					if (isPalindrome(input))
					{
						//insert original in the list
						list.insert(original);
						cout << "Palindrome inserted!" << endl;
					}
					else
						if (original!="-q")
							cout << "This is not palindrome, then not inserted!" << endl;
				} 
				while(original!="-q");
			}
			else
			{
				if (myArgv[0] != "-q")
				{
					cout << "Paremeter unknown " << myArgv[0] << endl;
				}
			}
		}
		else if (myArgc == 2)
		{
			if (myArgv[0] == "-f")
			{
				int cont0 = 0, cont1 = 0;

				ifstream inputFile (myArgv[1]);
				if(inputFile.is_open())
				{
					string tmp;
					while(getline(inputFile, tmp))
					{
						input = rmSpace(tmp);
						if (isPalindrome(input))
						{
							//insert tmp in the list
							list.insert(tmp);
							cont1++;
						}
						else
							cont0++;
					}
					cout << cont1 << " palindromes inserted." << endl;
					cout << cont0 << " sentences that are not palindrome, not inserted." << endl;
				}	
				else
				{
					//filename does not exists
					cout << "The file couldn't be opened, maybe doesn't exists :(\n";
				}
			}
			else if (myArgv[0] == "-d")
			{
				//delete the node of the leter passed in parameter
				if (myArgv[1].length() == 1)
					if(list.remove(myArgv[1].front()))
						cout << "Removed!" << endl;
					else
						cout<< "Node does not exists!" << endl;
				else
					cout << "just one char pleaaaaaase :)" << endl;
			}
			else
			{
				cout << "Paremeter unknown " << myArgv[0] << endl;
			}
		}
		else if (myArgc > 2)
		{
			cout << "There's way too much arguments :')\n";
		}
	}
	while(command != "-q");
}