I
Indrajeet
I was looking at this function segment that inserts a node into a
binary tree :
void insert(Tree** pRoot, int n)
{
if (*pRoot != NULL)
{
if ((*pRoot)->val > n)
insert(&((*pRoot)->left),n);
else
insert(&((*pRoot)->right),n);
}
else
{
Tree* new = (Tree *)malloc(sizeof(Tree*));
new->val = n;
new->left = NULL;
new->right = NULL;
*pRoot = new;
}
}
main() makes successive calls to insert as in insert(&root,35); insert
(&root, 37); insert(&root, 39);
A preOrder listing as in preOrder(&root) prints correctly.
Since 39 is the last created node and the line in insert() goes
"*pRoot = new", would this not alter the variable root in main() to
point to the node that holds the last added value, viz. 39? Pardon me
if my understanding of pointers is fuzzy, I'd be much grateful for an
explanation showing where my understanding has got holes, obviously
I've got some way to go
binary tree :
void insert(Tree** pRoot, int n)
{
if (*pRoot != NULL)
{
if ((*pRoot)->val > n)
insert(&((*pRoot)->left),n);
else
insert(&((*pRoot)->right),n);
}
else
{
Tree* new = (Tree *)malloc(sizeof(Tree*));
new->val = n;
new->left = NULL;
new->right = NULL;
*pRoot = new;
}
}
main() makes successive calls to insert as in insert(&root,35); insert
(&root, 37); insert(&root, 39);
A preOrder listing as in preOrder(&root) prints correctly.
Since 39 is the last created node and the line in insert() goes
"*pRoot = new", would this not alter the variable root in main() to
point to the node that holds the last added value, viz. 39? Pardon me
if my understanding of pointers is fuzzy, I'd be much grateful for an
explanation showing where my understanding has got holes, obviously
I've got some way to go