Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to create a priority queue for Huffman nodes.
DSA C#
var priorityQueue = new PriorityQueue<HuffmanNode, int>(); priorityQueue.Enqueue(new HuffmanNode('a', 5), [1]);
Attempts:
3 left
💡 Hint
Common Mistakes
Using the character as priority instead of frequency.
Using a fixed number like 0 or 1 instead of the frequency.
✗ Incorrect
The priority queue uses the frequency as the priority, so we enqueue with the frequency value 5.
2fill in blank
mediumComplete the code to merge two Huffman nodes by summing their frequencies.
DSA C#
int mergedFreq = node1.Frequency [1] node2.Frequency;Attempts:
3 left
💡 Hint
Common Mistakes
Using subtraction or multiplication instead of addition.
Dividing frequencies which is incorrect.
✗ Incorrect
To merge two nodes, we add their frequencies to get the combined frequency.
3fill in blank
hardFix the error in the code that builds the Huffman tree by dequeuing two nodes.
DSA C#
var left = priorityQueue.[1]();
var right = priorityQueue.Dequeue();Attempts:
3 left
💡 Hint
Common Mistakes
Using Pop which is for stacks.
Using Remove or Extract which are not valid methods here.
✗ Incorrect
The correct method to remove and return the element with the highest priority is Dequeue.
4fill in blank
hardFill both blanks to correctly assign left and right children when creating a new Huffman node.
DSA C#
var mergedNode = new HuffmanNode(null, mergedFreq)
{
Left = [1],
Right = [2]
};Attempts:
3 left
💡 Hint
Common Mistakes
Swapping left and right nodes.
Assigning null or wrong variables.
✗ Incorrect
The merged node's left child is the left node dequeued, and the right child is the right node dequeued.
5fill in blank
hardFill all three blanks to build a dictionary comprehension that maps characters to their Huffman codes.
DSA C#
var huffmanCodes = nodes.ToDictionary(n => n.[1], n => n.[2].[3]());
Attempts:
3 left
💡 Hint
Common Mistakes
Using Frequency as key instead of Character.
Not converting code to string.
✗ Incorrect
We map each node's Character to its Code converted to string using ToString().
