Guest User

Untitled

a guest
Feb 23rd, 2018
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.30 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <vector>
  4. #include <queue>
  5.  
  6.  
  7. using namespace std;
  8.  
  9. struct comparator1 {
  10. bool operator()(int i, int j) {
  11. return i > j;
  12. }
  13. };
  14.  
  15. struct Node {
  16.  
  17. string code;
  18. int value;
  19. Node * left;
  20. Node * right;
  21.  
  22. // компаратор
  23. bool operator() (const Node& x, const Node& y) const {
  24. return x.value > y.value;
  25. }
  26.  
  27. // конструктор по умолчанию нужен для создания объекта-компаратора
  28. Node (int value = 0, Node * left = nullptr, Node * right = nullptr) {
  29. this->value = value; // множество символом узла
  30. this->code = ""; // строковое представление битового кода узла
  31. this->left = left; // левый ребенок
  32. this->right = right; // правый ребенок
  33. }
  34.  
  35. Node (const Node& cnode) {
  36. this->value = cnode.value; // множество символом узла
  37. this->code = cnode.code; // строковое представление битового кода узла
  38. this->left = cnode.left; // левый ребенок
  39. this->right = cnode.right; // правый ребенок
  40. }
  41.  
  42. // объединение деревьев
  43. Node join (Node *x) {
  44. return *new Node(x->value + value, x, this);
  45. }
  46.  
  47. void traversal_code(string code) {
  48. this->code = code;
  49. if (left != nullptr || right != nullptr) {
  50. left->traversal_code(code + "1");
  51. right->traversal_code(code + "0");
  52. }
  53. }
  54.  
  55. };
  56.  
  57.  
  58. class Huffman
  59. {
  60. public:
  61.  
  62. std::vector<string> codes;
  63. priority_queue<Node, std::vector<Node>, Node> tree;
  64. std::vector<Node> nodesWithCodes;
  65.  
  66. Node root;
  67.  
  68.  
  69. void build()
  70. {
  71. // запускает алгоритм (после того как были добавлены все элементы)
  72. while (tree.size() > 1) {
  73. Node n1 = tree.top();
  74. tree.pop();
  75. Node n2 = tree.top();
  76. Node node = n1.join(n2);
  77. tree.push(node);
  78. tree.pop();
  79. }
  80.  
  81. root = tree.top();
  82. root.traversal_code("");
  83.  
  84. }
  85.  
  86. void addChance(int chance)
  87. {
  88. // добавляет элемент в список (дерево, все зависит от реализации)
  89. nodesWithCodes.push_back(*new Node(chance));
  90. tree.push(nodesWithCodes[nodesWithCodes.size()-1]);
  91.  
  92. codes.resize(nodesWithCodes.size());
  93.  
  94. }
  95.  
  96. string get(int i)
  97. {
  98. // выдает битовый код i символа
  99. return nodesWithCodes[i].code;
  100. }
  101.  
  102. };
  103.  
  104.  
  105. int main()
  106. {
  107.  
  108. int n;
  109. Huffman *huff = new Huffman();
  110. fstream fin;
  111. fin.open("/Users/Margarita/CLionProjects/HT3/input.txt", ios::in);
  112. if (fin.is_open())
  113. {
  114. fin >> n;
  115. for (int i = 0; i < n; i++)
  116. {
  117. int x;
  118. fin >> x;
  119. huff->addChance(x);
  120. }
  121.  
  122. fin.close();
  123.  
  124. huff->build();
  125. fstream fout;
  126. fout.open("/Users/Margarita/CLionProjects/HT3/output.txt", ios::out);
  127. for (int i = 0; i < n; i++)
  128. {
  129. fout << huff->get(i) << (i == n - 1 ? "" : " ");
  130. }
  131. fout.close();
  132. delete huff;
  133.  
  134. }
  135.  
  136. return 0;
  137.  
  138. }
Add Comment
Please, Sign In to add comment