From 6c6328375c55683645146909b7ab760d0de0d463 Mon Sep 17 00:00:00 2001 From: Santo Cariotti Date: Sun, 18 Oct 2020 18:56:43 +0200 Subject: chore: name of first year folder --- .../exercises/exam_20_07_20/ex4.cpp | 293 +++++++++++++++++++++ 1 file changed, 293 insertions(+) create mode 100644 1_anno/Programmazione_2/exercises/exam_20_07_20/ex4.cpp (limited to '1_anno/Programmazione_2/exercises/exam_20_07_20/ex4.cpp') diff --git a/1_anno/Programmazione_2/exercises/exam_20_07_20/ex4.cpp b/1_anno/Programmazione_2/exercises/exam_20_07_20/ex4.cpp new file mode 100644 index 0000000..e234363 --- /dev/null +++ b/1_anno/Programmazione_2/exercises/exam_20_07_20/ex4.cpp @@ -0,0 +1,293 @@ +#include +#include +#include + +using namespace std; + +template +struct node { + T key; + node* prev; + node* right; + node* left; +}; + +template +class bst { +public: + bst() : root{nullptr} , _val{0}{} + + ~bst() { + // TODO + } + + bst* insert(initializer_list&& list) { + for(auto const& i : list) + insert(i); + + return this; + } + + bst* insert(T k) { + node* nodus = new node{k, nullptr, nullptr, nullptr}; + node* iter = root; + node* prev = nullptr; + + while(iter) { + prev = iter; + iter = (k < iter->key) ? iter->left : iter->right; + } + + nodus->prev = prev; + if(!prev) + root = nodus; + else if(k < prev->key) + prev->left = nodus; + else + prev->right = nodus; + + return this; + } + + bst* remove(initializer_list&& list) { + for(auto const& i : list) + remove(i); + + return this; + } + + bst* remove(T k) { + node* nodus = search(k); + if(!nodus) return this; + + if(!nodus->left) { + _transplant(nodus, nodus->right); + } else if(!nodus->right) { + _transplant(nodus, nodus->left); + } else { + node* iter = _min(nodus->right); + if(iter->prev != nodus) { + _transplant(iter, iter->right); + iter->right = nodus->right; + iter->right->prev = iter; + } + _transplant(nodus, iter); + iter->left = nodus->left; + iter->left->prev = iter; + } + + delete nodus; + return this; + } + + node* min() { + return _min(root); + } + + node* min(node* nodus) { + return _min(nodus); + } + + node* max() { + return _max(root); + } + + node* max(node* nodus) { + return _max(nodus); + } + + node* search(T k) { + node* iter = root; + while(iter && iter->key != k) + iter = (iter->key > k) ? iter->left : iter->right; + + return iter; + } + + node* successor(T k) { + node* nodus = search(k); + if(!nodus) return nullptr; + + if(nodus->right) + return min(nodus->right); + + node* prev = nodus->prev; + while(prev && nodus == prev->right) { + nodus = prev; + prev = prev->prev; + } + + return prev; + + } + node* predecessor(T k) { + node* nodus = search(k); + if(!nodus) return nullptr; + + if(nodus->left) + return max(nodus->left); + + node* prev = nodus->prev; + while(prev && nodus == prev->left) { + nodus = prev; + prev = prev->prev; + } + + return prev; + } + + friend ostream& operator<<(ostream& os, bst* tree) { + tree->_inorder(os, tree->root); + return os; + } + T sol(T k) { + auto x = search(k); + auto m = _max(x)->key; + auto mnn = _min(x); + T mn; + if(mnn->right) + mn = _min(mnn->right)->key; + else + mn = mnn->key; + cout << m << ' ' << mn << endl; + return m - mn; + } +private: + int _val; + void _transplant(node* u, node* v) { + if(!u->prev) { + root = v; + } else if(u == u->prev->left) { + u->prev->left = v; + } else { + u->prev->right = v; + } + + if(v) + v->prev = u->prev; + } + + node* _min(node* root) { + node* iter = root; + while(iter && iter->left) + iter = iter->left; + + return iter; + } + + node* _max(node* root) { + node* iter = root; + while(iter && iter->right) + iter = iter->right; + + return iter; + } + + void _inorder(ostream& os, node* root) { + if(root) { + if(root->right && root->left) { + _val++; + } + _inorder(os, root->left); + os << root->key << ' '; + _inorder(os, root->right); + } + } + + void _preorder(ostream& os, node* root) { + if(root) { + os << root->key << ' '; + _inorder(os, root->left); + _inorder(os, root->right); + } + } + + void _postorder(ostream& os, node* root) { + if(root) { + _inorder(os, root->left); + _inorder(os, root->right); + os << root->key << ' '; + } + } + node* root; +}; + +int main() { + ifstream in("input.txt"); + ofstream out("output.txt"); + + for(int ts = 0; ts < 100; ++ts) { + string t, comm; + in >> t; + int n; + switch(t.at(0)) { + case 'd': + { + bst* b = new bst{}; + in >> n; + double k; + for(int i = 0; i < n; ++i) { + in >> comm; + stringstream tcomm(comm); + int ex= 0; + while(getline(tcomm, comm, ':')) { + if(ex == 0) { + if(comm == "ins") + ex = 1; + else + ex = 2; + } else { + if(ex == 1) + b->insert(stod(comm)); + else + b->remove(stod(comm)); + ex = 0; + } + } + } + in >> k; + cout << b << endl; + out << b->sol(k) << endl; + + delete b; + break; + } + case 'i': + { + bst* b = new bst{}; + in >> n; + int k; + for(int i = 0; i < n; ++i) { + in >> comm; + stringstream tcomm(comm); + int ex= 0; + while(getline(tcomm, comm, ':')) { + if(ex == 0) { + if(comm == "ins") + ex = 1; + else + ex = 2; + } else { + if(ex == 1) + b->insert(stoi(comm)); + else + b->remove(stoi(comm)); + ex = 0; + } + } + } + in >> k; + cout << b << endl; + out << b->sol(k) << endl; + + delete b; + break; + } + } + } + + in.close(); + out.close(); + return 0; +} + -- cgit v1.2.3-18-g5258