Advertisement
Sanlover

Untitled

Oct 8th, 2020
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.67 KB | None | 0 0
  1. #include <vector>
  2. #include <string>
  3. #include <iostream>
  4. #include <map>
  5. #include <algorithm>
  6. using namespace std;
  7.  
  8. #define COUNT 10
  9.  
  10. struct Node
  11. {
  12. int value;
  13. char symbol;
  14.  
  15. Node* next, * prev;
  16. Node* left, * right;
  17.  
  18. Node()
  19. {
  20. prev = next = left = right = nullptr;
  21. }
  22.  
  23. Node(int value, char symbol, Node* next, Node* prev)
  24. {
  25. this->value = value;
  26. this->symbol = symbol;
  27. this->next = next;
  28. this->prev = prev;
  29. left = right = nullptr;
  30. };
  31.  
  32. Node(int value, char symbol, Node* next, Node* prev, Node* left, Node* right)
  33. : value(value), symbol(symbol), next(next), prev(prev), left(left), right(right) {};
  34. };
  35.  
  36. class list
  37. {
  38. private:
  39. Node* first;
  40. Node* last;
  41. public:
  42. list();
  43. bool isEmpty();
  44.  
  45. Node* getFirst();
  46. bool hasOnlyOne();
  47. void push(int value, char symbol);
  48. void push(Node* node);
  49. Node* pop();
  50. void sort();
  51. void print();
  52.  
  53. };
  54.  
  55. list::list()
  56. {
  57. first = last = nullptr;
  58. };
  59.  
  60.  
  61.  
  62. void print(Node* root, int space)
  63. {
  64. if (root == nullptr)
  65. return;
  66.  
  67. space += COUNT;
  68.  
  69. print(root->left, space);
  70.  
  71. cout << endl;
  72. for (int i = COUNT; i < space; i++)
  73. cout << " ";
  74. cout << root->value << " (" << root->symbol << ")"<< "\n";
  75.  
  76. print(root->right, space);
  77. }
  78.  
  79.  
  80. bool compareFunction(Node* a, Node* b)
  81. {
  82. return a->value < b->value;
  83. }
  84.  
  85. map<char, vector<int>> table;
  86. vector<int> code;
  87.  
  88. void BuildTable(Node* root);
  89.  
  90. int main()
  91. {
  92. vector<int> repeats;// создаем переменную типа x
  93. string str = "it is my striiiiiiing!!!!!";
  94. map<char, int> m;
  95. for (int i = 0; i < str.length(); i++)
  96. {
  97. if(str[i] != ' ')
  98. m[str[i]]++;
  99. }
  100.  
  101. vector<Node*> toSort;
  102. list myList;
  103. for (map<char, int>::iterator it = m.begin(); it != m.end(); ++it)
  104. {
  105. myList.push(it->second, it->first);
  106. }
  107.  
  108. while (!myList.hasOnlyOne())
  109. {
  110. myList.sort();
  111. Node* rSon = myList.pop();
  112. Node* lSon = myList.pop();
  113.  
  114. int value = rSon->value + lSon->value;
  115. char symbol = ' ';
  116.  
  117.  
  118. Node* parent = new Node(value, symbol, nullptr, nullptr, lSon, rSon);
  119.  
  120. myList.push(parent);
  121. }
  122.  
  123. Node* root = myList.getFirst();
  124.  
  125. BuildTable(root);
  126.  
  127. for (auto it : table)
  128. {
  129. cout << endl;
  130. cout << "Symbol = " << it.first << " | Code = ";
  131. for (size_t i = 0; i < it.second.size(); i++)
  132. cout << it.second[i];
  133. }
  134.  
  135. cout << endl;
  136. print(root,0);
  137. return 0;
  138. }
  139.  
  140. void BuildTable(Node* root)
  141. {
  142. if (root->symbol != ' ')
  143. table[root->symbol] = code;
  144.  
  145. if (root->left != NULL)
  146. {
  147. code.push_back(0);
  148. BuildTable(root->left);
  149. }
  150.  
  151. if (root->right != NULL)
  152. {
  153. code.push_back(1);
  154. BuildTable(root->right);
  155. }
  156.  
  157. if (root->left == NULL && root->right == NULL)
  158. if(root->symbol != ' ')
  159. table[root->symbol] = code;
  160.  
  161.  
  162. if(!code.empty())
  163. code.pop_back();
  164. }
  165.  
  166. bool
  167. list::isEmpty()
  168. {
  169. return first == nullptr;
  170. }
  171.  
  172. Node* list::getFirst()
  173. {
  174. return first;
  175. }
  176.  
  177. bool
  178. list::hasOnlyOne()
  179. {
  180. return first == last;
  181. };
  182.  
  183. void
  184. list::push(int value, char symbol)
  185. {
  186.  
  187. if (isEmpty())
  188. {
  189. Node* newNode = new Node(value, symbol, nullptr, nullptr);
  190. first = last = newNode;
  191.  
  192. }
  193. else
  194. {
  195. if (first == last)
  196. {
  197. Node* newNode = new Node(value, symbol, nullptr, first);
  198. last = newNode;
  199. first->next = last;
  200. }
  201. else
  202. {
  203. Node* newNode = new Node(value, symbol, nullptr, last);
  204. last->next = newNode;
  205. last = last->next;
  206. }
  207.  
  208. }
  209.  
  210. }
  211.  
  212. void
  213. list::push(Node* node)
  214. {
  215.  
  216. if (isEmpty())
  217. {
  218. Node* newNode = new Node(node->value, node->symbol, nullptr, nullptr, node->left, node->right);
  219. first = last = newNode;
  220.  
  221. }
  222. else
  223. {
  224. if (first == last)
  225. {
  226. Node* newNode = new Node(node->value, node->symbol, nullptr, first, node->left, node->right);
  227. last = newNode;
  228. first->next = last;
  229. }
  230. else
  231. {
  232. Node* newNode = new Node(node->value, node->symbol, nullptr, last, node->left, node->right);
  233. last->next = newNode;
  234. last = last->next;
  235. }
  236.  
  237. }
  238. }
  239.  
  240. Node*
  241. list::pop()
  242. {
  243. if (isEmpty())
  244. throw exception("List is empty");
  245.  
  246. if (first == last)
  247. {
  248. Node* nodeToReturn = new Node(first->value, first->symbol, nullptr, nullptr, last->left, last->right);
  249. delete first;
  250. first = last = nullptr;
  251. return nodeToReturn;
  252. }
  253.  
  254. Node* nodeToReturn = new Node(last->value, last->symbol, nullptr, nullptr, last->left,last->right);
  255. Node* toDelete = last;
  256. last = last->prev;
  257. delete toDelete;
  258.  
  259. return nodeToReturn;
  260. }
  261.  
  262. void
  263. list::sort()
  264. {
  265. Node* temp = first;
  266. while (temp->next != nullptr)
  267. {
  268. if (temp->value < temp->next->value)
  269. {
  270. swap(temp->value, temp->next->value);
  271. swap(temp->symbol, temp->next->symbol);
  272. swap(temp->left , temp->next->left);
  273. swap(temp->right, temp->next->right);
  274. temp = temp->next;
  275. sort();
  276. }
  277. else
  278. temp = temp->next;
  279.  
  280. }
  281. }
  282.  
  283. void list::print()
  284. {
  285. Node* it = first;
  286.  
  287. cout << endl;
  288. while (it != nullptr)
  289. {
  290. cout << it->value << "-[" << it->symbol << "] ";
  291. it = it->next;
  292. }
  293. }
  294.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement