Advertisement
Guest User

Untitled

a guest
Jan 23rd, 2020
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.34 KB | None | 0 0
  1. #pragma once
  2. #include <iostream>
  3. using namespace std;
  4.  
  5. class map_pair {
  6. public:
  7. int key; // ключ
  8. int data; // значение
  9.  
  10. map_pair() {
  11.  
  12. }
  13.  
  14. map_pair(int key, int s) {
  15. this->key = key;
  16. this->data = s;
  17. }
  18.  
  19. //Сравнение ключей (<= >=) для создания мультимепа
  20. bool operator >(map_pair p) {
  21. return this->key >= p.key;
  22. }
  23.  
  24. bool operator <(map_pair& p) {
  25. return this->key <= p.key;
  26. }
  27.  
  28. friend ostream& operator <<(ostream& o, map_pair& p);
  29. };
  30. //для вывода элементов
  31. ostream& operator <<(ostream& o, map_pair& p) {
  32. o << "{" << p.key << ", " << p.key << "} " << endl;
  33. return o;
  34. }
  35.  
  36. template <typename T>
  37. class map {
  38. public:
  39. class node {
  40. public:
  41. node* right;
  42. node* left;
  43. map_pair key;
  44.  
  45. node() {
  46. right = nullptr;
  47. left = nullptr;
  48. }
  49.  
  50. node(map_pair& key) {
  51. this->key = key;
  52. right = nullptr;
  53. left = nullptr;
  54. }
  55. };
  56.  
  57. node* root;
  58.  
  59. map() {
  60. root = nullptr;
  61. }
  62.  
  63. void Push(node* parent, map_pair& key) {
  64. if (key > parent->key) {
  65. if (parent->right == nullptr) {
  66. parent->right = new node(key);
  67. }
  68. else {
  69. push(parent->right, key);
  70. }
  71. }
  72. else {
  73. if (key < parent->key) {
  74. if (parent->left == nullptr) {
  75. parent->left = new node(key);
  76. }
  77. else {
  78. push(parent->left, key);
  79. }
  80. }
  81. }
  82. }
  83.  
  84. void Push(map_pair& key) {
  85. if (root == nullptr)
  86. root = new node(key);
  87. else
  88. push(root, key);
  89. }
  90.  
  91. void Print(node* parent) {
  92. if (parent->right != nullptr)
  93. print(parent->right);
  94.  
  95. cout << parent->key;
  96.  
  97. if (parent->left != nullptr)
  98. print(parent->left);
  99. }
  100.  
  101. void Print() {
  102. print(root);
  103. }
  104.  
  105. static node* findl(node* parent) {
  106. node* n = parent;
  107.  
  108. while (n->left != NULL) {
  109. n = n->left;
  110. }
  111.  
  112. return n;
  113. }
  114.  
  115. static node* findr(node* parent) {
  116. node* n = parent;
  117.  
  118. while (n->right != NULL) {
  119. n = n->right;
  120. }
  121.  
  122. return n;
  123. }
  124.  
  125. class iterator {
  126. public:
  127. node* current;
  128. stack<node*> s;
  129.  
  130. iterator(node* n) {
  131. current = n;
  132. }
  133.  
  134. void findn() {
  135. if (current->right != NULL) {
  136. s.push(current);
  137. current = map::findl(current->right);
  138. }
  139. else {
  140. node* parent = current->parent;
  141.  
  142. if (!s.empty()) {
  143.  
  144. node* top = s.top();
  145. while (top == parent) {
  146. parent = top->parent;
  147. s.pop();
  148. if (s.empty())
  149. break;
  150. top = s.top();
  151. }
  152.  
  153. current = parent;
  154. }
  155. }
  156. }
  157.  
  158. bool operator !=(iterator other) {
  159. return current != other.current;
  160. }
  161. iterator operator++() {
  162. findn();
  163. return *this;
  164. }
  165.  
  166. iterator operator++(int) {
  167. findn();
  168. return *this;
  169. }
  170.  
  171. T operator *() {
  172. return current->key;
  173. }
  174. };
  175.  
  176. iterator begin() {
  177. return iterator(findl(root));
  178. }
  179.  
  180. iterator end() {
  181. return iterator(NULL);
  182. }
  183. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement