Function to return the number of parent nodes in a binary tree
Nov 23, 2015 at 9:08pm UTC
I am writing a program for my class and I can not seem to get this last function to work. I have to return the number of parent nodes in the binary tree. Here is what I have:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
int binaryTreeType::parentNodeCount(Node *p)
{
if (p == NULL)
return 0;
else
if (p->llink == NULL && p->rlink == NULL)
return 1;
else
if (p->llink == NULL && p->rlink != NULL)
return 1 + parentNodeCount(p->rlink);
else
if (p->rlink == NULL && p->llink != NULL)
return 1 + parentNodeCount(p->llink);
else
return parentNodeCount(p->llink);
}
Not sure if I am on the right track or not.
Last edited on Nov 23, 2015 at 9:11pm UTC
Nov 24, 2015 at 2:14am UTC
1 2 3 4 5
int binaryTreeType::parentNodeCount(Node *p){
if (!p)
return 0;
return 1 + this ->parentNodeCount(p->llink) + this ->parentNodeCount(p->rlink);
}
I think just "nodeCount" would be a more appropriate name.
Last edited on Nov 24, 2015 at 2:25am UTC
Nov 24, 2015 at 2:23pm UTC
Does "parent nodes" mean non-leaf nodes?
Topic archived. No new replies allowed.