Code implemented in C programming
Note: Comments are written for code explanation
Filename: huffman.c
#include "huffman.h"
#include<stdio.h>
#include<stdlib.h>
int cmp_int(int value1, int value2){
return value1-value2;
}
long int* asciicount_fn(long int* asciicount, FILE* fp){
unsigned char ch = 0;
do{
ch = fgetc(fp);
if(feof(fp)){
break;
}
asciicount[ch]++;
}while(ch!=EOF);
return asciicount;
}
Node* frequency_rank(long int* asciicount){
Node* head = NULL ;
for(int i=0;i<256;i++){
if(asciicount[i]!=0){
head =
_pq_enqueue(&head,i,asciicount[i],cmp_int);
}
}
return head;
}
Node *_pq_enqueue(Node **pq, int character, long int freq_ct,
int (*cmp_fn)(int, int))
{
Node* new = malloc(sizeof(*new));
if(new == NULL){
return NULL;
}
TreeNode* new_tn =
malloc(sizeof(*new_tn)); //allocate a new node for the
new_object to insert
if(new_tn==NULL){
//failure allocate
return NULL;
}
new_tn->ch =(unsigned char)character;
new_tn->freq = freq_ct;
new_tn->left = NULL;
new_tn->right = NULL;
new->t_node = new_tn;
if((*pq) == NULL){
//if its an empty list, the new node become the
first node
new->next = NULL;
*pq = new;
return *pq;
}
if(cmp_fn((*pq)->t_node->freq,freq_ct)>0){
new->next = *pq;
*pq = new;
return *pq;
}
else{
Node* head = *pq;//keep head stay
at the beginning of the linked_list
Node* curr = *pq;
while(((curr->next!=NULL)&&(cmp_fn(curr->next->t_node->freq,freq_ct)<0))){
//whether reach the end of the linked list or inserted in the
middle
curr=curr->next;
}
if((curr->next!=NULL)&&(cmp_fn(curr->next->t_node->freq,freq_ct)==0)){
if(curr->next->next != NULL){
while(curr->next->t_node->freq==curr->next->next->t_node->freq){
curr=curr->next;
if(curr->next->next==NULL){
break;
}
}
}
if(cmp_fn((int)(curr->next->t_node->ch),character)<0){
new->next = curr->next->next;
curr->next->next = new;
return head;
}
}
new->next = curr->next;
curr->next = new;
return head;
}
}
void _destroy_fn(Node *list, void
(*destroy_fn)(TreeNode*))
{
do{
Node* head = list->next;
Node* trash = list;
//break the first node from the list
list->next = NULL;
destroy_fn(trash->t_node);
free(list);
//free it
list = head;
}while(list!=NULL);
return;
}
Node *_pq_dequeue(Node **pq)
{
if(*pq == NULL){
return NULL;
}
else if(((*pq)->next==NULL)){ //move
the first node and assign NULL to pq.
Node* curr = *pq;
*pq = NULL;
return curr;
}
else{
Node* curr = *pq;
Node* consq = (*pq)->next;
*pq = consq;
curr->next = NULL;
return curr;
}
}
Node* build_tree(Node **pq)
{
while((*pq)->next!=NULL){
Node* junc_n =
malloc(sizeof(*junc_n));
if(junc_n==NULL){
return
NULL;
}
TreeNode* junc_tn =
malloc(sizeof(*junc_tn));
if(junc_tn == NULL){
return
NULL;
}
Node* child_a =
_pq_dequeue(pq);
Node* child_b =
_pq_dequeue(pq);
junc_tn->freq =
child_a->t_node->freq + child_b->t_node->freq;
junc_tn->left =
child_a->t_node;
junc_tn->right =
child_b->t_node;
free(child_a);
free(child_b);
junc_n->t_node = junc_tn;
tree_enqueue(pq,junc_n);
}
return *pq;
}
Node* tree_enqueue(Node** pq, Node* branch){
if((*pq) == NULL){
//if its an empty list, the new node become the
first node
branch->next = NULL;
*pq = branch;
return *pq;
}
if(((*pq)->t_node->freq)>(branch->t_node->freq)){
branch->next =
*pq;
*pq = branch;
return *pq;
}
else{
Node* head = *pq;//keep head stay
at the beginning of the linked_list
Node* curr = *pq;
while((curr->next!=NULL)&&(curr->next->t_node->freq
<= branch->t_node->freq)){ //whether reach the
end of the linked list or inserted in the middle
curr=curr->next;
}
branch->next =
curr->next;
curr->next = branch;
return head;
}
}
void huffman_code(TreeNode* root, char* array, int index, FILE*
fpe){
index++;
if((root->left==NULL)&&(root->right==NULL)){
array[index] =
root->ch;
fprintf(fpe,"%c%c",array[index],':');
for(int i =
0;i<=index-1;i++){
fprintf(fpe,"%c",array[i]);
}
fprintf(fpe,"\n");
}
else{
array[index] = '0';
huffman_code(root->left,array,index,fpe);
array[index] = '1';
huffman_code(root->right,array,index,fpe);
}
}
Filename: huffman.h
#include<stdio.h>
#include<stdlib.h>
typedef struct _TreeNode {
long int freq; // freq (weight) of char
unsigned char ch; // character
struct _TreeNode * left; // left
struct _TreeNode * right; // right
} TreeNode;
typedef struct _Node {
TreeNode* t_node;
struct _Node *next;
} Node;
Node* tree_enqueue(Node**, Node*);
Node* build_tree(Node**);
Node *_pq_enqueue(Node **pq, int, long int freq_ct,int
(*cmp_fn)(int,int));
void _destroy_fn(Node *list, void (*destroy_fn)(TreeNode *));
Node *_pq_dequeue(Node **pq);
long int* asciicount_fn(long int*, FILE*);
Node* frequency_rank(long int*);
void huffman_code(TreeNode*,char*, int,FILE*);
Filename: huffman_main.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "huffman.h"
void print_node(Node *list, void (*print_fn)(Node*))
{
while (list != NULL) {
// print the memory associated with
list->ptr
print_fn(list);
// print an arrow
fprintf(stdout,"->");
list = list->next;
}
// print NULL and a newline after that
fprintf(stdout, "NULL\n");
}
/*
static void print_fn(Node* list){
printf("%c:%ld",list->t_node->ch,list->t_node->freq);
}
*/
void write_file(FILE* fp2, Node* head){
while(head!=NULL){
fprintf(fp2,"%c",head->t_node->ch);
fprintf(fp2,"%c",':');
fprintf(fp2,"%ld",head->t_node->freq);
fprintf(fp2,"%s","->");
head = head->next;
}
fprintf(fp2,"%s","NULL");
fprintf(fp2,"\n");
}
void destroy_fn(TreeNode* node){
free(node);
}
void free_tree(TreeNode* node){
if((node->right!=NULL)){
free_tree(node->right);
}
if(node->left!=NULL){
free_tree(node->left);
}
free(node);
}
int main(int argc, char* argv[])
{
if(argc!=5){
return EXIT_FAILURE;
}
//Output file1 start
FILE *fp = fopen(argv[1],"r");
fseek(fp,0,SEEK_SET);
if(fp==NULL){
return EXIT_FAILURE;
}
FILE *fpw = fopen(argv[2],"w");
if(fpw==NULL){
return EXIT_FAILURE;
}
fseek(fpw,0,SEEK_SET);
long int asciicount[256] = {0};
asciicount_fn(asciicount,fp);
for(int i = 0; i<256; i++){
fprintf(fpw,"%ld\n",asciicount[i]);
}
// Output file1 end
Node* head =NULL;
head = frequency_rank(asciicount);
FILE* fp2 = fopen(argv[3],"w");
write_file(fp2,head);
// print_node(head,print_fn);// debugging purpose
Node* tree_head = NULL;
tree_head = build_tree(&head);
TreeNode* root = tree_head->t_node;
int count = 0;
while(root->right!= NULL){
count++;
root = root->right;
}
root = tree_head->t_node;
char adda;
char* array = &adda;
for(int i = 0;i<count;i++){
array[i] = '\0';
}
FILE* fpe = fopen(argv[4],"w");
int index = -1;
huffman_code(root,array,index,fpe);
free(tree_head);
free_tree(root);
fclose(fp2);
fclose(fp);
fclose(fpw);
fclose(fpe);
}
If you like my answer, hit thumbs up. Thank you
C language huffman This exercise will familiarize you with linked lists, which you will need for...
I have almost done with this code to Implement Huffman Coding. However I have an error at the "Heap<Tree>". Could anyone help with solving this issue, really appreciated. Thank you!! import java.util.Scanner; public class HuffmanEncoding { public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.print("Enter a text: "); String text = input.nextLine(); int[] counts = getCharacterFrequency(text); // Count frequency System.out.printf("%-15s%-15s%-15s%-15s\n", "ASCII Code", "Character", "Frequency", "Code"); Tree tree = getHuffmanTree(counts); // Create a Huffman tree String[]...
You will construct a Huffman tree based on the given frequencies of 26 English alphabets in upper case plus the space character. An internal tree node class in HuffmanTree with necessary information is required. • You will not randomly switch left and right children when merger two trees. Instead, you will build a right-heavy tree according to the following strategies to select the right child. (1) The tree that is taller will be the right child, i.e., the height is...
For this assignment, you will write a program to work with Huffman encoding. Huffman code is an optimal prefix code, which means no code is the prefix of another code. Most of the code is included. You will need to extend the code to complete three additional methods. In particular, code to actually build the Huffman tree is provided. It uses a data file containing the frequency of occurrence of characters. You will write the following three methods in the...
//I NEED THE PROGRAM IN C LANGUAGE!// QUESTION: I need you to write a program which manipulates text from an input file using the string library. Your program will accept command line arguments for the input and output file names as well as a list of blacklisted words. There are two major features in this programming: 1. Given an input file with text and a list of words, find and replace every use of these blacklisted words with the string...
I am getting a seg fault with my huffman tree. I'm not sure if it's my deconstructors but also getting some memory leaks. Can some one help me find my seg fault? It's in c++, thanks! //#ifndef HUFFMAN_HPP //#define HUFFMAN_HPP #include<queue> #include<vector> #include<algorithm> #include<iostream> #include <string> #include <iostream> using namespace std; class Node { public: // constructor with left and right NULL nodes Node(char charTemp, int c) { ch = charTemp; count = c; left...
Overview: file you have to complete is WordTree.h, WordTree.cpp, main.cpp Write a program in C++ that reads an input text file and counts the occurrence of individual words in the file. You will see a binary tree to keep track of words and their counts. Project description: The program should open and read an input file (named input.txt) in turn, and build a binary search tree of the words and their counts. The words will be stored in alphabetical order...
. Huffman Encoding (a.) (6 points) Suppose a certain file contains only the following letters with the corresponding frequencies 1 AİB 73 9 30 44 130 28 16 In a fixed-length encoding scheme, cach character is given a binary representation with the same number of bits. What is the minimum number of bits required to represent each letter of this file under fixed-length encoding scheme? Describe how to encode all seven letters in this file using the number of bits...
Write a C++ program which makes a binary tree that generates the Huffman code for any 7 characters and their given frequencies. As test input use a 3, b 4, c 1, d 3, e 12, f 4, g 2. Your program must insert nodes, and output the code for each character. Note: your program should be able to take any 7 characters and their frequencies as input. Three extra points if your program can accept 26 letters and 10...
I need help with this code, I'm stuck on it, please remember step 4, I'm very much stuck on that part. It says something about putting how many times it appears Assignment #1: Sorting with Binary Search Tree Through this programming assignment, the students will learn to do the following: Know how to process command line arguments. 1 Perform basic file I/O. 2. Use structs, pointers, and strings. Use dynamic memory. 3. 4. This assignment asks you to sort the...
You need to implement a queue based on the doubly linked list. (In C++) **PLEASE follow the format** **Don't worry about the readme.txt** THANK YOU SO SO MUCH! You have to implement the doubly linked list in DList.h and DList.cpp and the queue in the LinkedQueue.h and LinkedQueue.cpp; all as template classes. You have to provide the main.cpp file as we discussed in class to allow the user to interact with your queue using enqueue, dequeue, front, back, size, empty,...