[problemi]Alberi binari in C
Inviato: domenica 6 aprile 2014, 10:27
Devo scrivere una libreria che fa varie operazioni sugli alberi binari,ma quelle più complesse mi danno problemi di segmentation fault.
[code2=cpp]#include "abr_header_gc.h"
const char alfa[] = {'a','b','c','d','e','f','g','h','i','l','m','n','o','p','q','r','s','t','u','v','z'};
int readS(char *str){
int i=0;
char c;
while ( (c=getchar())!= '\n' ) {
str+=c;
i=i+1;
}
return i;
}
abr_node *insertElem(abr_node* root,char *str){ //Funziona
if (root==NULL) {
root = (abr_node *)malloc(sizeof(abr_node));
root->left = NULL;
root->right = NULL;
root->s=(char *)malloc(sizeof(char)*(strlen(str)+1));
strcpy(root->s,str);
return root;
}
else if( strcmp(str,root->s)<0 ){
root->left = insertElem(root->left,str);
return root;
}
else{
root->right = insertElem(root->right,str);
return root;
}
return root;
}
void stampaAbr(abr_node *root){ //Funziona
if(root!=NULL){
stampaAbr(root->left);
printf("%s\n",root->s);
stampaAbr(root->right);
}
}
void eliminaAbr(abr_node* root){ //Funziona
if(root->left!=NULL){
eliminaAbr(root->left);
}
if(root->right!=NULL){
eliminaAbr(root->right);
}
free(root);
}
abr_node* cancellaElem(abr_node *root,char *str){ //Funziona
if(strcmp(root->s,str)==0){
if(root->left==NULL){
return root->right;
}
else if(root->right==NULL){
return root->left;
}
else if(root->left==NULL&&root->right==NULL){
return NULL;
}
else{
abr_node *minimum;
minimum=root->right;
while(minimum->left!=NULL){
minimum=root->left;
}
minimum->left=root->left;
return root->right;
}
}
if(root->left!=NULL){
root->left = cancellaElem(root->left,str);
}
if(root->right!=NULL){
root->right= cancellaElem(root->right,str);
}
return root;
}
void cancellaConCondizione(abr_node *root,int p_o_d,char *a,char *b){
//dovrebbe cancellare dall'albero tutte le stringhe che hanno lunghezza pari o dispari
//e sono comprese tra le stringhe a e b e ogni volta mi da errore di segmentation fault, o
//rimane l'albero invariato.
}
void abrCasuale(abr_node *root,int n){
//funziona solo se io ho già inserito un altro elemento,ma se l'albero è null non funziona,anche se io uso sempre
//insertElem sempre nello stesso modo
int i,j;
char *str=(char *)malloc(sizeof(char)*10);
for(i=0;i<n;i++){
for(j=0;j<10;j++){
str[j]=alfa[rand()%strlen(alfa)];
}
root = insertElem(root,str);
}
}
void dupAbr(abr_node *root1,BTree *t2){ //Funziona
if(root1!=NULL){
t2->root = insertElem(t2->root,root1->s);
}
if(root1->left!=NULL){
dupAbr(root1->left,t2);
}
if(root1->right!=NULL){
dupAbr(root1->right,t2);
}
}
int abrCmp(abr_node *root1,abr_node *root2,int i){ //Funziona
if((root1!=NULL && root2!=NULL) || (root1!=NULL || root2!=NULL)){
if(strcmp(root1->s,root2->s)!=0){
i=1;
printf("sono diversi");
return i;
}
}
if(root1->left!=NULL){
return abrCmp(root1->left,root2->left,0);
}
if(root1->right!=NULL){
return abrCmp(root1->right,root2->right,0);
}
return 0;
}
void vectAbr(abr_node *root,char **a,int n,int *riemp){ //Funziona
if(root!=NULL){
vectAbr(root->left,a,n+1,riemp);
a[n]=(char *)malloc(sizeof(char)*strlen(root->s));
*riemp = *riemp + 1;
strcpy(a[n],root->s);
vectAbr(root->right,a,n+1,riemp);
}
}
void abrBilanciato(abr_node *nuovo,char **a,int p,int q,int riemp){
//Con l'array che ho creato precedentemente con la funzione sopra
//a ogni ricorsione inserisco l'elemento n/2, e passo i parametri cambiati in modo che
// p e q rappresentino gli estremi e io prendo sempre q-p/2,ma non funziona.
if(q-p==1){
nuovo = insertElem(nuovo,a[q-p]);
}
else{
nuovo = insertElem(nuovo,a[(q-p)/2]);
abrBilanciato(nuovo->left,a,p,q/2,riemp-1);
abrBilanciato(nuovo->right,a,(q-p)/2,q,riemp-1);
}
}[/code2]
Se mi potete dire dove sbaglio, perchè le funzioni semplici funzionano tutte, sopratutto quelle di lettura, mentre tutte queste sono di modifica dell'albero e ho pensato che magari sbaglio a fare qualcosa.
[code2=cpp]#include "abr_header_gc.h"
const char alfa[] = {'a','b','c','d','e','f','g','h','i','l','m','n','o','p','q','r','s','t','u','v','z'};
int readS(char *str){
int i=0;
char c;
while ( (c=getchar())!= '\n' ) {
str+=c;
i=i+1;
}
return i;
}
abr_node *insertElem(abr_node* root,char *str){ //Funziona
if (root==NULL) {
root = (abr_node *)malloc(sizeof(abr_node));
root->left = NULL;
root->right = NULL;
root->s=(char *)malloc(sizeof(char)*(strlen(str)+1));
strcpy(root->s,str);
return root;
}
else if( strcmp(str,root->s)<0 ){
root->left = insertElem(root->left,str);
return root;
}
else{
root->right = insertElem(root->right,str);
return root;
}
return root;
}
void stampaAbr(abr_node *root){ //Funziona
if(root!=NULL){
stampaAbr(root->left);
printf("%s\n",root->s);
stampaAbr(root->right);
}
}
void eliminaAbr(abr_node* root){ //Funziona
if(root->left!=NULL){
eliminaAbr(root->left);
}
if(root->right!=NULL){
eliminaAbr(root->right);
}
free(root);
}
abr_node* cancellaElem(abr_node *root,char *str){ //Funziona
if(strcmp(root->s,str)==0){
if(root->left==NULL){
return root->right;
}
else if(root->right==NULL){
return root->left;
}
else if(root->left==NULL&&root->right==NULL){
return NULL;
}
else{
abr_node *minimum;
minimum=root->right;
while(minimum->left!=NULL){
minimum=root->left;
}
minimum->left=root->left;
return root->right;
}
}
if(root->left!=NULL){
root->left = cancellaElem(root->left,str);
}
if(root->right!=NULL){
root->right= cancellaElem(root->right,str);
}
return root;
}
void cancellaConCondizione(abr_node *root,int p_o_d,char *a,char *b){
//dovrebbe cancellare dall'albero tutte le stringhe che hanno lunghezza pari o dispari
//e sono comprese tra le stringhe a e b e ogni volta mi da errore di segmentation fault, o
//rimane l'albero invariato.
}
void abrCasuale(abr_node *root,int n){
//funziona solo se io ho già inserito un altro elemento,ma se l'albero è null non funziona,anche se io uso sempre
//insertElem sempre nello stesso modo
int i,j;
char *str=(char *)malloc(sizeof(char)*10);
for(i=0;i<n;i++){
for(j=0;j<10;j++){
str[j]=alfa[rand()%strlen(alfa)];
}
root = insertElem(root,str);
}
}
void dupAbr(abr_node *root1,BTree *t2){ //Funziona
if(root1!=NULL){
t2->root = insertElem(t2->root,root1->s);
}
if(root1->left!=NULL){
dupAbr(root1->left,t2);
}
if(root1->right!=NULL){
dupAbr(root1->right,t2);
}
}
int abrCmp(abr_node *root1,abr_node *root2,int i){ //Funziona
if((root1!=NULL && root2!=NULL) || (root1!=NULL || root2!=NULL)){
if(strcmp(root1->s,root2->s)!=0){
i=1;
printf("sono diversi");
return i;
}
}
if(root1->left!=NULL){
return abrCmp(root1->left,root2->left,0);
}
if(root1->right!=NULL){
return abrCmp(root1->right,root2->right,0);
}
return 0;
}
void vectAbr(abr_node *root,char **a,int n,int *riemp){ //Funziona
if(root!=NULL){
vectAbr(root->left,a,n+1,riemp);
a[n]=(char *)malloc(sizeof(char)*strlen(root->s));
*riemp = *riemp + 1;
strcpy(a[n],root->s);
vectAbr(root->right,a,n+1,riemp);
}
}
void abrBilanciato(abr_node *nuovo,char **a,int p,int q,int riemp){
//Con l'array che ho creato precedentemente con la funzione sopra
//a ogni ricorsione inserisco l'elemento n/2, e passo i parametri cambiati in modo che
// p e q rappresentino gli estremi e io prendo sempre q-p/2,ma non funziona.
if(q-p==1){
nuovo = insertElem(nuovo,a[q-p]);
}
else{
nuovo = insertElem(nuovo,a[(q-p)/2]);
abrBilanciato(nuovo->left,a,p,q/2,riemp-1);
abrBilanciato(nuovo->right,a,(q-p)/2,q,riemp-1);
}
}[/code2]
Se mi potete dire dove sbaglio, perchè le funzioni semplici funzionano tutte, sopratutto quelle di lettura, mentre tutte queste sono di modifica dell'albero e ho pensato che magari sbaglio a fare qualcosa.