I am working on Binary Search Trees for a class and I am getting a Seg fault when I check to see if the tree (root) is NULL. I don't understand why it cant get past the IF() statement.
This is where it is seg faulting. Right at the if(!root) statement
1 2 3 4 5 6 7 8 9 10 11 12 13 14
int tree::add(node *& root, const job & to_add)
{
if(!root)
{
root= new node;
root->a_job.copy(to_add);
root->left=NULL;
root->right=NULL;
}
elseif(root->a_job.comp_comp(to_add)>=0)
...
The pointer is passed from here: I just need this statement to pass add the node pointer (root) to the function so I can use recursion.
1 2 3 4 5 6 7 8
int tree::add(const job & to_add)
{
add(root,to_add);
return 1;
}
this is the class:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
// this class controls the growing chain of jobs int a binary search tree. If there are more than one company it will then organize it
// by the job tile.
class tree
{
public:
tree();
~tree();
void display();
int find(char* to_find);
int add(node *& root, const job & to_add);
int add(const job & to_add);
int remove(char* to_remove);
private:
node * root; // begining of our tree.
int height; // will keep track of our tree height.
I don't understand why I cant check if root is NULL, I do it with linked lists all the time...
well it is initialized, but I initialized it to NULL in the constructor... is that not correct?
1 2 3 4 5 6 7 8 9 10 11 12 13 14
//manages the tree class as and is the interface between the user and the ADT. this class takes in information from the user converts the information into a key and send the information to the corrosponding tree index.
class interface
{
public:
interface();
~interface();
int add( char company [], char job_t [], char descrip [],
char location [], int rating, char ** skills, int skills_count, char ** lang, int lang_count);
void find (char* job_to_find);
void display_all();
void remove(char* job_to_remove);
private:
tree * a_tree;
};
Null means it doesn't point to an object so you can't access any of the member variables or call any of the member functions because there is no object to do it on.