Advertisement
Denny707

Untitled

Jun 2nd, 2017
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.06 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4. struct nodo{
  5.     int key;
  6.     nodo *figlio_sx;
  7.     nodo *figlio_dx;
  8. };
  9. nodo* ricercaNodoRIC(nodo *root, int n);
  10. nodo* ricercaNodoITR(nodo *root, int n);
  11. nodo* treeMin(nodo *root);
  12. nodo* treeMax(nodo *root);
  13. void aggiungiNodoRic(nodo *root,int n);
  14. void aggiungiNodo(nodo *root,int n);
  15. void pre_order(nodo *root);
  16. void symmetric_order(nodo *root);
  17. void post_order(nodo *root);
  18. bool is_empty_tree(nodo *root);
  19. bool is_empty_node(nodo *node);
  20. int campo_info(nodo *node);
  21. nodo* link_sx(nodo *node);
  22. nodo* link_dx(nodo *node);
  23. struct nodo* deleteNode(struct nodo* root, int key);
  24.  
  25.  
  26.  
  27.  
  28. int main(int argc, char** argv) {
  29.     nodo *root = new nodo();
  30.     cout<<"Is empty tree? \t\t\t\t"<<boolalpha<<is_empty_tree(root)<<endl;
  31.     //          5
  32.     //      2       7
  33.     //   1    3   6   9
  34.     root->key=5;
  35.     aggiungiNodo(root, 7);
  36.     aggiungiNodo(root, 2);
  37.     aggiungiNodoRic(root, 3);
  38.     aggiungiNodoRic(root, 1);
  39.     aggiungiNodoRic(root, 6);
  40.    
  41.     aggiungiNodo(root, 9);
  42.     aggiungiNodo(root, 10);
  43.    
  44.     cout<<"stampa 'pre order:'\t\t\t";pre_order(root);
  45.     cout<<endl<<"stampa 'symmetric'\t\t\t";symmetric_order(root);
  46.     cout<<endl<<"stampa 'post order'\t\t\t";post_order(root);
  47.    
  48.     cout<<endl<<"Is empty tree? \t\t\t\t"<<boolalpha<<is_empty_tree(root);
  49.    
  50.     cout<<endl<<"chiave di valore minimo: \t"<<treeMin(root)->key<<endl;
  51.     cout<<"chiave di valore massimo: \t"<<treeMax(root)->key<<endl;
  52.    
  53.     //cancellando la root, si va a prendere il valore minore del suo sottoalbero destro,
  54.     //in modo da mantentere l'ordine del BST.
  55.     int el_da_elimin=2;
  56.     deleteNode(root,el_da_elimin);
  57.     cout<<"ho cancellato l'elemento "<<el_da_elimin<<":\t";
  58.     pre_order(root);
  59.    
  60.     cout<<endl<<"ricerca di 1 (ricorsiva):  \t"<<ricercaNodoRIC(root,10);
  61.     cout<<endl<<"ricerca di 1 (iterativa):  \t"<<ricercaNodoITR(root,10);
  62.     cout<<endl<<"ricerca di un numero non presente (ricorsiva):  \t"<<ricercaNodoRIC(root,100);
  63.    
  64.     cout<<endl<<endl;
  65.     //system("PAUSE");
  66.     return 0;
  67. }
  68.  
  69. //La funzione restituisce il puntatote al nodo cercato,
  70. //oppure NULL se l'elemento non Ë presente
  71. nodo* ricercaNodoRIC(nodo *root, int n){
  72.     if(root==NULL || n == root->key){
  73.         return root;
  74.     }else if(n>root->key){
  75.         return ricercaNodoRIC(root->figlio_dx,n);
  76.     }else if(n<root->key){
  77.         return ricercaNodoRIC(root->figlio_sx,n);
  78.     }
  79.     return NULL;
  80. }
  81. nodo* ricercaNodoITR(nodo *root, int n){
  82.     while(n<root->key && root->figlio_sx!=NULL){
  83.         root = root->figlio_sx;
  84.     }
  85.     while(n>root->key && root->figlio_dx!=NULL){
  86.         root = root->figlio_dx;
  87.     }
  88.     if(root==NULL || n == root->key){
  89.         return root;
  90.     }
  91.     return NULL;
  92. }
  93. //Il valore minimo Ë il nodo foglia del sottoalbero pi˘ a sinista
  94. //ossia prima foglia che incontra l'inorder
  95. nodo* treeMin(nodo *root){
  96.     if(root==NULL){
  97.         return root; //albero vuoto
  98.     }else{
  99.         if(root->figlio_sx==NULL){
  100.             return root;
  101.         }else{
  102.             return treeMin(root->figlio_sx);
  103.         }
  104.     }
  105. }
  106. nodo* treeMax(nodo *root){
  107.     if(root==NULL){
  108.         return root; //albero vuoto
  109.     }else{
  110.         if(root->figlio_dx==NULL){
  111.             return root;
  112.         }else{
  113.             return treeMax(root->figlio_dx);
  114.         }
  115.     }
  116. }
  117.  
  118.  
  119. void aggiungiNodoRic(nodo *root,int n){
  120.     nodo *newNode = new nodo();
  121.     newNode->key=n;
  122.     newNode->figlio_dx=NULL;
  123.     newNode->figlio_sx=NULL;
  124.    
  125.     if(n>root->key){ //le chiavi sono uniche, non esiste caso di uguaglianza
  126.         if(root->figlio_dx==NULL){
  127.             root->figlio_dx=newNode;
  128.         }else{
  129.             aggiungiNodoRic(root->figlio_dx, n);
  130.         }
  131.     }else if(n<root->key){
  132.         if(root->figlio_sx==NULL){
  133.             root->figlio_sx=newNode;
  134.         }else{
  135.             aggiungiNodoRic(root->figlio_sx, n);
  136.         }
  137.     }
  138. }
  139.  
  140. void aggiungiNodo(nodo *root,int n){
  141.     nodo *newNode = new nodo();
  142.     newNode->key=n;
  143.     newNode->figlio_dx=NULL;
  144.     newNode->figlio_sx=NULL;
  145.     while(n<root->key && root->figlio_sx!=NULL){
  146.         root = root->figlio_sx;
  147.     }
  148.     while(n>=root->key && root->figlio_dx!=NULL){
  149.         root = root->figlio_dx;
  150.     }
  151.     if(n>root->key){ //le chiavi sono uniche, non esiste caso di uguaglianza
  152.         root->figlio_dx=newNode;
  153.     }else if(n<root->key){
  154.         root->figlio_sx=newNode;
  155.     }
  156.    
  157. }
  158.  
  159. // STAMPE
  160. void pre_order(nodo *root) {
  161.     if(root!=NULL){
  162.         cout << root->key << " ";
  163.         pre_order(root->figlio_sx);
  164.         pre_order(root->figlio_dx);
  165.     }
  166.    
  167. }
  168.  
  169. void symmetric_order(nodo *root){
  170.     if(root!=NULL){
  171.         symmetric_order(root->figlio_sx);
  172.         cout << root->key << " ";
  173.         symmetric_order(root->figlio_dx);
  174.     }
  175. }
  176.  
  177. void post_order(nodo *root){
  178.     if(root!=NULL){
  179.         post_order(root->figlio_sx);
  180.         post_order(root->figlio_dx);
  181.         cout << root->key << " ";
  182.     }
  183. }
  184.  
  185. bool is_empty_tree(nodo *root){
  186.     if(root->figlio_dx== NULL
  187.        && root->figlio_sx==NULL
  188.        && root->key==NULL){
  189.         return true;
  190.     }else{
  191.         return false;
  192.     }
  193. }
  194.  
  195. bool is_empty_node(nodo *node){
  196.     if(node->figlio_dx== NULL
  197.        && node->figlio_sx==NULL
  198.        && node->key==NULL){
  199.         return true;
  200.     }else{
  201.         return false;
  202.     }
  203. }
  204.  
  205. int campo_info(nodo *node){
  206.     return node->key;
  207. }
  208.  
  209. nodo* link_sx(nodo *node){
  210.     return node->figlio_sx;
  211. }
  212.  
  213. nodo* link_dx(nodo *node){
  214.     return node->figlio_dx;
  215. }
  216.  
  217.  
  218. struct nodo* deleteNode(struct nodo* root, int key){
  219.     if (root == NULL){return root;}
  220.     /**
  221.      * RICERCA DEL NODO DA ELIMINARE
  222.      **/
  223.     // Se la key da eliminare è più piccola della key di root
  224.     // allora essa si trova nel sottoalbero sinistro
  225.     if (key < root->key){
  226.         root->figlio_sx = deleteNode(root->figlio_sx, key);
  227.         //Se è più grande si trova a destra.
  228.     }else if (key > root->key){
  229.         root->figlio_dx = deleteNode(root->figlio_dx, key);
  230.        
  231.         /**
  232.          * NODO TROVATO
  233.          **/
  234.         // se la key è la stessa della root allora questo è il nodo da eliminare
  235.     }else{
  236.         // se il nodo ha un figlio, esso prende il posto di questo nodo
  237.         if (root->figlio_sx == NULL){
  238.             struct nodo *temp = root->figlio_dx;
  239.             root->figlio_sx=NULL;
  240.             root->figlio_dx=NULL;
  241.             root->key=NULL;
  242.             return temp;
  243.         }else if (root->figlio_dx == NULL){
  244.             cout<<"ho cancellato: "<<root->key<<endl;
  245.             struct nodo *temp = root->figlio_sx;
  246.             root->figlio_sx=NULL;
  247.             root->figlio_dx=NULL;
  248.             root->key=NULL;
  249.             return temp;
  250.         }
  251.        
  252.         // Se il nodo ha più di due figli, bisogna prendere il minore del suo sottoalbero destro
  253.         // Quindi mi salvo questo minore in un nodo temporaneo
  254.         struct nodo* temp = treeMin(root->figlio_dx);
  255.         root->key = temp->key;
  256.         root->figlio_dx = deleteNode(root->figlio_dx, temp->key);
  257.     }
  258.     return root;
  259. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement