hello. This is my code:  #include #include #include typedef struct HuffmanNode {     int character;     float frequency;     struct HuffmanNode* left;     struct HuffmanNode* right; } HuffmanNode; typedef struct HeapNode {     HuffmanNode* node;     struct HeapNode* next; } HeapNode; HuffmanNode* create_huffman_node(int character, float frequency) {     HuffmanNode* node = (HuffmanNode*)malloc(sizeof(HuffmanNode));     node->character = character;     node->frequency = frequency;     node->left = NULL;     node->right = NULL;     return node; } HeapNode* create_heap_node(HuffmanNode* node) {     HeapNode* heap_node = (HeapNode*)malloc(sizeof(HeapNode));     heap_node->node = node;     heap_node->next = NULL;     return heap_node; } HeapNode* insert_into_heap(HeapNode* heap, HuffmanNode* node) {     HeapNode* heap_node = create_heap_node(node);     if (heap == NULL) {         return heap_node;     }     // Compare nodes based on probability and character value     if (heap_node->node->frequency < heap->node->frequency ||         (heap_node->node->frequency == heap->node->frequency && heap_node->node->character < heap->node->character)) {         heap_node->next = heap;         return heap_node;     }     HeapNode* current = heap;     while (current->next != NULL && (current->next->node->frequency < heap_node->node->frequency ||                                      (current->next->node->frequency == heap_node->node->frequency &&                                       current->next->node->character < heap_node->node->character))) {         current = current->next;     }     heap_node->next = current->next;     current->next = heap_node;     return heap; } HuffmanNode* build_huffman_tree(float* count) {     HuffmanNode* nodes[128] = {NULL}; // Array to store nodes based on ASCII value     for (int i = 0; i < 128; i++) {         if (count[i] > 0) {             HuffmanNode* node = create_huffman_node(i, count[i]);             nodes[i] = node;         }     }     HeapNode* heap = NULL;     // Insert nodes into the heap     for (int i = 0; i < 128; i++) {         if (nodes[i] != NULL) {             heap = insert_into_heap(heap, nodes[i]);         }     }     while (heap->next != NULL) {         HuffmanNode* node1 = heap->node;         heap = heap->next;         HuffmanNode* node2 = heap->node;         heap = heap->next;         HuffmanNode* new_node = create_huffman_node(-1, node1->frequency + node2->frequency);         new_node->left = node1;         new_node->right = node2;         heap = insert_into_heap(heap, new_node);     }     return heap->node; } void print_huffman_codes(HuffmanNode* node, int* code, int code_length) {     if (node == NULL) {         return;     }     code[code_length] = 0;     print_huffman_codes(node->left, code, code_length + 1);     if (node->character != -1) {         printf("| %-5d | %.5f | ", node->character, node->frequency / 100);         for (int i = 0; i < code_length; i++) {             printf("%d", code[i]);         }         printf(" |\n");     }     code[code_length] = 1;     print_huffman_codes(node->right, code, code_length + 1); } void huffman_encoding(const char* filename) {     float count[128] = {0};     FILE* file = fopen(filename, "r");     if (file == NULL) {         printf("File not found.\n");         return;     }     int c;     while ((c = fgetc(file)) != EOF) {         if (c >= 0 && c < 128) {             count[c]++;         }     }     fclose(file);     HuffmanNode* root = build_huffman_tree(count);     printf("| ASCII | Percent | Code |\n");     printf("|-------|---------|------|\n");     int code[128] = {0};     print_huffman_codes(root, code, 0);     free(root); } int main() {     char filename[256];     printf("Enter File Name to read: ");     scanf("%s", filename);     huffman_encoding(filename);     return 0; }   It is not in the same format as the expected output and I don't know how to fix it. please help

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

hello. This is my code: 

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef struct HuffmanNode {
    int character;
    float frequency;
    struct HuffmanNode* left;
    struct HuffmanNode* right;
} HuffmanNode;

typedef struct HeapNode {
    HuffmanNode* node;
    struct HeapNode* next;
} HeapNode;

HuffmanNode* create_huffman_node(int character, float frequency) {
    HuffmanNode* node = (HuffmanNode*)malloc(sizeof(HuffmanNode));
    node->character = character;
    node->frequency = frequency;
    node->left = NULL;
    node->right = NULL;
    return node;
}

HeapNode* create_heap_node(HuffmanNode* node) {
    HeapNode* heap_node = (HeapNode*)malloc(sizeof(HeapNode));
    heap_node->node = node;
    heap_node->next = NULL;
    return heap_node;
}

HeapNode* insert_into_heap(HeapNode* heap, HuffmanNode* node) {
    HeapNode* heap_node = create_heap_node(node);

    if (heap == NULL) {
        return heap_node;
    }

    // Compare nodes based on probability and character value
    if (heap_node->node->frequency < heap->node->frequency ||
        (heap_node->node->frequency == heap->node->frequency && heap_node->node->character < heap->node->character)) {
        heap_node->next = heap;
        return heap_node;
    }

    HeapNode* current = heap;
    while (current->next != NULL && (current->next->node->frequency < heap_node->node->frequency ||
                                     (current->next->node->frequency == heap_node->node->frequency &&
                                      current->next->node->character < heap_node->node->character))) {
        current = current->next;
    }

    heap_node->next = current->next;
    current->next = heap_node;

    return heap;
}

HuffmanNode* build_huffman_tree(float* count) {
    HuffmanNode* nodes[128] = {NULL}; // Array to store nodes based on ASCII value

    for (int i = 0; i < 128; i++) {
        if (count[i] > 0) {
            HuffmanNode* node = create_huffman_node(i, count[i]);
            nodes[i] = node;
        }
    }

    HeapNode* heap = NULL;

    // Insert nodes into the heap
    for (int i = 0; i < 128; i++) {
        if (nodes[i] != NULL) {
            heap = insert_into_heap(heap, nodes[i]);
        }
    }

    while (heap->next != NULL) {
        HuffmanNode* node1 = heap->node;
        heap = heap->next;
        HuffmanNode* node2 = heap->node;
        heap = heap->next;

        HuffmanNode* new_node = create_huffman_node(-1, node1->frequency + node2->frequency);
        new_node->left = node1;
        new_node->right = node2;

        heap = insert_into_heap(heap, new_node);
    }

    return heap->node;
}

void print_huffman_codes(HuffmanNode* node, int* code, int code_length) {
    if (node == NULL) {
        return;
    }

    code[code_length] = 0;
    print_huffman_codes(node->left, code, code_length + 1);

    if (node->character != -1) {
        printf("| %-5d | %.5f | ", node->character, node->frequency / 100);
        for (int i = 0; i < code_length; i++) {
            printf("%d", code[i]);
        }
        printf(" |\n");
    }

    code[code_length] = 1;
    print_huffman_codes(node->right, code, code_length + 1);
}

void huffman_encoding(const char* filename) {
    float count[128] = {0};

    FILE* file = fopen(filename, "r");
    if (file == NULL) {
        printf("File not found.\n");
        return;
    }

    int c;
    while ((c = fgetc(file)) != EOF) {
        if (c >= 0 && c < 128) {
            count[c]++;
        }
    }

    fclose(file);

    HuffmanNode* root = build_huffman_tree(count);

    printf("| ASCII | Percent | Code |\n");
    printf("|-------|---------|------|\n");

    int code[128] = {0};
    print_huffman_codes(root, code, 0);

    free(root);
}

int main() {
    char filename[256];
    printf("Enter File Name to read: ");
    scanf("%s", filename);

    huffman_encoding(filename);

    return 0;
}

 

It is not in the same format as the expected output and I don't know how to fix it. please help

 

### Huffman Coding Simulation: Expected vs. Actual Output

#### Expected Output
The following table represents the expected output for a Huffman coding simulation. Each row displays the ASCII value, the percentage frequency of each character, and the corresponding Huffman code.

```
Enter File Name to read:
------------------------------
| ASCII | Percent | Code |
------------------------------
|   97  | 0.12000 | 1111 |
|   98  | 0.40000 | 0    |
|   99  | 0.15000 | 110  |
|   100 | 0.08000 | 1110 |
|   101 | 0.25000 | 10   |
------------------------------
```

**Explanation of Columns:**
- **ASCII:** The ASCII value of the character.
- **Percent:** The frequency of the character represented as a percentage of the total input.
- **Code:** The Huffman code assigned to the character.

#### Actual Output
The following table shows the actual output generated by the Huffman coding simulation. Each row displays the ASCII value, the percentage frequency of each character, and the corresponding Huffman code.

```
Enter File Name to read:
------------------------------
| ASCII | Percent | Code |
------------------------------
|   98  | 0.40000 | 0    |
|   101 | 0.25000 | 10    |
|   99  | 0.15000 | 110  |
|   100 | 0.08000 | 1110 |
|   97  | 0.12000 | 1111 |
------------------------------
```

**Graphical Explanation:**
Both tables feature similar data but are ordered differently. Each table includes the following columns:
- **ASCII** represents the ASCII value of each character in the file.
- **Percent** shows the frequency of occurrence for each character.
- **Code** displays the Huffman code assigned based on the character's frequency.

In the expected output, the characters are sorted by their ASCII values, while in the actual output, they appear to be sorted by their frequencies in descending order.

This comparison reveals a mismatch in the ordering criteria applied to the character frequencies and their corresponding Huffman codes. While the content and values are accurate, the presentation differs.
Transcribed Image Text:### Huffman Coding Simulation: Expected vs. Actual Output #### Expected Output The following table represents the expected output for a Huffman coding simulation. Each row displays the ASCII value, the percentage frequency of each character, and the corresponding Huffman code. ``` Enter File Name to read: ------------------------------ | ASCII | Percent | Code | ------------------------------ | 97 | 0.12000 | 1111 | | 98 | 0.40000 | 0 | | 99 | 0.15000 | 110 | | 100 | 0.08000 | 1110 | | 101 | 0.25000 | 10 | ------------------------------ ``` **Explanation of Columns:** - **ASCII:** The ASCII value of the character. - **Percent:** The frequency of the character represented as a percentage of the total input. - **Code:** The Huffman code assigned to the character. #### Actual Output The following table shows the actual output generated by the Huffman coding simulation. Each row displays the ASCII value, the percentage frequency of each character, and the corresponding Huffman code. ``` Enter File Name to read: ------------------------------ | ASCII | Percent | Code | ------------------------------ | 98 | 0.40000 | 0 | | 101 | 0.25000 | 10 | | 99 | 0.15000 | 110 | | 100 | 0.08000 | 1110 | | 97 | 0.12000 | 1111 | ------------------------------ ``` **Graphical Explanation:** Both tables feature similar data but are ordered differently. Each table includes the following columns: - **ASCII** represents the ASCII value of each character in the file. - **Percent** shows the frequency of occurrence for each character. - **Code** displays the Huffman code assigned based on the character's frequency. In the expected output, the characters are sorted by their ASCII values, while in the actual output, they appear to be sorted by their frequencies in descending order. This comparison reveals a mismatch in the ordering criteria applied to the character frequencies and their corresponding Huffman codes. While the content and values are accurate, the presentation differs.
Expert Solution
steps

Step by step

Solved in 3 steps

Blurred answer
Follow-up Questions
Read through expert solutions to related follow-up questions below.
Follow-up Question

hello. i tried it out and it still didn't work

Expected output ?
1 Enter File Name to read:
2 | ASCII | Percent | Code
3
|
4
5
6
7
68
8
9
97
98
LO
100 |
99
101 | 0.07000 | 000 |
102 | 0.34000 | 11 |
Actual output ?
·‒‒‒‒
101
5
6 99
7
97
8
102
0.14000 | 101 |
0.08000 001
1 Enter File Name to read: | ASCII | Percent | Co
2 |
-|
·| ––––
3
4 98
|
100
0.13000 | 100 |
0.24000 | 01 |
0.07000
000 |
0.08000 | 001 |
0.24000 | 01 |
0.13000
100
0.14000 | 101 |
0.34000
| 11 |
Transcribed Image Text:Expected output ? 1 Enter File Name to read: 2 | ASCII | Percent | Code 3 | 4 5 6 7 68 8 9 97 98 LO 100 | 99 101 | 0.07000 | 000 | 102 | 0.34000 | 11 | Actual output ? ·‒‒‒‒ 101 5 6 99 7 97 8 102 0.14000 | 101 | 0.08000 001 1 Enter File Name to read: | ASCII | Percent | Co 2 | -| ·| –––– 3 4 98 | 100 0.13000 | 100 | 0.24000 | 01 | 0.07000 000 | 0.08000 | 001 | 0.24000 | 01 | 0.13000 100 0.14000 | 101 | 0.34000 | 11 |
Solution
Bartleby Expert
SEE SOLUTION
Knowledge Booster
Operations of Linked List
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education