What binary search tree operation does the method below define? int method(Node
ID: 3791310 • Letter: W
Question
What binary search tree operation does the method below define?
int method(Node r)
{
int x=0, y=0;
if (r.leftChild() != null) x = method(leftChild());
if (r.rightChild() != null) y = method(rightChild());
return 1 + x + y;
}
It determines the minimum of the values in the binary search tree.
It determines the maximum of the values in the binary search tree.
It counts the number of elements in the binary search tree.
It computes the average of the elements stored in the binary search tree.
Explanation / Answer
It counts the number of elements in the binary search tree.
int method(Node r)
{
int x=0, y=0;
if (r.leftChild() != null) x = method(leftChild()); //traverse the left sub tree
if (r.rightChild() != null) y = method(rightChild()); //traverse the right sub tree
return 1 + x + y; // add nodes
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.