Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- #include <string>
- #define INPUT_FILE "input.txt"
- #define OUTPUT_FILE "output.txt"
- using namespace std;
- fstream infile, outfile;
- template <class H>
- struct Nodo
- {
- H val;
- Nodo<H>* padre;
- Nodo<H>* sinistro;
- Nodo<H>* destro;
- };
- template <class H>
- class BST
- {
- private:
- Nodo<H>* radice;
- public:
- BST() : radice(NULL) {}
- void insert(H valore);
- void trapianta(Nodo<H>* u, Nodo<H>* v);
- Nodo<H>* find(H x);
- void cancella(Nodo<H>* z);
- Nodo<H>* minimo(Nodo<H>* x);
- Nodo<H>* _succ(Nodo<H>* aux);
- H succ(H valore);
- };
- template <class H>
- void BST<H>::insert(H valore)
- {
- Nodo<H>* nuovo = new Nodo<H>;
- Nodo<H>* x = radice, *y = NULL;
- nuovo -> val = valore;
- nuovo -> sinistro = nuovo -> destro = NULL;
- while (x!=NULL)
- {
- y = x;
- if (valore < x->val)
- x = x->sinistro;
- else
- x = x->destro;
- }
- nuovo -> padre = y;
- if (y==NULL)
- radice = nuovo;
- else if (valore < y->val)
- y -> sinistro = nuovo;
- else
- y -> destro = nuovo;
- }
- template <class H>
- void BST<H>::trapianta(Nodo<H>* u, Nodo<H>* v)
- {
- if (u->padre==NULL)
- radice = v;
- else if (u==u->padre->sinistro)
- u->padre->sinistro = v;
- else
- u-> padre -> destro = v;
- if (v!=NULL)
- v->padre = u->padre;
- }
- template <class H>
- Nodo<H>* BST<H>::minimo(Nodo<H>* x)
- {
- Nodo<H>* min = x;
- while (min->sinistro != NULL)
- min = min->sinistro;
- return min;
- }
- template <class H>
- void BST<H>::cancella(Nodo<H>* z)
- {
- Nodo<H>* y;
- if (z->sinistro == NULL)
- trapianta(z, z->destro);
- else if (z->destro == NULL)
- trapianta(z, z->sinistro);
- else
- {
- y = minimo(z->destro);
- if (y->padre!=z)
- {
- trapianta(y, y->destro);
- y->destro = z->destro;
- y->destro->padre = y;
- }
- trapianta(z, y);
- y->sinistro = z->sinistro;
- y->sinistro->padre = y;
- }
- delete z;
- }
- template <class H>
- Nodo<H>* BST<H>::find(H x)
- {
- Nodo<H>* iter = radice;
- while ((iter!=NULL)&&(x!=iter->val))
- {
- if (x<iter->val)
- iter = iter->sinistro;
- else
- iter = iter -> destro;
- }
- return iter;
- }
- template <class H>
- Nodo<H>* BST<H>::_succ(Nodo<H>* aux)
- {
- if (aux==0)
- return 0;
- if (aux->destro)
- return minimo(aux->destro);
- }
- template <class H>
- H BST<H>::succ(H val)
- {
- Nodo<H>* aux = _succ(find(val));
- if (aux)
- return aux->val;
- else
- return -1;
- }
- int getDuePunti(string x)
- {
- for (int i=0; i<x.length(); i++)
- if (x[i]==':')
- return i;
- return 0;
- }
- string getOp(string x)
- {
- return x.substr(0, getDuePunti(x));
- }
- string _getValore(string x)
- {
- return x.substr(getDuePunti(x)+1, x.length());
- }
- int main()
- {
- infile.open(INPUT_FILE, fstream::in);
- outfile.open(OUTPUT_FILE, fstream::out);
- for (int k=0; k<100; k++)
- {
- string tipo;
- int N, M;
- infile >> tipo >> N >> M;
- if (tipo=="int")
- {
- BST<int> tree;
- for (int j=0; j<N; j++)
- {
- string tmp, _valore, op;
- int x;
- infile >> tmp;
- op = getOp(tmp);
- _valore = _getValore(tmp);
- x = stoi(_valore);
- if (op=="ins")
- tree.insert(x);
- else
- {
- Nodo<int>* elem = tree.find(x);
- tree.cancella(elem);
- }
- }
- for (int j=0; j<M; j++)
- {
- int chiave;
- infile >> chiave;
- int successore = tree.succ(chiave);
- outfile << successore << " ";
- }
- }
- else
- {
- BST<double> tree;
- for (int j=0; j<N; j++)
- {
- string tmp, _valore, op;
- double x;
- infile >> tmp;
- op = getOp(tmp);
- _valore = _getValore(tmp);
- x = stod(_valore);
- if (op=="ins")
- tree.insert(x);
- else
- {
- Nodo<double>* elem = tree.find(x);
- tree.cancella(elem);
- }
- }
- for (int j=0; j<M; j++)
- {
- int chiave;
- infile >> chiave;
- int successore = tree.succ(chiave);
- outfile << successore << " ";
- }
- }
- outfile << endl;
- }
- cout << "end of program, exit" << endl;
- infile.close();
- outfile.close();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement