I am struggling to understand why the function CountNodes()
below counts all the nodes in a BST.
If we assume we have the following BST:
20
/ \
10 30
/ \ / \
5 15 25 35
If I call CountNodes(pointer to root node 20);
then wouldn't the relevant if
statement:
if(root->left!=NULL)
{
n=n+1;
n=CountNodes(root->left);
}
simply look at node 10
and say, yes it is not null, add 1 to the counter n
, and then call CountNodes(pointer to 10)
which would then just send us down the left branch again to 5
. Then when at 5
the left
and right
variables are NULL
and hence the whole CountNodes
function just returns n
equal to int 3
.
I guess I am struggling to understand exactly when the value of the argument to CountNodes
is updated. Do we look right
and check if its NULL
and update the counter before we update the argument value in the first recursive call to CountNodes(pointer to 10)
in the left look even though the right look appears after the left recursive call in the code?
#include<iostream>
using namespace std;
int n=1;
struct node
{
int data;
node* left;
node* right;
};
struct node* getNode(int data)
{
node* newNode=new node();
newNode->data=data;
newNode->left=NULL;
newNode->right=NULL;
return newNode;
}
struct node* Insert(struct node* root, int data)
{
if (root == NULL)
return getNode(data);
if (data < root->data)
root->left = Insert(root->left, data);
else if (data > root->data)
root->right = Insert(root->right, data);
return root;
}
int CountNodes(node*root)
{
if(root==NULL)
return 0;
if(root->left!=NULL)
{
n=n+1;
n=CountNodes(root->left);
}
if(root->right!=NULL)
{
n=n+1;
n=CountNodes(root->right);
}
return n;
}
int main()
{
node* root=NULL;
root=Insert(root,10);
Insert(root,5);
Insert(root,20);
Insert(root,4);
Insert(root,8);
Insert(root,15);
Insert(root,25);
cout<<"Total No. of Nodes in the BST = "<<CountNodes(root)<<endl;
return 0;
}