utility.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include "pa09.h"
  4. Stack * pop_stack (Stack * top)
  5. {
  6. Stack * temp = top -> next;
  7. free(top);
  8. return temp;
  9. }
  10. Stack * push_stack (Stack * top, HuffNode * node)
  11. {
  12. Stack * current_top = malloc(sizeof(Stack)); //current_top = new_top
  13. current_top -> next = top;
  14. current_top -> node = node;
  15. return current_top;
  16. }
  17. HuffNode * create_HuffmanNode(char val)
  18. {
  19. HuffNode * node = malloc(sizeof(HuffNode));
  20. node -> value = val;
  21. node -> left = NULL;
  22. node -> right = NULL;
  23. return node;
  24. }
  25. void tree_destroy(HuffNode * tree)
  26. {
  27. if(tree == NULL)
  28. {
  29. return;
  30. }
  31. tree_destroy(tree -> left);
  32. tree_destroy(tree -> right);
  33. free(tree);
  34. }
  35. HuffNode * create_character_tree(FILE * input_file_ptr)
  36. {
  37. int flag = 0;
  38. Stack * top = NULL;
  39. char ch;
  40. char chardata;
  41. HuffNode * rc;
  42. HuffNode * lc;
  43. HuffNode * new;
  44. while(flag == 0)
  45. {
  46. ch = fgetc(input_file_ptr);
  47. if(ch == '1')
  48. {
  49. chardata = fgetc(input_file_ptr);
  50. top = push_stack(top, create_HuffmanNode(chardata));
  51. }
  52. if(ch == '0')
  53. {
  54. rc = top -> node;
  55. top = pop_stack(top);
  56. if(top == NULL)
  57. {
  58. flag = 1;
  59. }
  60. else
  61. {
  62. lc = top -> node;
  63. new = create_HuffmanNode(0);
  64. new -> right = rc;
  65. new -> left = lc;
  66. top = pop_stack(top);
  67. top = push_stack(top, new);
  68. }
  69. }
  70. }
  71. return rc;
  72. }
  73. HuffNode * load_header(FILE * input_file_ptr)
  74. {
  75. char ch = '0';
  76. int initial = 1;
  77. fseek(input_file_ptr, 0, SEEK_SET);
  78. Stack * top2 = NULL;
  79. while((ch = get_bits(input_file_ptr, 1) != 0) || (initial == 1) || top2 -> next != NULL)
  80. {
  81. initial = 0;
  82. if(ch == '1')
  83. {
  84. ch = get_bits(input_file_ptr, 8);
  85. top2 = push_stack(top2, NULL);
  86. top2 -> node = create_HuffmanNode(ch);
  87. }
  88. if(ch == '0')
  89. {
  90. HuffNode * node = create_HuffmanNode(0);
  91. node -> right = top2 -> node;
  92. top2 = pop_stack(top2);
  93. node -> left = top2 -> node;
  94. top2 = pop_stack(top2);
  95. top2 = push_stack(top2, node);
  96. }
  97. }
  98. Stack * temp = top2;
  99. free(top2);
  100. return temp -> node;
  101. }
  102. char get_bits(FILE * input_file_ptr, int number)
  103. {
  104. char ch = ' ';
  105. int counter = 0;
  106. char read_bits = ' ';
  107. if(counter == 0)
  108. {
  109. fread(&ch, 1, 1, input_file_ptr);
  110. counter = 8;
  111. }
  112. if(counter < number)
  113. {
  114. read_bits = ch;
  115. read_bits = (read_bits << (8 - counter));
  116. fread(&ch, 1, 1, input_file_ptr);
  117. read_bits = (read_bits | (ch >> counter));
  118. return read_bits;
  119. }
  120. read_bits = ch;
  121. if(number == 1)
  122. {
  123. read_bits = (read_bits >> (counter - number)) & 0x01;
  124. counter--;
  125. }
  126. else
  127. {
  128. read_bits = (read_bits >> (counter - number));
  129. counter = 0;
  130. }
  131. return read_bits;
  132. }
  133. void Huff_postOrderPrint(HuffNode * tree, FILE * output_file_ptr)
  134. {
  135. // Base case: empty subtree
  136. if (tree == NULL)
  137. {
  138. return;
  139. }
  140. // Recursive case: post-order traversal
  141. // Visit left
  142. fprintf(output_file_ptr, "Left\n");
  143. Huff_postOrderPrint(tree -> left, output_file_ptr);
  144. fprintf(output_file_ptr, "Back\n");
  145. // Visit right
  146. fprintf(output_file_ptr, "Right\n");
  147. Huff_postOrderPrint(tree -> right, output_file_ptr);
  148. fprintf(output_file_ptr, "Back\n");
  149. // Visit node itself (only if leaf)
  150. if (tree -> left == NULL && tree -> right == NULL)
  151. {
  152. fprintf(output_file_ptr, "Leaf: %c\n", tree -> value);
  153. }
  154. }