skull87
17-06-2008, 12:08
Ho un problema su questo codice, la traccia la potete trovare in alto al codice stesso, praticamente l'algoritmo che calcola il numero di nodi indicati nella traccia mi sembra funzioni correttamente ma non ho capito per quale motivo i nodi indicati da cancellare non vengono cancellati. Il file albero.txt di partenza è 100 92 170 4 94 110 per entrambi gli alberi.
La funzione interessata è void AeB(PAnodo , PAnodo &,int &); e le sue collegate, il resto serve quasi per la maggior parte per effettuare una stampa grafica dell'albero.
Ecco il 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 Albero{
int key;
Albero *left,*right;
};
typedef Albero *PAlbero;
using namespace std;
void StampaBST(PAlbero ,int );
void CreaAlberoDaFile(PAlbero &);
PAlbero AlberoCasualebst(int ); //Generazione Alberi Casuali Ord
void InsertSort2( PAlbero &,int , bool &);
void LegaPadre(PAlbero , PAlbero , PAlbero , PAlbero &);
void DelTNode(int , PAlbero &, bool &);
void NuovoCandidato(int , PAlbero &, PAlbero &);
void Cerca(PAlbero , int , PAlbero &, PAlbero &);
void DammiChiave(PAlbero ,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(PAlbero );
int Altezza(PAlbero );
void AeB(PAlbero , PAlbero &,int &);
void CercaNonno(PAlbero, int, PAlbero &,PAlbero &, PAlbero &);
bool cercaVal(PAlbero &,int);
int main(){
PAlbero 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(PAlbero &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){
PAlbero 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(PAlbero Tree, int KeyValue, PAlbero &Node,PAlbero &Padre, PAlbero &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(PAlbero &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(PAlbero A, PAlbero &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
PAlbero Insertbst(int info1, PAlbero As, PAlbero Ad) {
// PER INSERIRE UNA FOGLIA SI CHIAMA CON Insert(Numero,NULL,NULL)
PAlbero A2;
A2= new Albero;
A2->key=info1;
if(As>Ad) swap(As,Ad);
A2->left=As;
A2->right=Ad;
return A2;
}
PAlbero 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(PAlbero 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;
}
}
Continua...
La funzione interessata è void AeB(PAnodo , PAnodo &,int &); e le sue collegate, il resto serve quasi per la maggior parte per effettuare una stampa grafica dell'albero.
Ecco il 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 Albero{
int key;
Albero *left,*right;
};
typedef Albero *PAlbero;
using namespace std;
void StampaBST(PAlbero ,int );
void CreaAlberoDaFile(PAlbero &);
PAlbero AlberoCasualebst(int ); //Generazione Alberi Casuali Ord
void InsertSort2( PAlbero &,int , bool &);
void LegaPadre(PAlbero , PAlbero , PAlbero , PAlbero &);
void DelTNode(int , PAlbero &, bool &);
void NuovoCandidato(int , PAlbero &, PAlbero &);
void Cerca(PAlbero , int , PAlbero &, PAlbero &);
void DammiChiave(PAlbero ,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(PAlbero );
int Altezza(PAlbero );
void AeB(PAlbero , PAlbero &,int &);
void CercaNonno(PAlbero, int, PAlbero &,PAlbero &, PAlbero &);
bool cercaVal(PAlbero &,int);
int main(){
PAlbero 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(PAlbero &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){
PAlbero 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(PAlbero Tree, int KeyValue, PAlbero &Node,PAlbero &Padre, PAlbero &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(PAlbero &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(PAlbero A, PAlbero &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
PAlbero Insertbst(int info1, PAlbero As, PAlbero Ad) {
// PER INSERIRE UNA FOGLIA SI CHIAMA CON Insert(Numero,NULL,NULL)
PAlbero A2;
A2= new Albero;
A2->key=info1;
if(As>Ad) swap(As,Ad);
A2->left=As;
A2->right=Ad;
return A2;
}
PAlbero 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(PAlbero 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;
}
}
Continua...