Questions about count tree in the binary search function I writed these two func
ID: 3863062 • Letter: Q
Question
Questions about count tree in the binary search function I writed these two functions:
void MovieTree::countMovieNodes(MovieNode *node, int *c)
{
if(node->leftChild != NULL)
{
countMovieNodes(node->leftChild, c+1);
}
else if(node->rightChild != NULL)
{
countMovieNodes(node->rightChild, c+1);
}
}
int MovieTree::countMovieNodes()
{
if (root!=NULL)
{
int *c=new int;
countMovieNodes(root, c);
return *c;
}
else
{
return 0;
}
}
But when I tested my function in my whole project, I can't get a right answer. I have 50 terms in my BST. But When I use these functions, I always got a large number. can anyone help me to modify these two functions? (plz don't change my functions or the variables, just using these two)
void MovieTree::countMovieNodes(MovieNode *node, int *c)
int MovieTree::countMovieNodes()
Explanation / Answer
After looking at your code the main mistakes you are making are
1 int* c = new int;
also add *c=0; or you can use new int() instead of new int
2 When increment c you have to do it as *c++; or *c=*c+1; before calling I think that will be more correct.
3 when you are counting you are skipping the root to be counted so you also have to add +1 to the end result or start with *c=1;
Let me know if this works!
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.