Hello All,
Yes this is homework, but I have spent a lot of time on it and I am
close.
I want to be able to count the number of nodes in a tree that have only
one child.
I can identify the nodes with the following code, but I don't know how
to sum them up and return that value to the calling function in main().
I know the algorithm is recursive in nature, but I get gibberish for
return values. I hope someone can please Help???
Here is the code and the calling function which sends the root of a
tree. I have tried iterating oneChildCount and it returns extremely
large values. The BST nodes are char.
template <typename T>
int countOneChild(t node<T*t)
{
int oneChildCount, leftChildCount, rightChildCount ;
if (t != NULL) // If it were NULL there would be no one child
nodes!
{
countOneChild(t->left); // descend left
countOneChild(t->right); // descend right
if ((t->left != NULL && t->right == NULL) || (t->left == NULL
&& t->right != NULL))
oneChildCount = 1;
else
oneChildCount = 0;
cout << oneChildCount << endl;
return oneChildCount;
}
}
Calling Function is
cout << "Number of interior nodes with one child in Tree 1 is " <<
countOneChild (root1) << endl;
I would appreciate any help, Thanx in advance,
A.J. Johnston
Yes this is homework, but I have spent a lot of time on it and I am
close.
I want to be able to count the number of nodes in a tree that have only
one child.
I can identify the nodes with the following code, but I don't know how
to sum them up and return that value to the calling function in main().
I know the algorithm is recursive in nature, but I get gibberish for
return values. I hope someone can please Help???
Here is the code and the calling function which sends the root of a
tree. I have tried iterating oneChildCount and it returns extremely
large values. The BST nodes are char.
template <typename T>
int countOneChild(t node<T*t)
{
int oneChildCount, leftChildCount, rightChildCount ;
if (t != NULL) // If it were NULL there would be no one child
nodes!
{
countOneChild(t->left); // descend left
countOneChild(t->right); // descend right
if ((t->left != NULL && t->right == NULL) || (t->left == NULL
&& t->right != NULL))
oneChildCount = 1;
else
oneChildCount = 0;
cout << oneChildCount << endl;
return oneChildCount;
}
}
Calling Function is
cout << "Number of interior nodes with one child in Tree 1 is " <<
countOneChild (root1) << endl;
I would appreciate any help, Thanx in advance,
A.J. Johnston
Comment