Advertisement
Guest User

Untitled

a guest
Jan 24th, 2020
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.54 KB | None | 0 0
  1.  
  2. #include <iostream>
  3. #include <stack>
  4. using namespace std;
  5.  
  6.  
  7.  
  8.  
  9. class map {
  10. public:
  11.  
  12. class node {
  13. public:
  14.  
  15. node* right;
  16.  
  17. node* left;
  18.  
  19. node* parent;
  20.  
  21. int width;
  22. int key;
  23. int presure;
  24.  
  25. node(int key, node* parent, int width)
  26. : right(nullptr), left(nullptr), parent(parent), key(key), width(width) {}
  27. };
  28.  
  29. node* root;
  30.  
  31. map()
  32. :root(nullptr) {}
  33.  
  34. void push(int key, node* parent, int width)
  35. {
  36. if (key > parent->key)
  37. {
  38. if (parent->right == nullptr)
  39. {
  40. parent->right = new node(key, parent, width);
  41. }
  42. else
  43. {
  44. push(key, parent->right, width);
  45. }
  46. }
  47. else if (key < parent->key)
  48. {
  49. if (parent->left == nullptr)
  50. {
  51. parent->left = new node(key, parent, width);
  52. }
  53. else
  54. {
  55. push(key, parent->left, width);
  56. }
  57. }
  58. }
  59.  
  60. void push(int key, int width)
  61. {
  62. if (root == nullptr)
  63. {
  64. root = new node(key, nullptr, width);
  65. }
  66. else
  67. {
  68. push(key, root, width);
  69. }
  70. }
  71.  
  72. static node* findLeft(node* parent)
  73. {
  74. node* temp = parent;
  75.  
  76. while (temp->left)
  77. {
  78. temp = temp->left;
  79. }
  80. return temp;
  81. }
  82.  
  83. static node* findRight(node* parent)
  84. {
  85. node* temp = parent;
  86.  
  87. while (temp->right)
  88. {
  89. temp = temp->right;
  90. }
  91. return temp;
  92. }
  93.  
  94. class iterator {
  95. public:
  96.  
  97. node* current;
  98.  
  99. std::stack<node*> stack;
  100.  
  101. std::stack<node*> stack_minus;
  102.  
  103. iterator(node* n)
  104. : current(n) {}
  105.  
  106. node operator *()
  107. {
  108. if (current->width > 20) {
  109. current->presure = 50;
  110. }
  111. else {
  112. current->presure = 35;
  113. }
  114. return *current;
  115. }
  116.  
  117. bool operator !=(iterator other)
  118. {
  119. return current != other.current;
  120. }
  121.  
  122. void findNode()
  123. {
  124. if (current->right)
  125. {
  126. stack.push(current);
  127.  
  128. current = map::findLeft(current->right);
  129. }
  130. else
  131. {
  132. current = current->parent;
  133.  
  134. if (!stack.empty())
  135. {
  136. while (current == stack.top())
  137. {
  138. current = stack.top()->parent;
  139.  
  140. stack.pop();
  141.  
  142. if (stack.empty())
  143. {
  144. break;
  145. }
  146. }
  147. }
  148. }
  149. }
  150.  
  151. void findNode_Minus()
  152. {
  153. if (current->left)
  154. {
  155. stack_minus.push(current);
  156.  
  157. current = map::findRight(current->left);
  158. }
  159. else
  160. {
  161. current = current->parent;
  162.  
  163. if (!stack_minus.empty())
  164. {
  165. while (current == stack_minus.top())
  166. {
  167. current = stack_minus.top()->parent;
  168.  
  169. stack_minus.pop();
  170.  
  171. if (stack_minus.empty())
  172. {
  173. break;
  174. }
  175. }
  176. }
  177. }
  178. }
  179.  
  180. iterator& operator ++()
  181. {
  182. findNode();
  183.  
  184. return *this;
  185. }
  186.  
  187. iterator& operator ++(int)
  188. {
  189. findNode();
  190.  
  191. return *this;
  192. }
  193.  
  194. iterator& operator --()
  195. {
  196. findNode_Minus();
  197.  
  198. return *this;
  199. }
  200.  
  201. iterator& operator --(int)
  202. {
  203. findNode_Minus();
  204.  
  205. return *this;
  206. }
  207. };
  208.  
  209. iterator begin()
  210. {
  211. return iterator(findLeft(root));
  212. }
  213.  
  214. iterator end()
  215. {
  216. return iterator(findRight(root));
  217. }
  218. };
  219.  
  220. int main()
  221. {
  222.  
  223. map s1;
  224. /*
  225. s1.push(1, 120);
  226. s1.push(2, 40);
  227. s1.push(3, 300);
  228. s1.push(4, 11);
  229. */
  230. int w;
  231. for (int i = 1; i < 5; i++) {
  232. cout << "Enter width" << endl;
  233. cin >> w;
  234. s1.push(i, w);
  235. }
  236. for (map::iterator i = s1.begin(); i != NULL; i++)
  237. {
  238. map::node n = *i;
  239.  
  240.  
  241. cout << " key: " << n.key << " width: " << n.width << " pressure: " << n.presure << endl;
  242. }
  243.  
  244.  
  245. for (map::iterator i = s1.end(); i != NULL; i--)
  246. {
  247. map::node n = *i;
  248.  
  249.  
  250. cout << " key: " << n.key << " width: " << n.width << " pressure: " << n.presure << endl;
  251. }
  252.  
  253. return 0;
  254. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement