Advertisement
Guest User

Untitled

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