Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Can you go through this code and explain what is going on in each step? thank yo

ID: 3919127 • Letter: C

Question

Can you go through this code and explain what is going on in each step?

thank you.

//created ::

priority_queue<HuffNode*, vector<HuffNode*>, compare> minHeap;

// function to build the Huffman tree and store it in minHeap
void Utils:: HuffmanCodes(int size){
struct HuffNode *left, *right, *top;
for (map<char, int>::iterator v=freq.begin(); v!=freq.end(); v++)
minHeap.push(new HuffNode(v->first, v->second));
while (minHeap.size() != 1)
{
left = minHeap.top();
minHeap.pop();
right = minHeap.top();
minHeap.pop();
top = new HuffNode(NULL, left->freq + right->freq);
top->left = left;
top->right = right;
minHeap.push(top);
}
storeCodes(minHeap.top(),"");
}

Explanation / Answer

priority_queue<HuffNode*, vector<HuffNode*>, compare> minHeap; // taking one priority queue.

// function to build the Huffman tree and store it in minHeap
void Utils:: HuffmanCodes(int size){ // function taking int size
struct HuffNode *left, *right, *top; // not of tree which we can use to set elements
for (map<char, int>::iterator v=freq.begin(); v!=freq.end(); v++) //we are taking elements from map and put into priority queue.
minHeap.push(new HuffNode(v->first, v->second)); // pusing element in queue
while (minHeap.size() != 1) // if size if not one itereate the loop
{
left = minHeap.top(); // now start from first element and give it to left
minHeap.pop(); // pop it from the minHeap
right = minHeap.top(); // take next element and give it to right
minHeap.pop(); // pop it from the minHeap
top = new HuffNode(NULL, left->freq + right->freq); // giv node size to top
top->left = left; // now put left in to top left
top->right = right; // put right to top right
minHeap.push(top); //and push to last in minHiap
}
storeCodes(minHeap.top(),""); // this will save code some where
} // end of the code