Advertisement
Ser1ousSAM

SetDM

Sep 14th, 2022
46
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.48 KB | None | 0 0
  1. #include <iostream>
  2. #include <algorithm>
  3. #include <string>
  4. #include <vector>
  5.  
  6. template<typename T>
  7. struct SetNode {
  8. T data;
  9. SetNode* left;
  10. SetNode* right;
  11.  
  12. public:
  13. int containsNode(SetNode* root, T data) {
  14. if (root == NULL)
  15. return 0;
  16. int x = root->data == data ? 1 : 0;
  17. return x | containsNode(root->left, data) | containsNode(root->right, data);
  18. }
  19. SetNode* insert(SetNode* root, T data) {
  20. if (root == NULL) {
  21. SetNode<T>* tmp = new SetNode<T>;
  22. tmp->data = data;
  23. tmp->left = tmp->right = NULL;
  24. return tmp;
  25. }
  26. if (data < root->data) {
  27. root->left = insert(root->left, data);
  28. return root;
  29. }
  30. else if (data > root->data) {
  31. root->right = insert(root->right, data);
  32. return root;
  33. }
  34. else
  35. return root;
  36. }
  37. SetNode* remove(int x, SetNode* t) {
  38. SetNode* temp;
  39. if (t == NULL)
  40. return NULL;
  41. else if (x < t->data)
  42. t->left = remove(x, t->left);
  43. else if (x > t->data)
  44. t->right = remove(x, t->right);
  45. else {
  46. temp = t;
  47. if (t->left == NULL)
  48. t = t->right;
  49. else if (t->right == NULL)
  50. t = t->left;
  51. delete temp;
  52. }
  53. return t;
  54. }
  55. void inorder(SetNode* r) {
  56. if (r == NULL) {
  57. return;
  58. }
  59. inorder(r->left);
  60. std::cout << r->data << " ";
  61. inorder(r->right);
  62. }
  63.  
  64. };
  65.  
  66. template<typename T>
  67. class Set {
  68. private:
  69. static Set<T> U;//static - единственный объект для всех членов класса.
  70. public:
  71. std::vector<T> array;
  72. SetNode<T>* root;
  73. int size;
  74. Set() {
  75. root = NULL;
  76. size = 0;
  77. }
  78. Set(const Set& s) {
  79. root = s.root;
  80. size = s.size;
  81. array = s.array;
  82. }
  83. Set(std::vector<T>& vect) {
  84. root = NULL;
  85. size = vect.size();
  86. array = vect;
  87. }
  88. void setU(Set<T> data) {
  89. U = data;
  90. }
  91. Set<T> getU() {
  92. return U;
  93. }
  94. void add(T data) {
  95. if (!root->containsNode(root, data)) {
  96. root = root->insert(root, data);
  97. size++;
  98. array.push_back(data);
  99. std::sort(array.begin(), array.end());
  100. }
  101. }
  102. void remove(T data) {
  103. if (root->containsNode(root, data)) {
  104. root = root->remove(data, root);
  105. size--;
  106. for (int i = 0; i < array.size(); ++i) {
  107. if (array[i] == data) {
  108. array.erase(array.begin() + i);
  109. break;
  110. }
  111. }
  112. }
  113. }
  114. Set inUnion(Set& other) {
  115. Set<T> unitedSet;
  116. std::vector<T> ans;
  117. int i = 0;
  118. int j = 0;
  119. int n = size;
  120. int m = other.size;
  121. if (n < m)
  122. std::swap(n, m);
  123. while (i != n && j != m) {
  124. while (array[i] > other.array[j] && j != m) {
  125. ans.push_back(other.array[j]);
  126. j++;
  127. }
  128. while (array[i] < other.array[j] && i != n) {
  129. ans.push_back(array[i]);
  130. i++;
  131. }
  132. if (array[i] == other.array[j]) {
  133. ans.push_back(array[i]);
  134. i++;
  135. j++;
  136. }
  137. }
  138. while (i != n) {
  139. ans.push_back(array[i]);
  140. i++;
  141. }
  142. while (j != m) {
  143. ans.push_back(other.array[j]);
  144. j++;
  145. }
  146. for (size_t k = 0; k < ans.size(); ++k)
  147. unitedSet.add(ans[k]);
  148. return unitedSet;
  149. }
  150. Set intersect(Set& other) {
  151. Set<T> intersectedSet;
  152. for (int i = 0; i < size; i++) {
  153. for (int j = 0; j < other.size; j++) {
  154. if (array[i] == other.array[j])
  155. intersectedSet.add(array[i]);
  156. }
  157. }
  158. return intersectedSet;
  159. }
  160. bool contains(T data) {
  161. return root->containsNode(root, data) ? true : false;
  162. }
  163.  
  164. void display() {
  165. std::cout << "{ ";
  166. root->inorder(root);
  167. std::cout << "}\n";
  168. }
  169. //Union
  170. Set<T> operator + (Set<T>& X) {
  171. Set<T> res;
  172. for (const auto i : this->array)
  173. res.add(i);
  174. for (const auto i : X.array)
  175. res.add(i);
  176. return res;
  177. }
  178. //Intersection
  179. Set<T> operator * (Set<T>& X) {
  180. Set<T> res;
  181. for (const auto i : this->array)
  182. if (X.contains(i))
  183. res.add(i);
  184. }
  185.  
  186. bool isEmpty()
  187. {
  188. return array.empty();
  189. }
  190. //Addition
  191. Set<T> operator ~ () {
  192. Set<T> res;
  193. for (const auto i : this->array)
  194. if (!U.contains(i))
  195. res.add(i);
  196. }
  197. //Difference
  198. Set<T> operator / (Set<T>& X) {
  199.  
  200. Set<T> tmp = *this;
  201.  
  202. for (const auto i : tmp.array)
  203. if (X.contains(i))
  204. tmp.remove(i);
  205.  
  206. return tmp;
  207. }
  208. //Simmetric difference
  209. Set<T> operator || (Set<T>& X) {
  210. return (this / X) + (X / this);
  211. }
  212. };
  213.  
  214. int main() {
  215. Set<int> A;
  216. A.add(1);
  217. A.add(2);
  218. A.add(3);
  219. A.add(4);
  220. A.add(5);
  221. std::vector<int> c = { 1,2,3,4,5,6,7,12,10,23 };
  222. A.setU(c);
  223. A.getU().display();
  224. A.display();
  225. Set<std::string> B;
  226. B.add("fewf");
  227. B.add("ffffff");
  228. B.add("aaaaaaa");
  229. B.add(",,,,,,,,,,,");
  230. B.display();
  231. std::cout << std::endl;
  232.  
  233. std::cout << std::endl;
  234. }
  235.  
  236.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement