The problem with my program is that whenever I run it, it prints out a zero even though a zero is not one of the numbers in the file. Also the count is sometimes wrong. Any suggestions?
void insert(int key, tree *&p) // Note: &
{
if(p) // Note
{
if(key < p->info)
{
if(p->left != NULL)
insert(key, p->left);
else
{
p->left = new tree;
p->left->info = key;
p->left->left = NULL; //Sets the left child of the child node to null
p->left->right = NULL; //Sets the right child of the child node to null
}
}
elseif(key>=p->info)
{
if(p->right!= NULL)
insert(key, p->right);
else
{
p->right = new tree;
p->right->info = key;
p->right->left = NULL; //Sets the left child of the child node to null
p->right->right = NULL; //Sets the right child of the child node to null
}
}
}
else // Note
{
p = new tree;
p->info = key;
p->left = NULL;
p->right = NULL;
}
}
...
int main(){
tree *x = NULL;
...
Are you aware that you do not insert. It's only appending (it may not a problem if intended)
Thanks coder777, the change worked. Can you also go into more detail about why the insert function isn't really an insert?
I understand that it appends the tree and adds a new leaf but why wouldn't that be considered an insert?