cplusplus
.com
TUTORIALS
REFERENCE
ARTICLES
FORUM
C++
Tutorials
Reference
Articles
Forum
Forum
Beginners
Windows Programming
UNIX/Linux Programming
General C++ Programming
Lounge
Jobs
Forum
Windows Programming
please tell me what's wrong in my code.
please tell me what's wrong in my code.
Feb 15, 2011 at 4:38pm UTC
akshayjain
(1)
HERE IS A PROGRAM TO INSERT AN ELEMENT IN BINARY TREE
tree *create_new_node(int n)
{
np=new tree;
np->data=n;
np->left=NULL;
np->right=NULL;
return np;
}
tree *insert(int item,tree *t)
{
np=create_new_node(item);
if(t==NULL)
{
t=np;
return t;
}
else
{
if(item < t->data)
return insert(item,t->left);
else
return insert(item,t->right);
}
}
in this code can u please tell me HOW?? the return statement works and what's wrong with it.
Feb 15, 2011 at 6:21pm UTC
kbw
(9488)
The parameter t passed to insert is passed by value. So when you make changes to it within the function, they're not reflected outside of the function. You need to pass the tree* by reference some how.
Topic archived. No new replies allowed.