Advertisement
Guest User

Untitled

a guest
Feb 14th, 2020
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.98 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #define INPUT_FILE "input.txt"
  4. #define OUTPUT_FILE "output.txt"
  5.  
  6. using namespace std;
  7.  
  8. fstream infile, outfile;
  9.  
  10. template <class H>
  11. class Nodo
  12. {
  13. private:
  14. H val;
  15. Nodo<H> *sinistro, *destro, *padre;
  16. public:
  17. Nodo(H v, Nodo<H>* p=0, Nodo<H>* s=0, Nodo<H>* d=0) :
  18. val(v), padre(p), sinistro(s), destro(d) {}
  19. void setVal(H v)
  20. {
  21. val = v;
  22. }
  23. void setLeft(Nodo<H>* s)
  24. {
  25. sinistro = s;
  26. }
  27. void setRight(Nodo<H>* d)
  28. {
  29. destro = d;
  30. }
  31. void setPadre(Nodo<H>* p)
  32. {
  33. padre = p;
  34. }
  35. H getVal()
  36. {
  37. return val;
  38. }
  39. Nodo<H>* getLeft()
  40. {
  41. return sinistro;
  42. }
  43. Nodo<H>* getRight()
  44. {
  45. return destro;
  46. }
  47. Nodo<H>* getPadre()
  48. {
  49. return padre;
  50. }
  51. };
  52.  
  53. template <class H>
  54. class BST
  55. {
  56. private:
  57. Nodo<H>* root;
  58. void _printPreOrder(Nodo<H>* n);
  59. void _printInOrder(Nodo<H>* n);
  60. void _printPostOrder(Nodo<H>* n);
  61. Nodo<H>* _min(Nodo<H>* n);
  62. Nodo<H>* _search(H val);
  63. void _remove(Nodo<H>* n);
  64. public:
  65. BST():root(0) {}
  66. BST<H>* insert(H val);
  67. BST<H>* printInOrder();
  68. BST<H>* printPreOrder();
  69. BST<H>* printPostOrder();
  70. H min();
  71. bool search(H val);
  72. BST<H>* remove(H val);
  73. };
  74.  
  75. template <class H>
  76. BST<H>* BST<H>::insert(H val)
  77. {
  78. if (root==0)
  79. root = new Nodo<H>(val);
  80. else
  81. {
  82. Nodo<H>* aux = root;
  83. bool inserted = false;
  84. while (!inserted)
  85. {
  86. if (val>aux->getVal())
  87. {
  88. if (aux->getRight()==0)
  89. {
  90. aux->setRight(new Nodo<H>(val, aux));
  91. inserted = true;
  92. }
  93. else
  94. aux = aux->getRight();
  95. }
  96. else
  97. {
  98. if (aux->getLeft()==0)
  99. {
  100. aux->setLeft(new Nodo<H>(val, aux));
  101. inserted = true;
  102. }
  103. else
  104. aux = aux->getLeft();
  105. }
  106. }
  107. }
  108. return this;
  109. }
  110.  
  111. template <class H>
  112. void BST<H>::_printPreOrder (Nodo<H>* aux)
  113. {
  114. if (aux!=0)
  115. {
  116. outfile << aux->getVal() << " ";
  117. _printPreOrder(aux->getLeft());
  118. _printPreOrder(aux->getRight());
  119. }
  120.  
  121. }
  122. template <class H>
  123. void BST<H>::_printInOrder (Nodo<H>* aux)
  124. {
  125. if (aux!=0)
  126. {
  127. _printInOrder(aux->getLeft());
  128. outfile << aux->getVal() << " ";
  129. _printInOrder(aux->getRight());
  130. }
  131.  
  132. }
  133. template <class H>
  134. void BST<H>::_printPostOrder (Nodo<H>* aux)
  135. {
  136. if (aux!=0)
  137. {
  138. _printPostOrder(aux->getLeft());
  139. _printPostOrder(aux->getRight());
  140. outfile << aux->getVal() << " ";
  141. }
  142.  
  143. }
  144. template <class H>
  145. BST<H>* BST<H>::printPreOrder()
  146. {
  147. _printPreOrder(root);
  148. return this;
  149. }
  150. template <class H>
  151. BST<H>* BST<H>::printInOrder()
  152. {
  153. _printInOrder(root);
  154. return this;
  155. }
  156. template <class H>
  157. BST<H>* BST<H>::printPostOrder()
  158. {
  159. _printPostOrder(root);
  160. return this;
  161. }
  162.  
  163. template <class H>
  164. Nodo<H>* BST<H>::_min(Nodo<H>* r)
  165. {
  166. if (r==0)
  167. return 0;
  168. while (r->getLeft())
  169. r=r->getLeft();
  170. return r;
  171. }
  172. template <class H>
  173. H BST<H>::min()
  174. {
  175. Nodo<H>* aux = _min(root);
  176. if (aux)
  177. return aux->getVal();
  178. else
  179. return -99999;
  180. }
  181.  
  182. template <class H>
  183. Nodo<H>* BST<H>::_search(H val)
  184. {
  185. Nodo<H>* aux = root;
  186. while (aux != 0)
  187. {
  188. if (val > aux->getVal())
  189. aux = aux->getRight();
  190. else if (val < aux->getVal())
  191. aux = aux->getLeft();
  192. else
  193. return aux;
  194. }
  195. return 0;
  196. }
  197. template <class H>
  198. bool BST<H>::search(H val)
  199. {
  200. if (_search(val)==0)
  201. return false;
  202. else
  203. return true;
  204. }
  205.  
  206. template <class H>
  207. void BST<H>::_remove(Nodo<H>* n)
  208. {
  209. if (n == 0)
  210. return;
  211.  
  212. if (n->getLeft()==0 && n->getRight()==0)
  213. {
  214. Nodo<H>* padre = n->getPadre();
  215. if (padre!=0)
  216. {
  217. if (padre->getRight()==n)
  218. padre -> setRight(0);
  219. else
  220. padre -> setLeft(0);
  221. }
  222. else
  223. root = 0;
  224.  
  225. delete n;
  226. return;
  227. }
  228.  
  229. if (n->getLeft()==0 ^ n->getRight()==0)
  230. {
  231. Nodo<H>* padre = n->getPadre();
  232. if (n->getLeft()!=0)
  233. {
  234. if (padre != 0)
  235. {
  236. if (padre->getLeft()==n)
  237. padre->setLeft(n->getLeft());
  238. else
  239. padre->setRight(n->getLeft());
  240. n->getLeft()->setPadre(padre);
  241. }
  242. else
  243. root = n->getLeft();
  244. }
  245. else
  246. {
  247. if (padre!=0)
  248. {
  249. if (padre->getLeft()==n)
  250. padre->setLeft(n->getRight());
  251. else
  252. padre->setRight(n->getRight());
  253. n->getRight()->setPadre(padre);
  254. }
  255. else
  256. root = n->getRight();
  257. }
  258. delete n;
  259. return;
  260. }
  261. Nodo<H>* minimo = _min(n->getRight());
  262. n->setVal(minimo->getVal());
  263. _remove(minimo);
  264. }
  265. template <class H>
  266. BST<H>* BST<H>::remove(H val)
  267. {
  268. _remove(_search(val));
  269. return this;
  270. }
  271.  
  272. int getDuePunti(string x)
  273. {
  274. for (int i=0; i<x.length(); i++)
  275. if (x[i]==':')
  276. return i;
  277. return 0;
  278. }
  279.  
  280. string getOp(string x)
  281. {
  282. return x.substr(0, getDuePunti(x));
  283. }
  284.  
  285. string getValore(string x)
  286. {
  287. return x.substr(getDuePunti(x)+1, x.length());
  288. }
  289.  
  290. int main()
  291. {
  292. infile.open(INPUT_FILE, fstream::in);
  293. outfile.open(OUTPUT_FILE, fstream::out);
  294.  
  295. for (int k=0; k<100; k++)
  296. {
  297. string tipo, ordine;
  298. int N;
  299. infile >> tipo;
  300. if (tipo=="int")
  301. {
  302. infile >> N >> ordine;
  303. BST<int>* tree = new BST<int>;
  304. for (int i=0; i<N; i++)
  305. {
  306. string oper;
  307. string tmp;
  308. string _valore;
  309. int n;
  310. infile >>tmp;
  311. oper = getOp(tmp);
  312. _valore = getValore(tmp);
  313. n = stoi(_valore);
  314.  
  315. if(oper=="ins")
  316. tree->insert(n);
  317. else if (oper=="canc")
  318. tree->remove(n);
  319.  
  320. if (ordine=="preorder")
  321. tree->printPreOrder();
  322. else if (ordine=="inorder")
  323. tree->printInOrder();
  324. else if (ordine=="postorder")
  325. tree->printPostOrder();
  326. }
  327. }
  328. else if (tipo=="double")
  329. {
  330. infile >> N >> ordine;
  331. BST<double>* tree = new BST<double>;
  332. for (int i=0; i<N; i++)
  333. {
  334. string oper;
  335. string tmp;
  336. string _valore;
  337. double n;
  338. infile >>tmp;
  339. oper = getOp(tmp);
  340. _valore = getValore(tmp);
  341. n = stod(_valore);
  342.  
  343. if(oper=="ins")
  344. tree->insert(n);
  345. else if (oper=="canc")
  346. tree->remove(n);
  347. if (ordine=="preorder")
  348. tree->printPreOrder();
  349. else if (ordine=="postorder")
  350. tree->printPostOrder();
  351. else if (ordine == "inorder")
  352. tree->printInOrder();
  353. }
  354. }
  355. outfile << endl;
  356. }
  357. infile.close();
  358. outfile.close();
  359. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement