Advertisement
Guest User

Untitled

a guest
Feb 25th, 2020
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.14 KB | None | 0 0
  1. #include <iostream>
  2. #include <math.h>>
  3. #include <fstream>
  4.  
  5. using namespace std;
  6.  
  7.  
  8. template <class G>
  9. class Nodo{
  10. private:
  11. Nodo<G>* left;
  12. Nodo<G>* right;
  13. Nodo<G>* parent;
  14. G *key;
  15. public:
  16. Nodo(G k){
  17. this->key=new G(k);
  18. left=right=parent=NULL;
  19.  
  20.  
  21.  
  22. }
  23.  
  24.  
  25. Nodo<G>* getParent(){
  26. return parent;
  27. }
  28. Nodo<G>* getLeft(){
  29. return left;
  30. }
  31. Nodo<G>* getRight(){
  32. return right;
  33. }
  34.  
  35. G getKey(){
  36. return *key;
  37. }
  38.  
  39.  
  40. void setLeft(Nodo<G>* left){
  41. this->left=left;
  42. }
  43. void setRight(Nodo<G>* right){
  44. this->right=right;
  45. }
  46.  
  47. void setParent(Nodo<G>* parent){
  48. this->parent=parent;
  49. }
  50. void setKey(G item){
  51. this->key=new G(item);
  52. }
  53.  
  54. };
  55.  
  56.  
  57.  
  58. template <class H>
  59. class Albero{
  60. private:
  61. Nodo<H>* root;
  62.  
  63. public:
  64. Albero(){
  65. root=NULL;
  66. }
  67. Albero<H>* insert(H item){
  68. Nodo<H>* nuovo=new Nodo<H>(item);
  69. if(root==NULL){
  70. root=nuovo;
  71. return this;
  72. }
  73.  
  74.  
  75. Nodo<H>* iter=root;
  76. Nodo<H>* par=NULL;
  77. while(iter!=NULL){
  78. par=iter;
  79. if(iter->getKey()<item){
  80. iter=iter->getRight();
  81. }else{
  82. iter=iter->getLeft();
  83. }
  84.  
  85.  
  86.  
  87. }
  88.  
  89. if(par==NULL){
  90. return NULL;
  91. }
  92. else{
  93. if(par->getKey()<item){
  94. par->setRight(nuovo);
  95. }else
  96. par->setLeft(nuovo);
  97. }
  98.  
  99. nuovo->setParent(par);
  100. return this;
  101. }
  102.  
  103.  
  104.  
  105.  
  106.  
  107.  
  108.  
  109.  
  110. void inorder(){
  111. _inorder(root);
  112. }
  113.  
  114. void _inorder(Nodo<H>* start){
  115. if(start!=NULL){
  116. _inorder(start->getLeft());
  117. cout<<start->getKey()<<" ";
  118. _inorder(start->getRight());
  119. }
  120.  
  121. }
  122.  
  123.  
  124.  
  125.  
  126.  
  127. void ricerca(H item){
  128. _ricerca(root,item);
  129. }
  130.  
  131.  
  132. Nodo<H>* _ricerca(Nodo<H>* start,H item){
  133. while(start!=NULL && start->getKey()!=item){
  134. if(start->getKey()<item){
  135. start=start->getRight();
  136. }else{
  137. start=start->getLeft();
  138. }
  139.  
  140.  
  141. }
  142. if(start==NULL){
  143.  
  144. return NULL;
  145. }else{
  146. // cout<<start->getKey();
  147. return start;
  148. }
  149.  
  150.  
  151. }
  152.  
  153.  
  154. void mini(){
  155. return _mini(root);
  156.  
  157. }
  158.  
  159. Nodo<H>* _mini(Nodo<H>* start){
  160. if(start->getLeft()!=NULL){
  161.  
  162. start=start->getLeft();
  163. }
  164.  
  165. return start;
  166.  
  167.  
  168. }
  169.  
  170.  
  171. H* successore(H item){
  172. Nodo<H>* ric=_ricerca(root,item);
  173. if(ric==NULL){
  174. return NULL;
  175. }
  176. Nodo<H>* succ=_successore(ric);
  177. if(succ==NULL){
  178. return NULL;
  179. }
  180. cout<<succ->getKey();
  181. return new H(succ->getKey());
  182. }
  183.  
  184. Nodo<H>* _successore(Nodo<H>* start){
  185. if(start->getRight()!=NULL){
  186. return _mini(start->getRight());
  187. }
  188.  
  189. Nodo<H>* par=start->getParent();
  190.  
  191. while(par!=NULL && par->getKey()<start->getKey()){
  192.  
  193. par=par->getParent();
  194. }
  195.  
  196.  
  197.  
  198. return par;
  199.  
  200.  
  201.  
  202.  
  203.  
  204. }
  205.  
  206.  
  207.  
  208.  
  209. void cancella(H item){
  210. _cancella(root,item);
  211. }
  212.  
  213.  
  214.  
  215. void _cancella(Nodo<H>* start,H item){
  216. Nodo<H>* ric=_ricerca(start,item);
  217.  
  218. if(ric==NULL){
  219. return;
  220. }
  221.  
  222. Nodo<H>* par=ric->getParent();
  223.  
  224.  
  225. if(!ric->getLeft() || !ric->getRight()){
  226. Nodo<H>* under=ric->getRight();
  227. if(ric->getLeft()){
  228. under=ric->getLeft();
  229. }
  230.  
  231. if(par==NULL){
  232. root=under;
  233. }
  234. else{
  235. if(par->getLeft()==ric){
  236. par->setLeft(under);
  237. }else{
  238. par->setRight(under);
  239. }
  240.  
  241. }
  242.  
  243. if(under!=NULL){
  244. under->setParent(par);
  245. }
  246.  
  247.  
  248.  
  249. }else{
  250. Nodo<H>* succ=_successore(ric);
  251. ric->setKey(succ->getKey());
  252. _cancella(ric->getRight(),succ->getKey());
  253.  
  254. }
  255.  
  256. return;
  257.  
  258. }
  259.  
  260.  
  261.  
  262. int bilanciamento(H item){
  263. return _bilanciamento(root,item);
  264. }
  265.  
  266. int _bilanciamento(Nodo<H>* start, H item){
  267. Nodo<H>* ric=_ricerca(start,item);
  268. int i=0;
  269. Nodo<H>* sinistra=ric;
  270. while(sinistra->getLeft()!=NULL){
  271.  
  272. sinistra=sinistra->getLeft();
  273.  
  274. i++;
  275. }
  276. int j=0;
  277. while(ric->getRight()!=NULL){
  278. ric=ric->getRight();
  279. j++;
  280. }
  281. cout<<i<<j;
  282.  
  283. i=abs(i-j);
  284. cout<<" "<<i;
  285. }
  286.  
  287.  
  288.  
  289. void peso(H item){
  290. Nodo<H>* ric=_ricerca(root,item);
  291. int sx=0;
  292. int dx=0;
  293. // int bal=ric->getKey();
  294. int lb=_peso(ric->getLeft(),sx);
  295. int rb=_peso(ric->getRight(),dx);
  296. cout<<abs(lb-rb);
  297. // bal += abs(lb + rb);
  298. //cout<<bal;
  299. }
  300.  
  301.  
  302. int _peso(Nodo<H>* start,int &num){
  303. if(start!=NULL){
  304. num++;
  305. _peso(start->getLeft(),num);
  306. _peso(start->getRight(),num);
  307. }
  308.  
  309. return num;
  310.  
  311.  
  312.  
  313.  
  314. }
  315. void leaf(){
  316. int c=0;
  317. c= _leaf(c,root);
  318. cout<<c;
  319. }
  320.  
  321.  
  322. int _leaf(int &c,Nodo<H>* start){
  323.  
  324. if(start->getRight()==NULL && start->getLeft()==NULL){
  325. c++;
  326.  
  327.  
  328.  
  329. }
  330.  
  331.  
  332.  
  333. if(start->getRight()!=NULL){
  334. _leaf(c,start->getRight());
  335. }
  336. if(start->getLeft()!=NULL){
  337. _leaf(c,start->getLeft());
  338. }
  339.  
  340. return c;
  341. }
  342.  
  343.  
  344. };
  345.  
  346.  
  347.  
  348.  
  349. int main(){
  350.  
  351. Albero<int>* bst=new Albero<int>();
  352.  
  353. bst->insert(3);
  354. bst->insert(48);
  355. bst->insert(158);
  356. bst->insert(6);
  357. bst->insert(224);
  358. bst->insert(273);
  359. bst->insert(297);
  360. bst->insert(150);
  361. bst->insert(280);
  362. bst->insert(200);
  363. // bst->cancella(127);
  364. bst->inorder();
  365. // bst->predecessore(230);
  366. // bst->successore(223);
  367. // bst->ricerca(4);
  368. bst->bilanciamento(158);
  369. // bst->peso(158);
  370. // bst->leaf();
  371. }
  372. #include <iostream>
  373. #include <fstream>
  374. #include <cmath>
  375. using namespace std;
  376.  
  377. int selectionsort(int* vett, int n){
  378. int min;
  379. int sum = 0;
  380. for(int i = 0; i < n; i++){
  381. min = i;
  382. for(int j = i+1; j < n; j++)
  383. if(vett[j] < vett[min])
  384. min = j;
  385. sum += min -i;
  386. swap(vett[i], vett[min]);
  387.  
  388. }
  389.  
  390. return sum;
  391. }
  392.  
  393.  
  394. int main(){
  395.  
  396. ifstream in("input.txt");
  397. ofstream out("output.txt");
  398.  
  399. for(int i = 0; i < 100; i++){
  400. int n;
  401. in >> n;
  402. int* vett = new int[n];
  403. for(int j = 0; j < n; j++)
  404. in >> vett[j];
  405. out << selectionsort(vett, n) << endl;
  406.  
  407. delete [] vett;
  408. }
  409.  
  410.  
  411.  
  412. return 0;
  413. }
  414. #include <iostream>
  415. #include <fstream>
  416. using namespace std;
  417.  
  418. int insertionsort(int* vett, int n){
  419. int i, j, tmp;
  420. int contatore = 0;
  421. for(int i = 1; i < n; i++){
  422. j = i;
  423. while(j > 0 && vett[j-1] > vett[j]){
  424. tmp = vett[j];
  425. vett[j] = vett[j-1];
  426. vett[j-1] = tmp;
  427. j--;
  428. contatore++;
  429. }
  430. }
  431.  
  432.  
  433. return contatore;
  434. }
  435.  
  436.  
  437. int main(){
  438.  
  439. ifstream in("input.txt");
  440.  
  441. ofstream out("output.txt");
  442.  
  443. for(int i = 0; i < 10; i++){
  444. int n;
  445. in >> n;
  446. int* vett = new int[n];
  447. for(int j = 0; j < n; j++)
  448. in >> vett[j];
  449. int contatore = insertionsort(vett, n);
  450. out << contatore << endl;
  451.  
  452. }
  453.  
  454.  
  455. return 0;
  456. }
  457. #include <iostream>
  458. #include <fstream>
  459. #include <cstdlib>
  460. using namespace std;
  461.  
  462. void Merge(int* vett, int sx, int c, int dx){
  463. int i = sx;
  464. int j = c+1;
  465. int k = 0;
  466. int tmp[dx-sx+1];
  467.  
  468. while(i <= c && j <= dx){
  469. if(vett[i] < vett[j]){
  470. tmp[k] = vett[i];
  471. i++;
  472. }
  473. else{
  474. tmp[k] = vett[j];
  475. j++;
  476. }
  477. k++;
  478. }
  479.  
  480. while(i <= c){
  481. tmp[k] = vett[i];
  482. i++; k++;
  483. }
  484.  
  485. while(j <= dx){
  486. tmp[k] = vett[j];
  487. j++; k++;
  488. }
  489.  
  490. for(int i = sx; i <= dx; i++)
  491. vett[i] = tmp[i - sx];
  492. }
  493.  
  494. void MergeSort(int* vett, int sx, int dx, int &contatore){
  495. if(sx < dx){
  496. int c = (sx+dx)/2;
  497. contatore += vett[sx];
  498. MergeSort(vett, sx, c, contatore);
  499. MergeSort(vett, c+1, dx, contatore);
  500. Merge(vett, sx, c, dx);
  501.  
  502. }
  503. }
  504.  
  505.  
  506.  
  507.  
  508. int main(){
  509.  
  510. ifstream in("input.txt");
  511. ofstream out("output.txt");
  512.  
  513. for(int i = 0; i < 100; i++){
  514. int n; in >> n;
  515. int* vett = new int[n];
  516. for(int j = 0; j < n; j++){
  517. int tmp; in >> tmp;
  518. vett[j] = tmp;
  519. }
  520. int contatore = 0;
  521. MergeSort(vett, 0, n-1, contatore);
  522. out << contatore << endl;
  523. delete vett;
  524. }
  525.  
  526. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement