Huffman Tree Code Generator

So here is my problem I am trying to accomplish:

I have a method: string* generateCodewords() of class HuffmanTree.
This method should return an array with 256 elements containing codewords for the characters in the considered text.
I want my method to use recursion. For example:

Assume that an array returned is called "codewords" (string* codewords = tree->generateCodewords();)

- ASCII code for 'a' is 97 so, if codewords[97] is equal to "101" it means that the codeword for character 'a' is "101" (3bits).

I will post my functions and class definitions in a comment below. If someone could help me out That would be fantastic.

Thanks
Austin
Constructor:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
HuffmanTree::HuffmanTree(unsigned int* frequencies) {
	root = NULL;

	SortedArrayList<BTNode<InfoNode> > list;
	for (int i = 0; i < 256; i++) {
		if (frequencies[i] != 0) {
			InfoNode* newInfoNode = new InfoNode(i, frequencies[i]);
			BTNode<InfoNode>* newNode = new BTNode<InfoNode>(newInfoNode);
			list.add(newNode);
		}
	}
	BTNode<InfoNode>* p,* q;
	int size = list.size();
	for (int i = 0; i < size - 1; i++) {
		p = list.get(1);
		q = list.get(2);
		list.remove(1);
		list.remove(1);
		InfoNode* newInfoNode = new InfoNode(p->getElement()->getFrequency() + q->getElement()->getFrequency());
		BTNode<InfoNode>* newNode = new BTNode<InfoNode>(newInfoNode);
		newNode->setElement(newInfoNode);
		list.add(newNode);
		newNode->setLeftChild(p);
		newNode->setRightChild(q);

	}
	root = list.get(1);
	list.remove(1);
}

class Definition:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class HuffmanTree {
private:
	BTNode<InfoNode>* root;

	void destroy(BTNode<InfoNode>* node);
public:
	/**
	 * Builds Huffman Tree on the basis of the number of occurrences of characters.
	 */
	HuffmanTree(unsigned int* frequencies);
	~HuffmanTree();
	BTNode<InfoNode>* getRoot();
	void setRoot(BTNode<InfoNode>* root);
	string* generateCodewords();
};


Any other information you may need I will post. Thanks!
Topic archived. No new replies allowed.