0
0
DSA Cprogramming~10 mins

Huffman Encoding in DSA C - Interactive Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete 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'
Adata
Bfreq
C'\0'
DNULL
Attempts:
3 left
💡 Hint
Common Mistakes
Assigning frequency to data field.
Assigning NULL or zero character instead of actual data.
2fill in blank
medium

Complete 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'
A<
B>
C!=
D==
Attempts:
3 left
💡 Hint
Common Mistakes
Using '!=' instead of '==' causing wrong leaf detection.
Using relational operators like '>' or '<' which are invalid for pointers.
3fill in blank
hard

Fix 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'
Ai / 2
B(i - 1) / 2
Ci - 1
Di + 1
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.
4fill in blank
hard

Fill 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'
A'\0'
B'*'
C'$'
D' '
Attempts:
3 left
💡 Hint
Common Mistakes
Using actual characters from input instead of a special marker.
Using null character '\0' which may cause confusion.
5fill in blank
hard

Fill 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'
A0
B1
C2
D-1
Attempts:
3 left
💡 Hint
Common Mistakes
Using 2 or -1 which are invalid for binary codes.
Swapping 0 and 1 causing incorrect codes.