Advertisement
nicb

ABR Completo

Dec 14th, 2018
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 8.04 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3. class Nodo
  4. {
  5.     private:
  6.         int key;
  7.         Nodo *parent;
  8.         Nodo *left;
  9.         Nodo *right;
  10. //        T data;
  11.  
  12.     public:
  13.         Nodo(int key/*, T data*/):parent{nullptr}, left{nullptr},right{nullptr} {this->key=key;/*this->data=data;*/}
  14.         Nodo(Nodo *x){
  15.         setKey(x->getKey());
  16.         setParent(x->getParent());
  17.         setLeft(x->getLeft());
  18.         setRight(x->getRight());
  19.         }
  20.         void setKey(int tmp){key=tmp;};
  21.         int getKey(){return key;}
  22.         void setParent(Nodo *p){parent=p;}
  23.         Nodo *getParent(){return parent;}
  24.         void setLeft(Nodo *l){left=l;}
  25.         Nodo *getLeft(){return left;}
  26.         void setRight(Nodo *r){right=r;}
  27.         Nodo *getRight(){return right;}
  28.         ///T& getData(){return data;}
  29.         /*Nodo& operator=(Nodo *b){
  30.         this->setParent(b->getParent());
  31.         this->setLeft(b->getLeft());
  32.         this->setRight(b->getRight());
  33.         this->setKey(b->getKey());
  34.         return *this;
  35.         };*/
  36.         void Copy(Nodo *b);
  37.         virtual ~Nodo() {}
  38.  
  39. };
  40. void Nodo::Copy(Nodo *b){
  41. if(b){
  42. parent=b->getParent();
  43. left=b->getLeft();
  44. right=b->getRight();
  45. key=b->getKey();
  46. }
  47. }
  48. class BinarySearchTree
  49. {
  50.     private:
  51.         Nodo *root;
  52.  
  53.     public:
  54.         BinarySearchTree(){root=nullptr;}
  55.         virtual ~BinarySearchTree();  //TODO
  56.  
  57.         Nodo *getRoot(){return root;}
  58.         void setRoot(Nodo *x){root=x;};
  59.         void insertNodo(int key);
  60.         void delNodo(int key);  //TODO
  61.         void transplant(Nodo *a,Nodo *b);
  62.         void inorder(Nodo *x);//simmetrica
  63.         void preorder(Nodo *x);//anticipata
  64.         void postorder(Nodo *x);//differita
  65.  
  66.         Nodo *minimum(Nodo *x);
  67.         Nodo *maximum(Nodo *x);
  68.  
  69.         Nodo *searchNodo(int key);
  70.         Nodo *predecessor(Nodo *x);
  71.         Nodo *predecessor(int x);
  72.         Nodo *successor(Nodo *x);
  73.         Nodo *successor(int x);
  74.         void DeleteDouble(Nodo *x);
  75. };
  76. void BinarySearchTree::DeleteDouble(Nodo *x){
  77. if(x!=nullptr){
  78. DeleteDouble(x->getLeft());
  79. DeleteDouble(x->getRight());
  80. if(successor(x)!=nullptr && successor(x)->getKey()==x->getKey())
  81.   delNodo(x->getKey());
  82. }
  83. }
  84. void BinarySearchTree::transplant(Nodo *a,Nodo *b){
  85. if(a){
  86. if(!a->getParent())
  87.   this->setRoot(b);
  88. else if(a==a->getParent()->getLeft())
  89.   a->getParent()->setLeft(b);
  90. else
  91.   a->getParent()->setRight(b);
  92. if(b)//se non mi hai passato un nodo vuoto, imposta come Parent di b il Parent di a.
  93.   b->setParent(a->getParent());
  94. }
  95. }
  96. void BinarySearchTree::delNodo(int key){
  97. Nodo *a=this->searchNodo(key);
  98. if(!a->getLeft())
  99.   transplant(a,a->getRight());
  100. else if(!a->getRight())
  101.   transplant(a,a->getLeft());
  102. else{
  103.   Nodo *suc(this->minimum(a->getRight()));
  104.   if(suc->getParent()!=a){
  105.   transplant(suc,suc->getRight());
  106.   suc->setRight(a->getRight());
  107.   suc->getRight()->setParent(suc);
  108.   }
  109.   transplant(a,suc);
  110.   suc->setLeft(a->getLeft());
  111.   suc->getLeft()->setParent(suc);
  112. }
  113. }
  114. void BinarySearchTree::insertNodo(int key){
  115.     Nodo *z=new Nodo(key);
  116.     Nodo *y=nullptr;
  117.     Nodo *x=root;
  118.     while(x!=nullptr){
  119.         y=x;
  120.         if(z->getKey()<x->getKey())
  121.             x=x->getLeft();
  122.         else
  123.             x=x->getRight();
  124.     }
  125.     z->setParent(y);
  126.     if(y==nullptr)
  127.         root=z;
  128.     else if(z->getKey()<y->getKey())
  129.         y->setLeft(z);
  130.     else
  131.         y->setRight(z);
  132. }
  133.  
  134. void BinarySearchTree::inorder(Nodo *x){
  135.     if(x!=nullptr){
  136.         inorder(x->getLeft());
  137.         cout<<x->getKey()<<" ";
  138.         inorder(x->getRight());
  139.     }
  140. }
  141.  
  142. void BinarySearchTree::preorder(Nodo *x){
  143.     if(x!=nullptr){
  144.         cout<<x->getKey()<<" ";
  145.         inorder(x->getLeft());
  146.         inorder(x->getRight());
  147.     }
  148. }
  149.  
  150. void BinarySearchTree::postorder(Nodo *x){
  151.     if(x!=nullptr){
  152.         inorder(x->getLeft());
  153.         inorder(x->getRight());
  154.         cout<<x->getKey()<<" ";
  155.     }
  156. }
  157.  
  158. Nodo *BinarySearchTree::minimum(Nodo *x){
  159.     while(x->getLeft()!= nullptr)
  160.         x=x->getLeft();
  161.     return x;
  162. }
  163.  
  164. Nodo *BinarySearchTree::maximum(Nodo *x){
  165.     while(x->getRight()!= nullptr)
  166.         x=x->getRight();
  167.     return x;
  168. }
  169. Nodo *BinarySearchTree::predecessor(Nodo *x){
  170.    if(x->getLeft())
  171.      return this->maximum(x->getLeft());
  172.     Nodo *y=x->getParent();
  173.     while(y && x==y->getLeft()){
  174.     x=y;
  175.     y=y->getParent();
  176. }
  177. return y;
  178. }
  179. Nodo *BinarySearchTree::predecessor(int x){
  180. Nodo *tmp=searchNodo(x);
  181. if(tmp)
  182.    return predecessor(tmp);
  183. return tmp;
  184. }
  185. Nodo *BinarySearchTree::successor(Nodo *x){
  186. if(x->getRight()!=NULL)
  187.   return this->minimum(x->getRight());
  188. Nodo *y=x->getParent();
  189. while(y && x==y->getRight()){
  190.      x=y;
  191.      y=y->getParent();
  192. }
  193. return y;
  194. }
  195. Nodo *BinarySearchTree::successor(int x){
  196. Nodo *tmp=searchNodo(x);
  197. if(tmp)
  198.    return successor(tmp);
  199. return tmp;
  200. }
  201. Nodo *BinarySearchTree::searchNodo(int key){
  202. Nodo *tmp=getRoot();
  203. while(tmp && tmp->getKey()!=key){
  204.     if(tmp->getKey()<key)
  205.        tmp=tmp->getRight();
  206.     else
  207.        tmp=tmp->getLeft();
  208. }
  209. return tmp;
  210.  
  211. }
  212. BinarySearchTree::~BinarySearchTree()
  213. {
  214.     //dtor
  215. };
  216. int main()
  217. {
  218.     BinarySearchTree bt;
  219.     Nodo *n;
  220.     int tmp;
  221.     short choice;
  222.     bt.insertNodo(16);
  223.     bt.insertNodo(13);
  224.     bt.insertNodo(17);
  225.     bt.insertNodo(15);
  226.     bt.insertNodo(12);
  227.     bt.insertNodo(11);
  228.     bt.insertNodo(10);
  229.     bt.insertNodo(9);
  230.     while(1){
  231.     cout<<"1)PreOrder\n2)InOrder\n3)PostOrder\n4)Insert\n5)Ricerca\n6)Cancella\n7)Cerca predeccessore\n8)Cerca successore\n9)Trova minimo\n10)Trova massimo\n11)Ricava famiglia\n12)Elimina doppioni\n13)Uscire\n";
  232.     cin>>choice;
  233.     switch(choice){
  234.     case 1:
  235.     cout<<"Pre Order visit: "<<endl;
  236.     bt.preorder(bt.getRoot());
  237.     break;
  238.     case 2:
  239.     cout<<endl<<"In Order visit: "<<endl;
  240.     bt.inorder(bt.getRoot());
  241.     break;
  242.     case 3:
  243.     cout<<endl<<"Post Order visit: "<<endl;
  244.     bt.postorder(bt.getRoot());
  245.     cout<<endl;
  246.     break;
  247.     case 4:
  248.     cout<<"Inserire il valore dell'elemento da aggiungere: "<<endl;
  249.     cin>>tmp;
  250.     bt.insertNodo(tmp);
  251.     break;
  252.     case 6:
  253.     cout<<"Inserire il valore dell'elemento da eliminare: "<<endl;
  254.     cin>>tmp;
  255.     bt.delNodo(tmp);
  256.     break;
  257.     case 5:
  258.     cout<<"Inserire valore della chiave del nodo da ricercare: ";
  259.     cin>>tmp;
  260.     if(bt.searchNodo(tmp))
  261.        cout<<"Il nodo e' stato trovato"<<endl;
  262.     else
  263.        cout<<"Il nodo non e' stato trovato"<<endl;
  264.     break;
  265.     case 7:
  266.     cout<<"Inserire elemento di cui vuoi calcolare il predecessore: ";
  267.     cin>>tmp;
  268.     if(n=bt.predecessor(tmp))
  269.        cout<<n->getKey()<<endl;
  270.     else
  271.        cout<<"No data found"<<endl;
  272.     break;
  273.     case 8:
  274.     cout<<"Inserire elemento di cui vuoi calcolare il successore: ";
  275.     cin>>tmp;
  276.     if(n=bt.successor(tmp))
  277.        cout<<n->getKey()<<endl;
  278.     else
  279.        cout<<"No data found"<<endl;
  280.     break;
  281.     case 9:
  282.     if(n=bt.minimum(bt.getRoot()))
  283.        cout<<"Il minimo e': "<<n->getKey()<<endl;
  284.     else
  285.       cout<<"L'albero e' vuoto"<<endl;
  286.     break;
  287.     case 10:
  288.     if(n=bt.maximum(bt.getRoot()))
  289.        cout<<"Il massimo e': "<<n->getKey()<<endl;
  290.     else
  291.        cout<<"L'albero e' vuoto"<<endl;
  292.     break;
  293.     case 11:
  294.     cout<<"Inserire valore della chiave del nodo di cui vuoi conoscere la famiglia: ";
  295.     cin>>tmp;
  296.     if(n=bt.searchNodo(tmp)){
  297.     if(n->getParent())
  298.       cout<<"Il padre e': "<<n->getParent()->getKey()<<endl;
  299.     else
  300.       cout<<"Il nodo e' radice"<<endl;
  301.     if(n->getLeft())
  302.       cout<<"Il figlio sinistro e': "<<n->getLeft()->getKey()<<endl;
  303.     else
  304.       cout<<"Il nodo non ha figlio sinistro"<<endl;
  305.     if(n->getRight())
  306.       cout<<"Il figlio destro e': "<<n->getRight()->getKey()<<endl;
  307.     else
  308.       cout<<"Il nodo non ha figlio destro"<<endl;
  309.     }
  310.     else
  311.       cout<<"Il nodo non e' stato trovato\n";
  312.     break;
  313.     case 12:
  314.        bt.DeleteDouble(bt.getRoot());
  315.        break;
  316.     case 13:
  317.        exit(0);
  318.     default:
  319.        cout<<"Comando non riconosciuto, riprovare\n";
  320.        break;
  321. }
  322. cout<<endl;
  323. }
  324. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement