Skip to content

Commit 7bc85ec

Browse files
authored
Create main.cpp
1 parent bd3cd02 commit 7bc85ec

File tree

1 file changed

+88
-0
lines changed
  • 25 - Greedy Algorithm Problems/08 - Huffman Encoding

1 file changed

+88
-0
lines changed
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
// Define a Node class to represent each node in the Huffman tree
2+
class Node {
3+
public:
4+
int data; // The frequency of the character (or combined frequencies)
5+
Node* right; // Pointer to the right child node
6+
Node* left; // Pointer to the left child node
7+
8+
// Constructor to initialize a node with a given frequency
9+
Node(int d) {
10+
this -> data = d; // Set the frequency (data) of the node
11+
left = NULL; // Initialize the left child as NULL
12+
right = NULL; // Initialize the right child as NULL
13+
}
14+
};
15+
16+
// Comparator class for the priority queue to create a min-heap
17+
class cmp {
18+
public:
19+
bool operator()(Node* a, Node* b) {
20+
// Return true if the frequency of 'a' is greater than 'b',
21+
// which helps in maintaining a min-heap (lowest frequency at top)
22+
return a -> data > b -> data;
23+
}
24+
};
25+
26+
// Solution class that contains the logic for building Huffman codes
27+
class Solution {
28+
public:
29+
// Helper function to traverse the Huffman tree and store codes
30+
void traverse(Node* root, vector<string>& ans, string temp) {
31+
// Base case: If it's a leaf node, add the generated code to the answer
32+
if(root -> left == NULL && root -> right == NULL) {
33+
ans.push_back(temp);
34+
return;
35+
}
36+
37+
// Recursively traverse the left subtree and append '0' to the code
38+
traverse(root -> left, ans, temp + "0");
39+
40+
// Recursively traverse the right subtree and append '1' to the code
41+
traverse(root -> right, ans, temp + "1");
42+
}
43+
44+
// Main function to build Huffman codes
45+
vector<string> huffmanCodes(string S, vector<int> f, int N) {
46+
// Priority queue to store nodes of the Huffman tree; min-heap based on frequency
47+
priority_queue<Node*, vector<Node*>, cmp> pq;
48+
49+
// Step 1: Insert all the nodes into the priority queue (based on frequency)
50+
for(int i = 0; i < N; i++) {
51+
Node* temp = new Node(f[i]); // Create a new node with frequency f[i]
52+
pq.push(temp); // Push the node into the priority queue
53+
}
54+
55+
// Step 2: Build the Huffman tree by combining the two nodes with the smallest frequencies
56+
while(pq.size() > 1) {
57+
// Extract the two nodes with the smallest frequencies
58+
Node* left = pq.top();
59+
pq.pop();
60+
61+
Node* right = pq.top();
62+
pq.pop();
63+
64+
// Create a new internal node with a frequency equal to the sum of the two nodes' frequencies
65+
Node* newNode = new Node(left -> data + right -> data);
66+
67+
// Set the left and right children of the new node
68+
newNode -> left = left;
69+
newNode -> right = right;
70+
71+
// Push the new node back into the priority queue
72+
pq.push(newNode);
73+
}
74+
75+
// The final node in the priority queue is the root of the Huffman tree
76+
Node* root = pq.top();
77+
78+
// Step 3: Traverse the Huffman tree to generate the Huffman codes
79+
vector<string> ans; // This will store the final Huffman codes
80+
string temp = ""; // Temporary string to build the code for each character
81+
82+
// Call the helper function to traverse the tree and build the codes
83+
traverse(root, ans, temp);
84+
85+
// Return the final Huffman codes
86+
return ans;
87+
}
88+
};

0 commit comments

Comments
 (0)