Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to create a new node for the Huffman tree.
DSA C
struct Node* createNode(char data, unsigned freq) {
struct Node* temp = (struct Node*)malloc(sizeof(struct Node));
temp->data = [1];
temp->freq = freq;
temp->left = temp->right = NULL;
return temp;
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Assigning frequency to data field.
Assigning NULL or zero character instead of actual data.
✗ Incorrect
The node's data field should be set to the character passed as 'data'.
2fill in blank
mediumComplete the code to check if a node is a leaf node in the Huffman tree.
DSA C
int isLeaf(struct Node* root) {
return (root->left == NULL && root->right [1] NULL);
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using '!=' instead of '==' causing wrong leaf detection.
Using relational operators like '>' or '<' which are invalid for pointers.
✗ Incorrect
A leaf node has both left and right pointers equal to NULL.
3fill in blank
hardFix the error in the code that inserts a node into the min heap.
DSA C
void insertMinHeap(struct MinHeap* minHeap, struct Node* node) {
++minHeap->size;
int i = minHeap->size - 1;
while (i && node->freq < minHeap->array[(i - 1) / 2]->freq) {
minHeap->array[i] = minHeap->array[(i - 1) / 2];
i = [1];
}
minHeap->array[i] = node;
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using i - 1 moves one step left, not to parent.
Using i / 2 is incorrect for zero-based indexing.
Using i + 1 moves down, not up.
✗ Incorrect
To move up the heap, update i to the parent's index: (i - 1) / 2.
4fill in blank
hardFill both blanks to build the Huffman tree by extracting two minimum nodes and combining them.
DSA C
while (minHeap->size != 1) { struct Node* left = extractMin(minHeap); struct Node* right = extractMin(minHeap); struct Node* top = createNode('[1]', left->freq + right->freq); top->left = left; top->right = right; insertMinHeap(minHeap, top); }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using actual characters from input instead of a special marker.
Using null character '\0' which may cause confusion.
✗ Incorrect
The internal nodes use a special character like '$' to indicate they are not leaves.
5fill in blank
hardFill the blanks to print the Huffman codes by traversing the tree recursively.
DSA C
void printCodes(struct Node* root, int arr[], int top) {
if (root->left) {
arr[top] = [1];
printCodes(root->left, arr, top + 1);
}
if (root->right) {
arr[top] = [2];
printCodes(root->right, arr, top + 1);
}
if (isLeaf(root)) {
printf("%c: ", root->data);
for (int i = 0; i < top; ++i) {
printf("%d", arr[i]);
}
printf("\n");
}
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 2 or -1 which are invalid for binary codes.
Swapping 0 and 1 causing incorrect codes.
✗ Incorrect
Left edges are marked with 0, right edges with 1. The option '2' is a distractor and not used here.