Advertisement
Guest User

Untitled

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