Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- class Node{
- private:
- int var;
- Node *left, *right;
- public:
- Node(int v, Node *l, Node *r):var(v), left(l), right(r){}
- Node(int v):var(v), left(nullptr), right(nullptr){}
- Node* leftChild(){return left;}
- Node* rightChild(){return right;}
- int value(){ return var; }
- void setleft(Node* l){ left = l;}
- void setright(Node* r){ right = r;}
- bool isLeaf(){ if(left == nullptr && right == nullptr) return true; else return false;}
- };
- class Albero{
- private:
- Node *root; //aggregazione
- public:
- Albero(Node *r):root(r){}
- Node* radice(){return root;}
- Node* insertion(Node *, int );
- void build(Node*, int n_nodi);
- void visit(Node*);
- int height(Node*);
- Node* BinarySearch(Node*, int);
- Node* minimo(Node*);
- Node* massimo(Node*);
- };
- Node* Albero :: massimo(Node* subroot){
- if(subroot == nullptr) //albero vuoto
- return nullptr;
- else if(subroot->rightChild() == nullptr) //figlio destro vuoto allora il masssimo risiede nella radice
- return subroot;
- else
- return massimo(subroot->rightChild()); //autoattivazione
- }
- Node* Albero :: minimo(Node* subroot){
- if(subroot == nullptr) //albero vuoto
- return nullptr;
- else if(subroot->leftChild() == nullptr) //figlio sinistro vuoto allora il minimo risiede nella radice
- return subroot;
- else
- return minimo(subroot->leftChild()); //autoattivazione
- }
- Node* Albero :: BinarySearch(Node* subroot, int key){
- if(subroot == nullptr || subroot->value() == key)
- return subroot;
- if(subroot->value() > key)
- return BinarySearch(subroot->leftChild(), key);
- else return BinarySearch(subroot->rightChild(), key);
- }
- int Albero :: height(Node *subroot){
- int s,d;
- if(subroot == nullptr)
- return 0;
- else{
- s = height(subroot->rightChild());
- d = height(subroot->leftChild());
- }
- return (max(s,d)+1);
- }
- void Albero :: visit(Node *subroot){
- if(subroot == nullptr){
- cout<<"()";
- return;
- }//vuoto
- cout<<"( "<<subroot->value(); //radice
- visit(subroot->leftChild()); //sinistra VISITA PREORDER
- visit(subroot->rightChild());//destra
- cout<<" )";
- }
- Node* Albero :: insertion(Node *subroot, int val){
- if (subroot == nullptr){
- subroot = new Node(val);
- }
- else if(val > subroot->value()){
- subroot->setright(insertion(subroot->rightChild(), val));
- }
- else{
- subroot->setleft(insertion(subroot->leftChild(), val));
- }
- return subroot;
- }
- void Albero :: build(Node *subroot, int n_nodi){
- int element;
- cout<<"Building:"<<endl;
- for (int i = 0; i < n_nodi; i++){
- cout<<"Insert "<<i+1<<"element:";
- cin>>element;
- subroot = insertion(subroot, element);
- }
- }
- int main(){
- cout<<"Inserimento radice ARB: ";
- int r;
- cin>>r;
- Node root(r);
- Node* radice = &root;
- Albero tree(radice);
- cout<<"Inserisci il numero dei nodi che formeranno ARB: ";
- int n;
- cin>>n;
- tree.build(radice,n);
- tree.visit(radice);
- cout<<"La profondita' dell' Albero e' "<<tree.height(radice);
- cout<<"\nInserisci la chiave da ricercare: ";
- int k;
- cin>>k;
- Node* s = tree.BinarySearch(radice,k);
- cout<<"\nNodo: "<<s->value();
- Node* mi = tree.minimo(radice);
- cout<<"\nNodo con chiave minore: "<<mi->value()<<endl;
- Node* ma = tree.massimo(radice);
- cout<<"\nNodo con chiave minore: "<<ma->value()<<endl;
- }
Add Comment
Please, Sign In to add comment