by Kai-Uwe Bux <jkherciueh@[EMAIL PROTECTED]
>
Mar 3, 2008 at 09:45 AM
noagbodjivictor@[EMAIL PROTECTED]
wrote:
> Hi all,
>
> I thought I knew how pointers work, but then I'm struggling with a
> code, well I happen to have written last semester in my data structure
> class. Why do we pass the pointer to the node of a binary tree by
> reference? Isn't a pointer supposed to hold the address of an object
> in memory?
>
> Take this for example:
> ItemType findMax(TreeNode* & tree) {
> while(tree->right != NULL)
> tree = tree->right;
> return tree->info;
> }
>
> Why are we are passing the pointer tree by reference here? Don't be
> too harsh, I just happen to have forgotten a little bit...
The code seems to search for the right-most leaf in a tree. What passing
the
parameter tree by reference does for you is that the function changes the
variable so that it will point to the right-most leaf after the call.
Best
Kai-Uwe Bux