Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <algorithm>
- #include <string>
- #include <vector>
- template<typename T>
- struct SetNode {
- T data;
- SetNode* left;
- SetNode* right;
- public:
- int containsNode(SetNode* root, T data) {
- if (root == NULL)
- return 0;
- int x = root->data == data ? 1 : 0;
- return x | containsNode(root->left, data) | containsNode(root->right, data);
- }
- SetNode* insert(SetNode* root, T data) {
- if (root == NULL) {
- SetNode<T>* tmp = new SetNode<T>;
- tmp->data = data;
- tmp->left = tmp->right = NULL;
- return tmp;
- }
- if (data < root->data) {
- root->left = insert(root->left, data);
- return root;
- }
- else if (data > root->data) {
- root->right = insert(root->right, data);
- return root;
- }
- else
- return root;
- }
- SetNode* remove(int x, SetNode* t) {
- SetNode* temp;
- if (t == NULL)
- return NULL;
- else if (x < t->data)
- t->left = remove(x, t->left);
- else if (x > t->data)
- t->right = remove(x, t->right);
- else {
- temp = t;
- if (t->left == NULL)
- t = t->right;
- else if (t->right == NULL)
- t = t->left;
- delete temp;
- }
- return t;
- }
- void inorder(SetNode* r) {
- if (r == NULL) {
- return;
- }
- inorder(r->left);
- std::cout << r->data << " ";
- inorder(r->right);
- }
- };
- template<typename T>
- class Set {
- private:
- static Set<T> U;//static - единственный объект для всех членов класса.
- public:
- std::vector<T> array;
- SetNode<T>* root;
- int size;
- Set() {
- root = NULL;
- size = 0;
- }
- Set(const Set& s) {
- root = s.root;
- size = s.size;
- array = s.array;
- }
- Set(std::vector<T>& vect) {
- root = NULL;
- size = vect.size();
- array = vect;
- }
- void setU(Set<T> data) {
- U = data;
- }
- Set<T> getU() {
- return U;
- }
- void add(T data) {
- if (!root->containsNode(root, data)) {
- root = root->insert(root, data);
- size++;
- array.push_back(data);
- std::sort(array.begin(), array.end());
- }
- }
- void remove(T data) {
- if (root->containsNode(root, data)) {
- root = root->remove(data, root);
- size--;
- for (int i = 0; i < array.size(); ++i) {
- if (array[i] == data) {
- array.erase(array.begin() + i);
- break;
- }
- }
- }
- }
- Set inUnion(Set& other) {
- Set<T> unitedSet;
- std::vector<T> ans;
- int i = 0;
- int j = 0;
- int n = size;
- int m = other.size;
- if (n < m)
- std::swap(n, m);
- while (i != n && j != m) {
- while (array[i] > other.array[j] && j != m) {
- ans.push_back(other.array[j]);
- j++;
- }
- while (array[i] < other.array[j] && i != n) {
- ans.push_back(array[i]);
- i++;
- }
- if (array[i] == other.array[j]) {
- ans.push_back(array[i]);
- i++;
- j++;
- }
- }
- while (i != n) {
- ans.push_back(array[i]);
- i++;
- }
- while (j != m) {
- ans.push_back(other.array[j]);
- j++;
- }
- for (size_t k = 0; k < ans.size(); ++k)
- unitedSet.add(ans[k]);
- return unitedSet;
- }
- Set intersect(Set& other) {
- Set<T> intersectedSet;
- for (int i = 0; i < size; i++) {
- for (int j = 0; j < other.size; j++) {
- if (array[i] == other.array[j])
- intersectedSet.add(array[i]);
- }
- }
- return intersectedSet;
- }
- bool contains(T data) {
- return root->containsNode(root, data) ? true : false;
- }
- void display() {
- std::cout << "{ ";
- root->inorder(root);
- std::cout << "}\n";
- }
- //Union
- Set<T> operator + (Set<T>& X) {
- Set<T> res;
- for (const auto i : this->array)
- res.add(i);
- for (const auto i : X.array)
- res.add(i);
- return res;
- }
- //Intersection
- Set<T> operator * (Set<T>& X) {
- Set<T> res;
- for (const auto i : this->array)
- if (X.contains(i))
- res.add(i);
- }
- bool isEmpty()
- {
- return array.empty();
- }
- //Addition
- Set<T> operator ~ () {
- Set<T> res;
- for (const auto i : this->array)
- if (!U.contains(i))
- res.add(i);
- }
- //Difference
- Set<T> operator / (Set<T>& X) {
- Set<T> tmp = *this;
- for (const auto i : tmp.array)
- if (X.contains(i))
- tmp.remove(i);
- return tmp;
- }
- //Simmetric difference
- Set<T> operator || (Set<T>& X) {
- return (this / X) + (X / this);
- }
- };
- int main() {
- Set<int> A;
- A.add(1);
- A.add(2);
- A.add(3);
- A.add(4);
- A.add(5);
- std::vector<int> c = { 1,2,3,4,5,6,7,12,10,23 };
- A.setU(c);
- A.getU().display();
- A.display();
- Set<std::string> B;
- B.add("fewf");
- B.add("ffffff");
- B.add("aaaaaaa");
- B.add(",,,,,,,,,,,");
- B.display();
- std::cout << std::endl;
- std::cout << std::endl;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement