Huffman coding using priority queues

Why is it suggested to use 2 priority queues instead of 1 to implement Huffman coding? I'm just trying to understand how it aids computational complexity. For instance wouldn't it be O(n) even if 1 priority queue is used?

I'm also attaching the program I wrote using smart pointers and 2 priority queues to generate Huffman codes
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
#include <iostream>
#include <tuple>
#include <vector> 
#include <queue>
#include <functional>
#include <string>
#include <memory>

using namespace std;

class binarySearchTree
{
	public:

		struct treeNode
		{
			char data;
			shared_ptr<treeNode> left, right;
		};

		shared_ptr<treeNode> root;

		void setRoot(char a)
		{
			root = make_shared<treeNode>();
			(*root).data = a;
		}

		void mergeSubTrees(const shared_ptr<binarySearchTree>& nodeA, const shared_ptr<binarySearchTree>& nodeB)
		{
			root = make_shared<treeNode>();
			(*root).left = (*nodeA).root; (*root).right = (*nodeB).root;
		}

		void inOrder(shared_ptr<treeNode> tNodeCurr, vector<tuple<char, string>>& huffmanCodes, string& tempStr)
		{

			if (tNodeCurr)
			{
				if ((*tNodeCurr).data) huffmanCodes.push_back(make_tuple((*tNodeCurr).data, tempStr));
				if ((*tNodeCurr).left)
				{
					tempStr += "0";
					inOrder((*tNodeCurr).left, huffmanCodes, tempStr);
					tempStr.pop_back();
				}				
				if ((*tNodeCurr).right)
				{
					tempStr += "1";
					inOrder((*tNodeCurr).right, huffmanCodes, tempStr);
					tempStr.pop_back();
				}
			}
			else return;
		}

};

vector<tuple<char, string>> generateHuffmanCodes(const vector<tuple<int, char>>& characterList)
{
	vector<tuple<char, string>> huffmanCodes;

	if (characterList.size() > 1)
	{
		int i, j, queueChoice;
		string tempStr;

		priority_queue<tuple<int, shared_ptr<binarySearchTree>>, vector<tuple<int, shared_ptr<binarySearchTree>>>, greater<tuple<int, shared_ptr<binarySearchTree>>> > characterListPQ1, characterListPQ2;
		shared_ptr<binarySearchTree> binarySearchTreePtr1 = make_shared<binarySearchTree>(), binarySearchTreePtr2;
		
		auto characterListIterator = characterList.cbegin();
		for (; characterListIterator != characterList.end(); ++characterListIterator)
		{
			(*binarySearchTreePtr1).setRoot(get <1>(*characterListIterator)); 
			characterListPQ1.push(make_tuple(get <0>(*characterListIterator), make_shared<binarySearchTree>(*binarySearchTreePtr1)));
		}
		 
		binarySearchTreePtr1 = NULL;

		while (characterListPQ1.size() + characterListPQ2.size() > 1)
		{
			if (characterListPQ1.size() == 0)
			{ 
				i = get <0>(characterListPQ2.top());
				binarySearchTreePtr1 = get <1>(characterListPQ2.top());
				characterListPQ2.pop();
				binarySearchTreePtr2 = get <1>(characterListPQ2.top());
				shared_ptr<binarySearchTree> binaryMergedSearchTreePtr = make_shared<binarySearchTree>();
				(*binaryMergedSearchTreePtr).mergeSubTrees(binarySearchTreePtr1, binarySearchTreePtr2);
				characterListPQ1.push(make_tuple(i + get <0>(characterListPQ2.top()), binaryMergedSearchTreePtr));
				characterListPQ2.pop();
			}
			else if (characterListPQ2.size() == 0)
			{ 
				i = get <0>(characterListPQ1.top());
				binarySearchTreePtr1 = get <1>(characterListPQ1.top());
				characterListPQ1.pop();
				binarySearchTreePtr2 = get <1>(characterListPQ1.top());					
				shared_ptr<binarySearchTree> binaryMergedSearchTreePtr = make_shared<binarySearchTree>();
				(*binaryMergedSearchTreePtr).mergeSubTrees(binarySearchTreePtr1, binarySearchTreePtr2);
				characterListPQ2.push(make_tuple(i + get <0>(characterListPQ1.top()), binaryMergedSearchTreePtr));
				characterListPQ1.pop();
			}
			else
			{ 
				if (get <0>(characterListPQ1.top()) < get <0>(characterListPQ2.top()))
				{
					i = get <0>(characterListPQ1.top());
					binarySearchTreePtr1 = get <1>(characterListPQ1.top());
					characterListPQ1.pop();
					
				}
				else
				{
					i = get <0>(characterListPQ2.top());
					binarySearchTreePtr1 = get <1>(characterListPQ2.top());
					characterListPQ2.pop();
				}
				
				if (get <0>(characterListPQ1.top()) < get <0>(characterListPQ2.top()))
				{
					j = get <0>(characterListPQ1.top());
					binarySearchTreePtr2 = get <1>(characterListPQ1.top());
					characterListPQ1.pop();
					queueChoice = 1;
				}
				else
				{
					j = get <0>(characterListPQ2.top());
					binarySearchTreePtr2 = get <1>(characterListPQ2.top());
					characterListPQ2.pop();
					queueChoice = 2;
				}

				shared_ptr<binarySearchTree> binaryMergedSearchTreePtr = make_shared<binarySearchTree>();
				(*binaryMergedSearchTreePtr).mergeSubTrees(binarySearchTreePtr1, binarySearchTreePtr2);
				
				if (queueChoice == 1) characterListPQ2.push(make_tuple(i + j, binaryMergedSearchTreePtr));
				else characterListPQ2.push(make_tuple(i + j, binaryMergedSearchTreePtr));
				
			}

		}

		if (characterListPQ1.size() == 1) binarySearchTreePtr1 = get <1>(characterListPQ1.top());
		else binarySearchTreePtr1 = get <1>(characterListPQ2.top());

		(*binarySearchTreePtr1).inOrder((*binarySearchTreePtr1).root, huffmanCodes, tempStr);
	}

	return huffmanCodes;
}

int main()
{
	vector<tuple<int, char>> characterList(6);
	characterList[0] = make_tuple(3, 'A');
	characterList[1] = make_tuple(2, 'B');
	characterList[2] = make_tuple(6, 'C');
	characterList[3] = make_tuple(8, 'D');
	characterList[4] = make_tuple(2, 'E');
	characterList[5] = make_tuple(6, 'F');
	sort(characterList.begin(), characterList.end(), less<tuple<int, char>>());
	
	auto huffmanCodes = generateHuffmanCodes(characterList);
	
	return 0;
}
Last edited on
Topic archived. No new replies allowed.