Guest User

Untitled

a guest
Jan 22nd, 2019
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 7.09 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. typedef struct huffman_table {
  6.     char *character;
  7.     char code[10000];
  8.     struct huffman_table *next_;
  9. }   huffman_table;
  10.     huffman_table *root;
  11.  
  12. typedef struct frequency {
  13.     char *character;
  14.     int frequency;
  15.     struct frequency *next;
  16.     struct frequency *left;
  17.     struct frequency *right;
  18. }   td_frequency;
  19.     td_frequency *start, *start_freq, *freq_hold_x, *freq_hold_y, *pointer, *temp, *temp2;
  20.  
  21. int size = 0;
  22.  
  23. int count();
  24. int is_present();
  25. void generate_frequency();
  26. td_frequency huffman_main ();
  27. td_frequency *create_parent ();
  28. td_frequency extract_node ();
  29. int count_nodes ();
  30. void generate_code ();
  31. void print_file ();
  32.  
  33. int main (int argc, char *argv[]) {
  34.     start = NULL;
  35.     root = NULL;
  36.     char code[1];
  37.  
  38.     char input[50];
  39.     scanf("%s", input);
  40.  
  41.     if (argc != 4) {
  42.         printf("\nSyntax: %s <input.txt> <histogram.txt> <encoded.txt>", argv[1]);
  43.     }
  44.  
  45.     generate_frequency(input);
  46.     puts("\nGenerating tree...");
  47.     huffman_main();
  48.     puts("\nGenerating code...");
  49.     td_frequency *start_print = start;
  50.  
  51.     //traverse(start_print);
  52.  
  53.     generate_code (start, code);
  54.     return 0;
  55. }
  56.  
  57. void generate_frequency (const char *inserted_string) {
  58.     int i;
  59.  
  60.     for (i = 0; i < strlen(inserted_string); i++){
  61.         td_frequency *inserted_node = (td_frequency *)malloc(sizeof(td_frequency));
  62.         volatile int temp_count = count(inserted_string[i], inserted_string);
  63.         volatile int is_there = is_present(inserted_string[i], inserted_string);
  64.  
  65.         inserted_node->frequency = temp_count;
  66.         inserted_node->character = inserted_string[i];
  67.         inserted_node->left = NULL;
  68.         inserted_node->right = NULL;
  69.  
  70.         if (is_present(inserted_string[i], inserted_string) == 0) {
  71.             if(start == NULL) {
  72.                     start =  inserted_node;
  73.                     start->next = NULL;
  74.             }   else if (start->frequency > temp_count) {
  75.                     temp = start;
  76.                     start = inserted_node;
  77.                     start->next = temp;
  78.             }   else {
  79.                     temp = start;
  80.                     temp2 = start;
  81.                     while (temp != NULL) {
  82.                         if (temp->frequency > temp_count) {
  83.                                 temp2->next = (td_frequency *)malloc(sizeof(td_frequency));
  84.                                 temp2 = temp2->next;
  85.                                 temp2->character = inserted_string[i];
  86.                                 temp2->frequency = temp_count;
  87.                                 temp2->right = NULL;
  88.                                 temp2->left = NULL;
  89.                                 temp2->next = temp;
  90.                                 break;
  91.                         }   else if (temp->next == NULL) {
  92.                                 temp->next = (td_frequency *)malloc(sizeof(td_frequency));
  93.                                 temp = temp->next;
  94.                                 temp->character = inserted_string[i];
  95.                                 temp->frequency = temp_count;
  96.                                 temp->next = NULL;
  97.                                 temp->left = NULL;
  98.                                 temp->right = NULL;
  99.                                 break;
  100.                         }   else temp2 = temp, temp = temp->next;
  101.                     }
  102.             }
  103.         }
  104.     }
  105.  
  106.     printf("\nALL ENTRIES:\n");
  107.     pointer = start;
  108.     while (pointer != NULL) {
  109.         printf("\n| Chaarcter:   %c\n| Frequency:   %d\n", pointer->character, pointer->frequency);
  110.         pointer = pointer->next;
  111.         size++;
  112.     }
  113.     return;
  114. }
  115.  
  116. td_frequency huffman_main (int size) {
  117.     td_frequency *temp_parent;
  118.     int i, n = count_nodes();
  119.  
  120.     if (start != NULL) {
  121.         for (i = n; i > 1 ; i--) {
  122.             if (count_nodes() >1) {
  123.                 printf("\nHUFFMAN MAIN!\n");
  124.                 extract_node();
  125.                 temp_parent = create_parent();
  126.                 printf("\nTemporary parent: %d", temp_parent->frequency);
  127.  
  128.                 if (start == NULL) {
  129.                     start = temp_parent;
  130.                 }   else if (start->frequency > temp_parent->frequency) {
  131.                         temp = start;
  132.                         start = temp_parent;
  133.                         start->next = temp;
  134.                 }   else {
  135.                         temp = start;
  136.                         temp2 = start;
  137.                         while (temp != NULL) {
  138.                             if (temp->frequency > temp_parent->frequency) {
  139.                                 temp2->next = temp_parent;
  140.                                 temp2 = temp2->next;
  141.                                 temp2->next = temp;
  142.                                 break;
  143.                             }   else if (temp->next == NULL) {
  144.                                     temp->next = temp_parent;
  145.                                     temp = temp->next;
  146.                                     temp->next = NULL;
  147.                                     break;
  148.                             }   else {
  149.                                     temp2 = temp;
  150.                                     temp = temp->next;
  151.                             }
  152.                         }
  153.                 }
  154.             }
  155.         }
  156.     }
  157.     start = temp_parent;
  158.     printf("%\nSTRT FREQ: %d\n", start->frequency);
  159.     pointer = start;
  160.  
  161.     return;
  162. }
  163.  
  164. td_frequency *create_parent (void) {
  165.     td_frequency *parent = (td_frequency *)malloc(sizeof(td_frequency));
  166.     parent->frequency = freq_hold_x->frequency + freq_hold_y->frequency;
  167.     freq_hold_x->next = NULL;
  168.     freq_hold_y->next = NULL;
  169.  
  170.     parent->left = freq_hold_x;
  171.     parent->right = freq_hold_y;
  172.     printf("\nFrequency (inside create_parent): %d\n", parent->frequency);
  173.  
  174.     return parent;
  175. }
  176.  
  177. td_frequency extract_node (void) {
  178.     freq_hold_x = start;
  179.     printf("\nFrequency of x: %d\n", start->frequency);
  180.     freq_hold_y = start->next;
  181.     printf("Frequency of y: %d\n", start->next->frequency);
  182.     start = start->next->next;
  183.     return;
  184. }
  185.  
  186. int count_nodes (void) {
  187.     int count = 0;
  188.     td_frequency *temp = start;
  189.  
  190.     do {
  191.         count++;
  192.         temp = temp->next;
  193.     }   while (temp != NULL);
  194.     printf("\nCOUNT: %d\n", count);
  195.     return count;
  196. }
  197.  
  198. int count (char *being_counted, char *input) {
  199.     int count_dracula = 0, i;
  200.     for (i = 0; i < strlen(input); i++) {
  201.         if (being_counted == input[i]) count_dracula++;
  202.     }
  203.     return count_dracula;
  204. }
  205.  
  206. int is_present (char *being_searched, char *input) {
  207.     td_frequency *search = start;
  208.     while (search != NULL) {
  209.         if (being_searched == search->character) return 1;
  210.         else {
  211.             search = search->next;
  212.         }
  213.     }
  214.     return 0;
  215. }
  216.  
  217. void generate_code (td_frequency *tree, char *huffman_code) {
  218.     /*puts("ENTERING 1.\n");
  219.     printf("TREE->ELEMENT: %d\n",tree->frequency);
  220.     if(tree->left != NULL) printf("TREE->LEFT: %d\n", tree->left->frequency);
  221.     if(tree->right!=NULL) printf("TREE->RIGHT: %d\n", tree->right->frequency);
  222.     printf("TREE IS NULL: %d\n",tree==NULL);
  223.     printf("TREE->RIGHT IS NULL: %d\n", tree->right==NULL);
  224.     printf("TREE->LEFT IS NULL: %d\n\n", tree->left==NULL);*/
  225.     if (tree->left == NULL && tree->right == NULL) {
  226.         puts("ENTERING 2.");
  227.         huffman_table *table_ = (huffman_table *)malloc(sizeof(huffman_table));
  228.         table_->character = tree->character;
  229.         table_->code[10000] = huffman_code;
  230.         table_->next_ = NULL;
  231.         puts("ENTERING 2.1");
  232.  
  233.         if (root == NULL) {
  234.             puts("ENTERING 2.2");
  235.             root = table_;
  236.             root->next_ = NULL;
  237.         }   else {
  238.                 puts("ENTERING 2.3");
  239.                 huffman_table *tempting = root;
  240.                 while (tempting->next_ != NULL) {
  241.                     puts("ENTERING 2.4");
  242.                     printf("TEMP NEXT IS NULL: %d\n", tempting->next_==NULL);
  243.                     tempting = tempting->next_;
  244.                     puts("EXITING 2.4");
  245.                 }
  246.                 puts("ENTERING ASSIGNING PHASE!");
  247.                 //tempting->next_ = (huffman_table *)malloc(sizeof(huffman_table));
  248.                 tempting->next_ = table_;
  249.                 puts("DONE");
  250.         }
  251.  
  252.     }   else {
  253.         puts("ENTERING 3.");
  254.         //printf("BEFOR STRCAT: %s\n", huffman_code);
  255.         //printf("STRCAT RESULT: %s\n\n", strcat(huffman_code, "0"));
  256.         generate_code(tree->left, strcat(huffman_code, "0"));
  257.         puts("ENTERING 3.2");
  258.         generate_code(tree->right, strcat(huffman_code, "1"));
  259.     }
  260.  
  261.     puts("supposedly returning na");
  262.     return 0;
  263. }
  264.  
  265. void print_file (char *file_name) {
  266.  
  267. }
  268.  
  269. void traverse (td_frequency *rootsss) {
  270.     if (rootsss != NULL) {
  271.         printf("%d ", rootsss->frequency);
  272.         puts("\nError?");
  273.         traverse(rootsss->left);
  274.         puts("\nError?");
  275.         traverse(rootsss->right);
  276.     }
  277. }
Add Comment
Please, Sign In to add comment