Advertisement
starfoxleader

Lab09

Apr 16th, 2020
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.40 KB | None | 0 0
  1. // Matthew Felix
  2. // CSE-100
  3. // Lab 09
  4. // Spring 2020
  5. // 4.16.2020
  6.  
  7. #include <iostream>
  8. #include <string>
  9. #include <vector>
  10. #include <queue>
  11.  
  12. using namespace std;
  13.  
  14. // A Huffman tree node struct
  15.  
  16. struct Node {
  17.  
  18. char data;
  19. unsigned freq;
  20. Node* left;
  21. Node* right;
  22.  
  23. Node(char data, unsigned freq) {
  24.  
  25. left = NULL;
  26. right = NULL;
  27. this->data = data;
  28. this->freq = freq;
  29.  
  30. }
  31.  
  32. };
  33.  
  34. string* s = new string[1000];
  35.  
  36. // Comparing two heap nodes
  37.  
  38. struct compare {
  39.  
  40. bool operator()(Node* l, Node* r) {
  41. return (l->freq > r->freq);
  42. }
  43. };
  44.  
  45. void printCode(struct Node* root, string str, char arr[], int n) {
  46.  
  47. if (root == NULL) {
  48. return;
  49. }
  50.  
  51. if (root->data != ' ') {
  52. for (int i = 0; i < n; i++) {
  53. if (root->data == arr[i]) {
  54. s[i] = str;
  55. }
  56. }
  57. //cout << root->data << ":" << str << endl;
  58.  
  59. }
  60.  
  61. printCode(root->left, str + "0", arr, n);
  62. printCode(root->right, str + "1", arr, n);
  63.  
  64. }
  65.  
  66. void huffman(char arr[], int freq[], int n, priority_queue<Node*, vector<Node*>, compare> Q) {
  67.  
  68. struct Node* left;
  69. struct Node* right;
  70. struct Node* z;
  71.  
  72. // "min-priority queue, Q, keyed on the freq atributes"
  73.  
  74. for (int i = 0; i < n; i++) {
  75.  
  76. Q.push(new Node(arr[i], freq[i]));
  77. }
  78.  
  79. for (int i = n; i > 1; i--) {
  80.  
  81. //Extract Min Q
  82. left = Q.top();
  83. Q.pop(); //remove from queue
  84.  
  85. //Extract Min Q
  86. right = Q.top();
  87. Q.pop(); //remove from queue
  88.  
  89. z = new Node(' ', left->freq + right->freq);
  90.  
  91. z->left = left;
  92. z->right = right;
  93. Q.push(z); //insert the added values into the queue
  94.  
  95. }
  96.  
  97. printCode(Q.top(), "", arr, n);
  98.  
  99. for (int i = 0; i < n; i++) {
  100. cout << arr[i] << ":" << s[i] << endl;
  101. }
  102.  
  103. }
  104.  
  105. int main() {
  106.  
  107. // "input consists of 6 integers"
  108. int n = 6;
  109. char arr[] = { 'A', 'B', 'C', 'D', 'E', 'F' };
  110.  
  111. //created an array with the given outputs for easy testing
  112. //int freq[] = { 34787, 28342, 39894, 28982, 19899, 12998 };
  113.  
  114. int* freq = new int[6];
  115.  
  116. for (int i = 0; i < n; i++) {
  117. cin >> freq[i];
  118. }
  119.  
  120. //page 431 mentions using a priority queue
  121. priority_queue<Node*, vector<Node*>, compare> Q;
  122.  
  123. huffman(arr, freq, n, Q);
  124.  
  125. return 0;
  126.  
  127. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement