Advertisement
Guest User

Untitled

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