4) A binary tree class uses the BSTNode<T> class for its nodes and has one field
ID: 3630743 • Letter: 4
Question
4) A binary tree class uses the BSTNode<T> class for its nodes and has one field:
private BSTNode<T> root;
Write a method for this class which returns the number of nodes in the tree. (You may, write two methods where one calls the other to accomplish this.)
The following class is used in problems 4.
public class BSTNode<T extends Comparable<T>>
{
private T data;
private BSTNode<T> left = null, right = null;
public BSTNode(T data)
{
this.data = data;
}//end of method
// public get and set methods are here
// for the three fields
}//end of class
Explanation / Answer
int size(struct node* node) {
if (node==NULL) {
return(0);
} else {
return(size(node->left) + 1 + size(node->right));
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.