darkjessy94

alb bin con classi (alb di natale) l'arocca

Nov 7th, 2017
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.45 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. class Node{
  5. private:
  6.     int var;
  7.     Node *left, *right;
  8. public:
  9.     Node(int v, Node *l, Node *r):var(v), left(l), right(r){}
  10.     Node(int v):var(v), left(nullptr), right(nullptr){}
  11.     Node* leftChild(){return left;}
  12.     Node* rightChild(){return right;}
  13.     int value(){ return var; }
  14.     void setleft(Node* l){ left = l;}
  15.     void setright(Node* r){ right = r;}
  16.     bool isLeaf(){ if(left == nullptr && right == nullptr) return true; else return false;}
  17. };
  18.  
  19. class Albero{
  20.   private:
  21.   Node *root; //aggregazione
  22.   public:
  23.   Albero(Node *r):root(r){}
  24.   Node* radice(){return root;}
  25.   Node* insertion(Node *, int );
  26.   void build(Node*, int n_nodi);
  27.   void visit(Node*);
  28.   int height(Node*);
  29.   Node* BinarySearch(Node*, int);
  30.   Node* minimo(Node*);
  31.   Node* massimo(Node*);
  32. };
  33.  
  34. Node* Albero :: massimo(Node* subroot){
  35.     if(subroot == nullptr) //albero vuoto
  36.         return nullptr;
  37.     else if(subroot->rightChild() == nullptr) //figlio destro vuoto allora il masssimo risiede nella radice
  38.             return subroot;
  39.         else
  40.             return massimo(subroot->rightChild()); //autoattivazione
  41. }
  42.  
  43. Node* Albero :: minimo(Node* subroot){
  44.     if(subroot == nullptr) //albero vuoto
  45.         return nullptr;
  46.     else if(subroot->leftChild() == nullptr) //figlio sinistro vuoto allora il minimo risiede nella radice
  47.             return subroot;
  48.         else
  49.             return minimo(subroot->leftChild()); //autoattivazione
  50. }
  51.  
  52. Node* Albero :: BinarySearch(Node* subroot, int key){
  53. if(subroot == nullptr || subroot->value() == key)
  54.     return subroot;
  55. if(subroot->value() > key)
  56.     return BinarySearch(subroot->leftChild(), key);
  57. else return BinarySearch(subroot->rightChild(), key);
  58. }
  59.  
  60. int Albero :: height(Node *subroot){
  61. int s,d;
  62. if(subroot == nullptr)
  63.     return 0;
  64. else{
  65.      s = height(subroot->rightChild());
  66.      d = height(subroot->leftChild());
  67.     }
  68.     return (max(s,d)+1);
  69. }
  70.  
  71. void Albero :: visit(Node *subroot){
  72. if(subroot == nullptr){
  73.         cout<<"()";
  74.         return;
  75.         }//vuoto
  76.     cout<<"( "<<subroot->value(); //radice
  77.     visit(subroot->leftChild()); //sinistra         VISITA PREORDER
  78.     visit(subroot->rightChild());//destra
  79.     cout<<" )";
  80. }
  81.  
  82. Node* Albero :: insertion(Node *subroot, int val){
  83.   if (subroot == nullptr){
  84.     subroot = new Node(val);
  85.   }
  86.     else if(val > subroot->value()){
  87.         subroot->setright(insertion(subroot->rightChild(), val));
  88.     }
  89.         else{
  90.         subroot->setleft(insertion(subroot->leftChild(), val));
  91.     }
  92.     return subroot;
  93. }
  94.  
  95.  
  96. void Albero :: build(Node *subroot, int n_nodi){
  97.   int element;
  98.   cout<<"Building:"<<endl;
  99.   for (int i = 0; i < n_nodi; i++){
  100.     cout<<"Insert "<<i+1<<"element:";
  101.     cin>>element;
  102.     subroot = insertion(subroot, element);
  103.   }
  104. }
  105.  
  106.  
  107. int main(){
  108.  cout<<"Inserimento radice ARB: ";
  109.  int r;
  110.  cin>>r;
  111.  Node root(r);
  112.  Node* radice = &root;
  113.  Albero tree(radice);
  114.  cout<<"Inserisci il numero dei nodi che formeranno ARB: ";
  115.  int n;
  116.  cin>>n;
  117.  tree.build(radice,n);
  118.  tree.visit(radice);
  119.  cout<<"La profondita' dell' Albero e' "<<tree.height(radice);
  120.  cout<<"\nInserisci la chiave da ricercare: ";
  121.  int k;
  122.  cin>>k;
  123.  Node* s = tree.BinarySearch(radice,k);
  124.  cout<<"\nNodo: "<<s->value();
  125.  Node* mi = tree.minimo(radice);
  126.  cout<<"\nNodo con chiave minore: "<<mi->value()<<endl;
  127.  Node* ma = tree.massimo(radice);
  128.  cout<<"\nNodo con chiave minore: "<<ma->value()<<endl;
  129. }
Add Comment
Please, Sign In to add comment