コード例 #1
0
int HuffmanTree::traverseAndPrint(string &bits, int i, BSTNode *cur)
{

  // **************************************************************
  // TODO: Write this function using recursion.
  //  This is essentially your decode function.  You need to step through the tree based on reading 0 or 1 and when you
	//      reach a leaf, print (using cout) the appropriate character.
	// i represents your current location in the string
	// cur represents the cur node in your tree
	// Don't forget that you need to keep going after printing out a character (which means restarting at th top of the tree       

  
  if(i >= (int)bits.length()){
	return -1;
  }
  
  if (cur->right != nullptr || cur->left != nullptr){
	if(bits[i] == '0' && cur->left != nullptr){
	  i++;
	 return traverseAndPrint(bits, i, cur->left);
	}
	
	else{
	  i++;
	  return traverseAndPrint(bits, i, cur->right);
	}
  }
  else{
	cout << cur->data.letter;
	return i;
  }

	// **************************************************************
}
コード例 #2
0
bool HuffmanTree::decodeFile(string filename)
{
	ifstream encodedfile;
	encodedfile.open(filename.c_str(), ios::binary);
	if( !encodedfile.is_open() )
	{
		cout << "ERROR opening encoded file!\n";
		return false;
	}
	
	encodedfile >> std::noskipws;	
	
	string bitStream = "";
	while(!encodedfile.eof())
	{
		unsigned char readMe;
		encodedfile >> readMe;
		bitset<8> set((unsigned long)readMe);
		bitStream += set.to_string();
	}
	
	encodedfile.close();
	
	int i=0;
	while(i != -1)
	{
		i = traverseAndPrint(bitStream, i);
	}
	return false;
}
コード例 #3
0
int main(void)
{
	int i;
	node *headNode;
	node *test1, *test2, *test3, *test4;
	printf("hello, world.\n");
	
	headNode = createNode(-1);
	for(i=0 ; i<COUNT ; i++)
	{
		addNodeToTail(headNode, createNode(i));
	}
	traverseAndPrint(headNode);	
	return 0;
}