My question involves the bit of code in the function below called inOrder. I know the code works because I got it from a book and tested it, so that's not the problem.
I'm trying to map this function out on paper, but I don't understand how it can work if the second line keeps recursively calling the function again.
Assuming this is the bTree:
I'm thinking this happens:
1. pointer (1) does not equal to NULL
2. call inOrder (pass in the pointer to left)
3. pointer (-3) equals to NULL
4. since pointer equals to know, the if statement does not execute and nothing happens.
I know this is not right, but I don't see how. Could someone please walk me through the function?
Thanks so much.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
|
struct node {
int info;
int *left;
int *right;
};
class bTree {
//various class methods not related to the question...
public:
void inOrderTraversal ();
private:
void inOrder (int *);
node *root;
};
void bTree::inOrderTraversal ()
{
inOrder (root);
}
void bTree::inOrder (int *pointer)
{
if (pointer != NULL) {
inOrder (pointer ->left);
cout << pointer -> info << " ";
inOrder (pointer ->right);
}
}
|