Advertisement
Guest User

Untitled

a guest
Feb 25th, 2020
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.29 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. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement