codice:
/*
Note : Sia A un albero qualsiasi e B un albero binario ordinato
Scrivere una procedura che calcoli quanti elementi
di B con almeno un figlio sono presenti in A, cancellando
contemporaneamente i nodi foglia di B presenti in A.
*/
#include <cstdlib>
#include <iostream>
#include <fstream>
#include <math.h>
#define TIPO_DATO int
#define OFFSET_SPACE 5
struct node {
int key;
struct node* left;
struct node* right;
};
struct node1{
TIPO_DATO *info; //Puntatore all'informazione tenuta dal nodo
struct node1 *left; //Puntatore sinistro
struct node1 *right; //Puntatore destro
};
struct elemCoda{
struct node1 *elem;
struct elemCoda *next;
};
struct Anodo{
int key;
Anodo *left,*right;
};
typedef Anodo *PAnodo;
using namespace std;
void StampaBST(PAnodo ,int );
void CreaAlberoDaFile(PAnodo &);
PAnodo AlberoCasualebst(int ); //Generazione Alberi Casuali Ord
void InsertSort2( PAnodo &,int , bool &);
void DammiChiave(PAnodo ,int &);
void writeLevelTree(struct node1 *root); //Per Stampa Grafica
struct node1 *buildTree(); //Per Stampa Grafica
struct node1 *insertInOrder(struct node1 *root, TIPO_DATO info); //Per Stampa Grafica
void pfileorder(PAnodo );
int Altezza(PAnodo );
void AeB(PAnodo , PAnodo &,int &);
void CercaNonno(PAnodo, int, PAnodo &,PAnodo &, PAnodo &);
bool cercaVal(PAnodo &,int);
int main(){
PAnodo A,B;
struct node1 *root = NULL;
CreaAlberoDaFile(A);
//StampaBST(A,0);
//system("pause");
//Crea albero
printf("\n\t\t .: 1 :. Costruzione albero...\n");
root = buildTree();
if (root==NULL) return 1;
//Stampa albero per livelli
system("cls");
writeLevelTree (root);
printf("\n\n\n");
system("pause");
//StampaBST(A,0);
CreaAlberoDaFile(B);
//B=AlberoCasualebst(3);
pfileorder(B);
//system("pause");
//Crea albero
printf("\n\t\t .: 1 :. Costruzione albero...\n");
root = buildTree();
if (root==NULL) return 1;
//Stampa albero per livelli
// system("cls");
writeLevelTree (root);
printf("\n\n\n");
cout<<endl;
cout<<endl;
system("pause");
int nodi=0;
AeB(A,B,nodi);
cout<<"Nodi: "<<nodi<<endl;
pfileorder(B);
//system("pause");
//Crea albero
printf("\n\t\t .: 1 :. Costruzione albero...\n");
root = buildTree();
if (root==NULL) return 1;
//Stampa albero per livelli
// system("cls");
writeLevelTree (root);
printf("\n\n\n");
cout<<endl;
cout<<endl;
system("pause");
system("pause");
return 0;
}
bool cercaVal(PAnodo &Tree, int val){
if(Tree==NULL)
return false;
else if(Tree->key==val && (Tree->left!=NULL || Tree->right!=NULL))
return true;
else if(Tree->key==val && Tree->left==NULL && Tree->right==NULL){
PAnodo Padre=NULL,Nonno=NULL;
CercaNonno(Tree,Tree->key,Tree,Padre,Nonno);
if(Padre!=NULL){
if(Padre->left!=NULL && Padre->left->key==val)
Padre->left=NULL;
else if(Padre->right!=NULL && Padre->right->key==val)
Padre->right=NULL;
}
return false;
}
else if(Tree->key>val)
return cercaVal(Tree->left,val);
else
return cercaVal(Tree->right,val);
}
void CercaNonno(PAnodo Tree, int KeyValue, PAnodo &Node,PAnodo &Padre, PAnodo &Nonno){
int NodesKey;
Nonno=NULL;
Padre=NULL;
Node=Tree;
DammiChiave(Node, NodesKey) ;
while ((Node!=NULL) && (NodesKey!=KeyValue))
{ Nonno=Padre;
Padre=Node;
if (NodesKey>KeyValue)
Node=Node->left;
else
Node=Node->right;
DammiChiave(Node, NodesKey);
}
}
int sommaNodi(PAnodo &Tree){
int k=0;
if(Tree!=NULL){
if(Tree->left!=NULL && Tree->right!=NULL) k=Tree->key;
else k=0; //non è una foglia
return sommaNodi(Tree->left)+sommaNodi(Tree->right)+k;
}
else
return 0;
}
void AeB(PAnodo A, PAnodo &B,int &nodi){
if(A!=NULL){
if(cercaVal(B,A->key)) nodi++;
AeB(A->left,B,nodi);
AeB(A->right,B,nodi);
}
}
//Generazione di albero BST ordinato
PAnodo Insertbst(int info1, PAnodo As, PAnodo Ad) {
// PER INSERIRE UNA FOGLIA SI CHIAMA CON Insert(Numero,NULL,NULL)
PAnodo A2;
A2= new Anodo;
A2->key=info1;
if(As>Ad) swap(As,Ad);
A2->left=As;
A2->right=Ad;
return A2;
}
PAnodo AlberoCasualebst(int n)
{
//Dato un intero n restituisce un albero di interi di altezza n ORDINATO
if (n==0) {
//srand ( time(0) );
return Insertbst(rand()%100 ,NULL,NULL);
}
else{
//srand ( time(0) );
return Insertbst(rand()%100,AlberoCasualebst(n-1),AlberoCasualebst(n-1));
}
}
int Altezza(PAnodo A)
{ // CALCOLA L’ALTEZZA DELL’ALBERO A
int Hs, Hd;
if (A==NULL)
return -1;
else {
Hs=Altezza(A->left); Hd=Altezza(A->right);
if (Hs > Hd)
return ++Hs;
else
return ++Hd;
}
}