Advertisement
Guest User

albero.h

a guest
Nov 22nd, 2019
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.38 KB | None | 0 0
  1. #ifndef ALBERO_H_INCLUDED
  2. #define ALBERO_H_INCLUDED
  3. #include "nodo.h"
  4.  
  5. class albero
  6. {
  7.     nodo *root;
  8.     nodo *nil;
  9.     void leftrotate(nodo *);
  10.     void rightrotate(nodo *);
  11.     void fixdelate(nodo *);
  12.     void fixviolation(nodo *);
  13.     void treeinsert(nodo *);
  14.     void transplant(nodo *, nodo *);
  15.     nodo *treeminimum(nodo *);
  16.  
  17. public:
  18.     albero();
  19.     void inserimento(int);
  20.     void inorder(nodo *);
  21.     void delate(nodo *);
  22.     nodo *getroot(){return this->root;}
  23.     ~albero(){;}
  24. };
  25.  
  26. albero::albero()
  27. {
  28.     this->nil=new nodo(-1, nullptr, nullptr, nullptr, nero);
  29.     root=nil;
  30.     nil->setp(root);
  31. }
  32.  
  33. void albero::leftrotate(nodo *nod)
  34. {
  35.     nodo *app=nod->getdx();
  36.     nod->setdx(app->getsx());
  37.  
  38.     if(app->getsx()!=this->nil)
  39.         app->getsx()->setp(nod);
  40.  
  41.     app->setp(nod->getp());
  42.  
  43.     if(nod->getp()==this->nil)
  44.     {
  45.         this->root=app;
  46.     }
  47.     else if(nod==nod->getp()->getsx())
  48.     {
  49.         nod->getp()->setsx(app);
  50.     }
  51.     else
  52.         nod->getp()->setdx(app);
  53.  
  54.     app->setsx(nod);
  55.     nod->setp(app);
  56. }
  57.  
  58. void albero::rightrotate(nodo *nod)
  59. {
  60.     nodo *app=nod->getsx();
  61.     nod->setsx(app->getdx());
  62.  
  63.     if(app->getdx()!=this->nil)
  64.         app->getdx()->setp(nod);
  65.  
  66.     app->setp(nod->getp());
  67.  
  68.     if(nod->getp()==this->nil)
  69.     {
  70.         this->root=app;
  71.     }
  72.     else if(nod==nod->getp()->getdx())
  73.     {
  74.         nod->getp()->setdx(app);
  75.     }
  76.     else
  77.         nod->getp()->setsx(app);
  78.  
  79.     app->setdx(nod);
  80.     nod->setp(app);
  81. }
  82.  
  83. void albero::treeinsert(nodo *nod)
  84. {
  85.     nodo *x=this->root;
  86.     nodo *y=this->nil;
  87.  
  88.     while(x!=nil)
  89.     {
  90.         y=x;
  91.         if(nod->getkey()<x->getkey())
  92.             x=x->getsx();
  93.         else
  94.             x=x->getdx();
  95.     }
  96.  
  97.     nod->setp(y);
  98.  
  99.     if(y==this->nil)
  100.         this->root=nod;
  101.     else if(nod->getkey()<y->getkey())
  102.         y->setsx(nod);
  103.     else
  104.         y->setdx(nod);
  105. }
  106.  
  107. void albero::fixviolation(nodo *app)
  108. {
  109.     while((app!=this->root) && (app->getp()->getcolore()==rosso))
  110.     {
  111.         if(app->getp()==app->getp()->getp()->getsx())
  112.         {
  113.             nodo *niu=app->getp()->getp()->getdx();
  114.             if(niu->getcolore()==rosso)
  115.             {
  116.                 app->getp()->setcolore(nero);
  117.                 niu->setcolore(nero);
  118.                 app->getp()->getp()->setcolore(rosso);
  119.                 app=app->getp()->getp();
  120.             }
  121.             else
  122.             {
  123.                 if(app==app->getp()->getdx())
  124.                 {
  125.                     app=app->getp();
  126.                     this->leftrotate(app);
  127.                 }
  128.                 app->getp()->setcolore(nero);
  129.                 app->getp()->getp()->setcolore(rosso);
  130.                 this->rightrotate(app->getp()->getp());
  131.             }
  132.         }
  133.         else
  134.         {
  135.             nodo *niu=app->getp()->getp()->getsx();
  136.             if(niu->getcolore()==rosso)
  137.             {
  138.                 app->getp()->setcolore(nero);
  139.                 niu->setcolore(nero);
  140.                 app->getp()->getp()->setcolore(rosso);
  141.                 app=app->getp()->getp();
  142.             }
  143.             else
  144.             {
  145.                 if(app==app->getp()->getsx())
  146.                 {
  147.                     app=app->getp();
  148.                     this->rightrotate(app);
  149.                 }
  150.                 app->getp()->setcolore(nero);
  151.                 app->getp()->getp()->setcolore(rosso);
  152.                 this->leftrotate(app->getp()->getp());
  153.             }
  154.         }
  155.     }
  156.     this->root->setcolore(nero);
  157. }
  158.  
  159. void albero::transplant(nodo *u, nodo *v)
  160. {
  161.     if(u->getp()==this->nil)
  162.         this->root=v;
  163.     else if(u==u->getp()->getsx())
  164.         u->getp()->setsx(v);
  165.     else
  166.         u->getp()->setdx(v);
  167.  
  168.     v->setp(u->getp());
  169. }
  170.  
  171. void albero::fixdelate(nodo *x)
  172. {
  173.     while(x!=this->root && x->getcolore()==nero)
  174.     {
  175.         if(x==x->getp()->getsx())
  176.         {
  177.             nodo *w=x->getp()->getdx();
  178.             if(w->getcolore()==rosso)
  179.             {
  180.                 w->setcolore(nero);
  181.                 x->getp()->setcolore(rosso);
  182.                 leftrotate(x->getp());
  183.                 w=x->getp()->getdx();
  184.             }
  185.             if(w->getsx()->getcolore()==nero && w->getsx()->getcolore()==nero)
  186.             {
  187.                 w->setcolore(rosso);
  188.                 x=x->getp();
  189.             }
  190.             else
  191.             {
  192.                 if(w->getdx()->getcolore()==nero)
  193.                 {
  194.                     w->getsx()->setcolore(nero);
  195.                     w->setcolore(rosso);
  196.                     rightrotate(w);
  197.                     w=w->getp()->getdx();
  198.                 }
  199.                 w->setcolore(x->getp()->getcolore());
  200.                 x->getp()->setcolore(nero);
  201.                 w->getdx()->setcolore(nero);
  202.                 leftrotate(x->getp());
  203.                 x=this->root;
  204.             }
  205.         }
  206.         else
  207.         {
  208.             nodo *w=x->getp()->getsx();
  209.             if(w->getcolore()==rosso)
  210.             {
  211.                 w->setcolore(nero);
  212.                 x->getp()->setcolore(rosso);
  213.                 rightrotate(x->getp());
  214.                 w=x->getp()->getsx();
  215.             }
  216.             if(w->getdx()->getcolore()==nero && w->getdx()->getcolore()==nero)
  217.             {
  218.                 w->setcolore(rosso);
  219.                 x=x->getp();
  220.             }
  221.             else
  222.             {
  223.                 if(w->getsx()->getcolore()==nero)
  224.                 {
  225.                     w->getdx()->setcolore(nero);
  226.                     w->setcolore(rosso);
  227.                     leftrotate(w);
  228.                     w=w->getp()->getsx();
  229.                 }
  230.                 w->setcolore(x->getp()->getcolore());
  231.                 x->getp()->setcolore(nero);
  232.                 w->getsx()->setcolore(nero);
  233.                 rightrotate(x->getp());
  234.                 x=this->root;
  235.             }
  236.         }
  237.  
  238.     }
  239.     x->setcolore(nero);
  240. }
  241.  
  242. nodo *albero::treeminimum(nodo *a)
  243. {
  244.     if(a->getsx()==this->nil)
  245.         return a;
  246.     else
  247.     {
  248.         treeminimum(a->getsx());
  249.     }
  250. }
  251.  
  252. void albero::inserimento(int dato)
  253. {
  254.     nodo *app=new nodo(dato, this->nil, this->nil, this->nil, rosso);
  255.     this->treeinsert(app);
  256.     this->fixviolation(app);
  257. }
  258.  
  259. void albero::inorder(nodo *n)
  260. {
  261.     if(n!=this->nil)
  262.     {
  263.         std::cout<<n->getkey()<<" ";
  264.         (n->getcolore()==rosso) ? std::cout<<"rosso"<<std::endl : std::cout<<"nero"<<std::endl;
  265.         inorder(n->getsx());
  266.         inorder(n->getdx());
  267.     }
  268. }
  269.  
  270. void albero::delate(nodo *z)
  271. {
  272.     nodo *y=z;
  273.     Colore yc=z->getcolore();
  274.     if(z->getsx()==this->nil)
  275.     {
  276.         nodo *x=z->getdx();
  277.         transplant(z, z->getdx());
  278.        // if(yc==nero)
  279.          //   fixdelate(x);
  280.     }
  281.     else if(z->getdx()==this->nil)
  282.     {
  283.         nodo *x=z->getsx();
  284.         transplant(z, z->getsx());
  285.        // if(yc==nero)
  286.          //   fixdelate(x);
  287.     }
  288.     else
  289.     {
  290.  
  291.         y=treeminimum(z->getdx());
  292.         yc=y->getcolore();
  293.         nodo *x=y->getdx();
  294.         if(y->getp()==z)
  295.             x->setp(y);
  296.         else
  297.         {
  298.             transplant(y, y->getdx());
  299.             y->setdx(z->getdx());
  300.             y->getdx()->setp(y);
  301.         }
  302.         transplant(z, y);
  303.         y->setsx(z->getsx());
  304.         y->getsx()->setp(y);
  305.         y->setcolore(z->getcolore());
  306.         if(yc==nero)
  307.             fixdelate(x);
  308.     }
  309. }
  310.  
  311. #endif //ALBERO_H_INCLUDED
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement