Click here to Skip to main content
15,892,480 members
Home / Discussions / Java
   

Java

 
GeneralRe: Noob question Pin
fly90419-Apr-09 9:56
fly90419-Apr-09 9:56 
QuestionHuffman coding in Java Script Encoding Decoding Pin
novhard19-Apr-09 4:35
novhard19-Apr-09 4:35 
AnswerRe: Huffman coding in Java Script Encoding Decoding Pin
MikeMarq19-Apr-09 5:13
MikeMarq19-Apr-09 5:13 
GeneralRe: Huffman coding in Java Script Encoding Decoding Pin
novhard19-Apr-09 6:33
novhard19-Apr-09 6:33 
GeneralRe: Huffman coding in Java Script Encoding Decoding Pin
MikeMarq19-Apr-09 8:20
MikeMarq19-Apr-09 8:20 
GeneralRe: Huffman coding in Java Script Encoding Decoding Pin
novhard19-Apr-09 18:39
novhard19-Apr-09 18:39 
GeneralRe: Huffman coding in Java Script Encoding Decoding [modified] Pin
MikeMarq20-Apr-09 6:37
MikeMarq20-Apr-09 6:37 
GeneralThe code Pin
MikeMarq20-Apr-09 6:41
MikeMarq20-Apr-09 6:41 
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>

#define TRUE 1
#define FALSE 0
#define MAXBITS 26


void prq_create(struct prqueue*);
int prq_empty(struct prqueue*);
void prq_insert(struct prqueue*, struct info_node*);
void prq_remove(struct prqueue*, struct info_node*);
void merge_nodes(struct info_node*, struct info_node*, struct info_node*);
void init_node(struct info_node*);
void print_level_order(FILE*, struct info_node*);
void print_code(FILE*, struct info_node*);
int isleft(struct info_node*);
int build_numcode(struct info_node*, int []);

void print_code(FILE*,struct info_node* pleaf_location[]);
void build_pque (FILE*, struct prqueue*, struct info_node* []);
struct info_node* build_tree (struct prqueue*);
void print_inorder(FILE*, struct info_node*);
void decode (FILE* ,struct info_node* , FILE*);




#define TRUE 1
#define FALSE 0


struct info_node
{
char code[MAXBITS] ;
float weight ;
int level ;
struct info_node* father ;
struct info_node* left ;
struct info_node* right ;
struct info_node* next ;
} ;


struct prqueue
{
struct info_node * prqtop ;
};

struct lo_queue
{
struct info_node* front ;
struct info_node* rear ;
};


int main()
{
struct prqueue pque ;
struct info_node* pleaf_location[MAXBITS] ;
struct info_node* tree_root ;
FILE* infile; FILE* outfile; FILE* dec_result; FILE* bitfile;
int count = 0;


infile = fopen("symb_freq.txt", "r");
outfile = fopen("code_tree.txt", "w");
bitfile = fopen("bit_file.txt", "r");
dec_result = fopen("decoded_file.txt", "w");


//builds the priority queue
pque.prqtop = NULL;
build_pque(infile, &pque, pleaf_location);

fclose(infile);

////builds the tree
tree_root = build_tree(&pque);

//does the inorder traversal
print_inorder(outfile, tree_root);
fprintf(outfile, "\n\n");

//prints the level order traversal
print_level_order(outfile, tree_root);


//prints the code for each letter
print_code(outfile, pleaf_location);

//decodes the zeros and ones into a message
decode(bitfile, tree_root, dec_result);

fclose(outfile);
fclose(bitfile);
fclose(dec_result);

return( 0 ) ;
} // end of function main()


void build_pque (FILE* infile, struct prqueue* pque, struct info_node* pleaf_location[])
{
int n = 0; struct info_node* node;


//creates the priority queue and loads pleaf_location array
while(!feof(infile))
{
node = (info_node*) malloc(sizeof(struct info_node));
init_node(node);
fscanf(infile, "%s", &node->code[0]);
fscanf(infile, "%f", &node->weight);
prq_insert(pque, node);
pleaf_location[n] = node;
n++;
}
}

struct info_node* build_tree (struct prqueue* pque)
{
struct info_node* p1; struct info_node* p2;
struct info_node* rnode; struct info_node* tree_root;

//builds the tree
while(pque->prqtop->next != NULL)
{
p1 = pque->prqtop;
p2 = p1->next;
pque->prqtop = p2->next; //moves top to the next spot
rnode = (info_node*) malloc(sizeof(struct info_node));
init_node(rnode);
merge_nodes(p1, p2, rnode);
prq_insert(pque, rnode);
}
tree_root = pque->prqtop;
pque->prqtop = NULL;

return tree_root;
}



void print_code(FILE* outfile, struct info_node* node)
{
int n = 0;

while(node->code[n] != '\0')
{
fprintf(outfile, "%c",node->code[n]);
n++;
}
fprintf(outfile, " ");
}


//makes a node and inititializes it
void init_node (struct info_node* node)
{
node->father = NULL;
node->left = NULL;
node->right = NULL;
}


//test if it is empty
int prq_empty(struct prqueue* prq)
{
if (prq->prqtop == NULL)
return TRUE;
else
return FALSE;
}

//inserts node into priority queue
void prq_insert(struct prqueue* prq, struct info_node* node)
{
struct info_node* curnode; struct info_node* lastnode = NULL;

curnode = prq->prqtop;
if ((curnode == NULL) || (node->weight <= curnode->weight)) //inserts at the start of the queue
{
node->next = curnode;
prq->prqtop = node;
}

else //moves through the list and inserts at the right spot
{
//lastnode = curnode;
//curnode = lastnode->next;
while ((curnode != NULL) && (node->weight > curnode->weight))
{
lastnode = curnode;
curnode = lastnode->next;
}
lastnode->next = node;
node->next = curnode;
}

return;
}

//removes one node and passes it back by reference
void prq_remove(struct prqueue* prq, struct info_node* node)
{
node = prq->prqtop;
prq->prqtop = node->next;
node->next = NULL;
}


//this creates a new node above the 2 existing nodes and calculated values for the new node
void merge_nodes(struct info_node* p1, struct info_node* p2, struct info_node* rnode)
{
//links nodes
rnode->left = p1;
rnode->right = p2;
p1->father = rnode;
p2->father = rnode;

//calculates value for the father
rnode->weight = p1->weight + p2->weight;
strcpy(rnode->code , p1->code);
strcat(rnode->code, p2->code);

//clears old links
p1->next = NULL;
p2->next = NULL;
}

//builds the code in left to right order. //this will be reversed when it is read
int build_numcode(struct info_node* node, int numcode [])
{
int count = 0;

while (node->father != NULL)
{
if (isleft(node))
{
numcode[count] = 0;
count += 1;
}
else
{
numcode[count] = 1;
count += 1;
}
node = node->father;
}

return count;
}

//test if the node is on the left side of the father node
int isleft(struct info_node* node)
{
if (node == node->father->left)
return TRUE;
else
return FALSE;
}




///////////////////////////////////////////////////////////////////////////////
// PRINTS THE CODE FOR EACH LETTER


void print_code(FILE* outfile, struct info_node* pleaf_location[])
{
int n; int a; int count; int numcode[10];

//prints the code for each letter
fprintf(outfile, "\n\n");
for (n = 0; n < MAXBITS; n++)
{
count = build_numcode(pleaf_location[n], numcode);
fprintf(outfile, "Symbol: %c Huffman Code: ", pleaf_location[n]->code[0]);
for (a = (count - 1); a >= 0; a--) //prints code in reverse order
fprintf(outfile, "%d", numcode[a]);

fprintf(outfile, "\n");
}
}




////////////////////////////////////////////////////////////////////////////////
// IN ORDER TRAVERAL CODE


void print_inorder(FILE* outfile, struct info_node* node)
{
if (node != NULL)
{
print_inorder(outfile, node->left);
fprintf(outfile, "FREQUENCY: %f SYMBOL: ", node->weight);
print_code(outfile, node); //prints out all the letters
fprintf(outfile, "\n");
print_inorder(outfile, node->right);
}
}



////////////////////////////////////////////////////////////////////////////////////////
//LEVEL ORDER TRAVERSAL CODE


//prints the current layer and gets nodes of the next layer
void print_level_order(FILE* outfile, struct info_node* tree_root)
{
struct info_node* left; struct info_node* right; int level = 0;
struct lo_queue* queue;

queue = (lo_queue*) malloc(sizeof(struct lo_queue));

//prints out the levels
tree_root->level = 0;
queue->rear = tree_root;
queue->front = tree_root;
fprintf(outfile, "level 0: ");
//print_levels(outfile, &real_queue);

while(queue->rear != NULL)
{
if (queue->rear->level != level) //if level changed
{
fprintf(outfile, "\n");
fprintf(outfile, "level %d: ", queue->rear->level);
}
print_code(outfile, queue->rear);
level = queue->rear->level;

//puts atoms in the queue
left = queue->rear->left;
if (left != NULL)
{
left->level = level + 1;
queue->front->next = left;
queue->front = left;
}
right = queue->rear->right;
if (right != NULL)
{
right->level = level + 1;
queue->front->next = right;
queue->front = right;
}
queue->rear = queue->rear->next;
}

}



////////////////////////////////////////////////////////////////////////////////////////
//Decoding


void decode (FILE* infile,struct info_node* tree_root, FILE* outfile)
{
char num; int n = 0; struct info_node* node;

node = tree_root;

while(!(feof(infile)))
{
fscanf(infile, "%c", &num);
if (num == '0')
{
node = node->left;
if (node->left == NULL) //is a leaf node
{
fprintf(outfile, "%c" , node->code[0]);
node = tree_root;
}
}
else if (num == '1')
{
node = node->right;
if (node->right == NULL) //is a leaf node
{
fprintf(outfile, "%c" , node->code[0]);
node = tree_root;
}
}
else
{
fprintf(outfile, "%c", num);
}

n++;
}
}
GeneralThe encripted file Pin
MikeMarq20-Apr-09 6:43
MikeMarq20-Apr-09 6:43 
GeneralSymbol Frequencies Pin
MikeMarq20-Apr-09 6:44
MikeMarq20-Apr-09 6:44 
GeneralRe: Huffman coding in Java Script Encoding Decoding Pin
novhard21-Apr-09 18:53
novhard21-Apr-09 18:53 
QuestionDisplaying Text on a JFrame Pin
MikeMarq18-Apr-09 11:22
MikeMarq18-Apr-09 11:22 
QuestionHow to display Time for an Online Test Pin
kaushal kishore sharma16-Apr-09 19:31
kaushal kishore sharma16-Apr-09 19:31 
QuestionJava Video Player Pin
Flying_Doc16-Apr-09 16:00
Flying_Doc16-Apr-09 16:00 
AnswerRe: Java Video Player Pin
vctrlao23-Apr-09 20:39
vctrlao23-Apr-09 20:39 
QuestionHow to log into a web site with a certificate. Pin
werpa13-Apr-09 11:03
werpa13-Apr-09 11:03 
Questiongetting text from radio button Pin
Vishnu Prem12-Apr-09 20:12
Vishnu Prem12-Apr-09 20:12 
AnswerRe: getting text from radio button Pin
vctrlao23-Apr-09 20:41
vctrlao23-Apr-09 20:41 
QuestionHow can i get special folder icon ? Pin
anbluemoon12-Apr-09 1:44
anbluemoon12-Apr-09 1:44 
AnswerRe: How can i get special folder icon ? Pin
fly90412-Apr-09 5:02
fly90412-Apr-09 5:02 
Question[Message Deleted] Pin
kaushal kishore sharma10-Apr-09 0:01
kaushal kishore sharma10-Apr-09 0:01 
AnswerRe: making Proxy server by using Java Pin
Nagy Vilmos11-Apr-09 0:40
professionalNagy Vilmos11-Apr-09 0:40 
QuestionUrdu input in java or Vb6 Pin
atiyakhan9-Apr-09 23:16
atiyakhan9-Apr-09 23:16 
QuestionHow to append data to na excel file using java or jsp or struts? Pin
kaushal kishore sharma9-Apr-09 19:59
kaushal kishore sharma9-Apr-09 19:59 
Questiondid the ide like vs are avail for java and jsp too? [modified] Drop Down feature in java n jsp????? Pin
Alok Sharma ji9-Apr-09 6:52
Alok Sharma ji9-Apr-09 6:52 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.