I have to make a function in C++ that finds the external path and the internal path of a binary tree and prints them. I’m having trouble with a segfault 11 once the function for finding the paths is called, but I can’t figure out where the segfault is. I don’t have a very clear understanding of what a segfault is, all I know is that the program is trying to access a chunk of memory that’s not available. Thank you for your time.
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
using namespace std;
class Nodo
{
friend class Albero;
Nodo (int);
int valore;
Nodo *prev;
Nodo *next;
};
Nodo :: Nodo (int num)
{
valore = num;
prev = 0;
next = 0;
};
class Albero
{
public:
Albero ();
void inserisci (int); // insertion of a value;
void trovaCammino (int&, int&, int); // find path
void stampa (); //print
private:
Nodo *radice; // first item
void stampaRicorsivo (Nodo*);
void inserisciRicorsivo (Nodo *&, int);
void trovaCamminoRic (Nodo*&, int&, int&, int);
};
Albero :: Albero ()
{
radice = 0;
};
void Albero :: inserisci (int nuovo)
{
inserisciRicorsivo (radice, nuovo);
};
void Albero :: inserisciRicorsivo (Nodo*& radice, int nuovo)
{
if (radice == 0)
{
radice = new Nodo (nuovo);
} else if (nuovo < radice->valore)
{
inserisciRicorsivo(radice->prev, nuovo);
} else if (nuovo > radice->valore)
{
inserisciRicorsivo(radice->next, nuovo);
}
};
void Albero :: trovaCammino (int& contInt, int& contEst, int curr)
{
trovaCamminoRic(radice, contInt, contEst, curr);
};
void Albero :: trovaCamminoRic (Nodo*& radice, int& contInt, int& contEst, int curr)
{
if (radice->prev != 0) // if previous element is present
{
curr++; // increase current level
contInt++; // increase internal path counter
trovaCamminoRic(radice->prev, contInt, contEst, curr); // call it again on the previous element
} else if (radice->next != 0) // if next element is present...
{
curr++;
contInt++;
trovaCamminoRic(radice->prev, contInt, contEst, curr);
} else // if both are untrue, which means that the node is a leaf
{
contEst = (contEst+curr); // increase the external counter
contInt--; // decrease the internal counter by one;
}
};
void Albero :: stampa ()
{
stampaRicorsivo (radice);
};
void Albero :: stampaRicorsivo (Nodo* radice)
{
if (radice != 0)
{
stampaRicorsivo (radice->prev);
cout << radice->valore << endl;
stampaRicorsivo(radice->next);
}
;}
int main ()
{
int curr;
curr = 0;
Albero rami;
int dim;
cout << "Inserire la grandezza dell'albero che si vuole generare"; cin >> dim;
for (int i = 0; i < dim; i++)
{
int num;
cout << "Inserire il valore n° " << (i+1) << ": "; cin >> num;
rami.inserisci(num);
}
int contInt = 0;
int contEst = 0;
rami.stampa();
rami.trovaCammino(contInt, contEst, curr);
cout << "Il cammino interno dell'albero è: " << contInt << endl << "Il cammino esterno dell'albero è: " << contEst << endl;
}
>Solution :
In trovaCamminoRic, radice can be NULL, but you don’t check that, and dereferencing NULL like in radice->prev is undefined behaviour, which usually crashes your program with a segfault.
Simply add this check and it should work:
void Albero::trovaCamminoRic(Nodo*& radice, int& contInt, int& contEst, int curr)
{
if (radice == NULL)
return;
...
You should invest an hour or so in learning the basics of your debugger. With a debugger you find this kind of problems in minutes.
Disclaimer: I’m not sure what the program is supposed to do, and there may be other problems.