
Concept explainers
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


Step by stepSolved in 3 steps

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

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

- // FILE: DPQueue.h// CLASS PROVIDED: p_queue (priority queue ADT)//// TYPEDEFS and MEMBER CONSTANTS for the p_queue class:// typedef _____ value_type// p_queue::value_type is the data type of the items in// the p_queue. It may be any of the C++ built-in types// (int, char, etc.), or a class with a default constructor, a// copy constructor, an assignment operator, and a less-than// operator forming a strict weak ordering.//// typedef _____ size_type// p_queue::size_type is the data type considered best-suited// for any variable meant for counting and sizing (as well as// array-indexing) purposes; e.g.: it is the data type for a// variable representing how many items are in the p_queue.// It is also the data type of the priority associated with// each item in the p_queue//// static const size_type DEFAULT_CAPACITY = _____// p_queue::DEFAULT_CAPACITY is the default initial capacity of a// p_queue that is created by the default…arrow_forward#include <iostream> usingnamespace std; class Queue { int size; int* queue; public: Queue(){ size = 0; queue = new int[100]; } void add(int data){ queue[size]= data; size++; } void remove(){ if(size ==0){ cout <<"Queue is empty"<<endl; return; } else{ for(int i =0; i < size -1; i++){ queue[i]= queue[i +1]; } size--; } } void print(){ if(size ==0){ cout <<"Queue is empty"<<endl; return; } for(int i =0; i < size; i++){ cout<<queue[i]<<" <- "; } cout << endl; } //your code goes here }; int main(){ Queue q1; q1.add(42); q1.add(2); q1.add(8); q1.add(1); Queue q2; q2.add(3); q2.add(66); q2.add(128); q2.add(5); Queue q3 = q1+q2; q3.print();…arrow_forward#include <iostream>#include <cstdlib>using namespace std; class IntNode {public: IntNode(int dataInit = 0, IntNode* nextLoc = nullptr); void InsertAfter(IntNode* nodeLoc); IntNode* GetNext(); void PrintNodeData(); int GetDataVal();private: int dataVal; IntNode* nextNodePtr;}; // ConstructorIntNode::IntNode(int dataInit, IntNode* nextLoc) { this->dataVal = dataInit; this->nextNodePtr = nextLoc;} /* Insert node after this node. * Before: this -- next * After: this -- node -- next */void IntNode::InsertAfter(IntNode* nodeLoc) { IntNode* tmpNext = nullptr; tmpNext = this->nextNodePtr; // Remember next this->nextNodePtr = nodeLoc; // this -- node -- ? nodeLoc->nextNodePtr = tmpNext; // this -- node -- next} // Print dataValvoid IntNode::PrintNodeData() { cout << this->dataVal << endl;} // Grab location pointed by nextNodePtrIntNode* IntNode::GetNext() { return this->nextNodePtr;} int IntNode::GetDataVal() {…arrow_forward
- // FILL IN THE BLANKS (LINKED-LISTS CODE) (C++)#include<iostream>using namespace std; struct ________ {int data ;struct node *next; }; node *head = ________;node *createNode() { // allocate a memorynode __________;temp = new node ;return _______ ;} void insertNode(){node *temp, *traverse;int n;cout<< "Enter -1 to end "<<endl;cout<< "Enter the values to be added in list"<<endl;cin>>n; while(n!=-1){temp = createNode(); // allocate memorytemp->data = ________;temp->next = ________;if ( ___________ == NULL){head = _________;} else {traverse = ( );while (traverse->next != ________{traverse = traverse-> ___________;} traverse->next= temp;} cout<<"Enter the value to be added in the list"<<endl;cin>>n; }} void printlist(){node *traverse = head; // if head == NULLwhile (traverse != NULL) { cout<<traverse->data<<" ";traverse = traverse->next;}} int main(){int option; do{cout<<"\n =============== MAIN…arrow_forwardC++ Data Structure:Create an AVL Tree C++ class that works similarly to std::map, but does NOT use std::map. In the PRIVATE section, any members or methods may be modified in any way. Standard pointers or unique pointers may be used.** MUST use given Template below: #ifndef avltree_h#define avltree_h #include <memory> template <typename Key, typename Value=Key> class AVL_Tree { public: classNode { private: Key k; Value v; int bf; //balace factor std::unique_ptr<Node> left_, right_; Node(const Key& key) : k(key), bf(0) {} Node(const Key& key, const Value& value) : k(key), v(value), bf(0) {} public: Node *left() { return left_.get(); } Node *right() { return right_.get(); } const Key& key() const { return k; } const Value& value() const { return v; } const int balance_factor() const {…arrow_forwardstruct nodeType { int infoData; nodeType * next; }; nodeType *first; … and containing the values(see image) Using a loop to reach the end of the list, write a code segment that deletes all the nodes in the list. Ensure the code performs all memory ‘cleanup’ functions.arrow_forward
- struct remove_from_front_of_dll { // Function takes no parameters, removes the book at the front of a doubly // linked list, and returns nothing. void operator()(const Book& unused) { //// TO-DO (13) |||| // Write the lines of code to remove the book at the front of "my_dll", // // Remember, attempting to remove an element from an empty data structure is // a logic error. Include code to avoid that. ///// END-TO-DO (13) //// } std::list& my_dll; };arrow_forwardC++ The List class represents a linked list of dynamically allocated elements. The list has only one member variable head which is a pointer that leads to the first element. See the following code for the destructor to List. ~ List () { for (int i = 0; i <size (); i ++) { pop_back (); } } What problems does the destructor have? Select one or more options: 1. There are no parameters for the destructor. 2. The return value from pop_back (if any) is nerver handled. 3. The destructor will create a stack overflow. 4. The destructor will create dangling pointers. 5.The destructor will create memory leaks. 6.The destructor will create undefined behavior (equivalent to zero pointer exception). 7.The condition must be: i <size () - 1 8. There is at least one problem with the destructor, but none of the above.arrow_forward@6 The Reference-based Linked Lists: Select all of the following statements that are true. options: As a singly linked list's node references both its predecessor and its successor, it is easily possible to traverse such a list in both directions. According to the terminology introduced in class, the head reference variable in a singly linked list object references the list's first node. According to the terminology introduced in class, in a doubly linked list, each node references both the head and tail node. In a double-ended singly linked list, the tail reference variable provides access to the entire list. In a circular linked list, the last node references the first node.arrow_forward
- Database System ConceptsComputer ScienceISBN:9780078022159Author:Abraham Silberschatz Professor, Henry F. Korth, S. SudarshanPublisher:McGraw-Hill EducationStarting Out with Python (4th Edition)Computer ScienceISBN:9780134444321Author:Tony GaddisPublisher:PEARSONDigital Fundamentals (11th Edition)Computer ScienceISBN:9780132737968Author:Thomas L. FloydPublisher:PEARSON
- C How to Program (8th Edition)Computer ScienceISBN:9780133976892Author:Paul J. Deitel, Harvey DeitelPublisher:PEARSONDatabase Systems: Design, Implementation, & Manag...Computer ScienceISBN:9781337627900Author:Carlos Coronel, Steven MorrisPublisher:Cengage LearningProgrammable Logic ControllersComputer ScienceISBN:9780073373843Author:Frank D. PetruzellaPublisher:McGraw-Hill Education





