I want to count the number of failed students who got a grade under 60 recursively using a binary search tree in java. I don't know if my method Is correct. find the helper method to be used in the public wrapper method after
private int NumberfailStudents(int numoffailed, BNode current) {
// BaseCase
if (current == null) {
return 0 + numofpassed;
} else {
if (current.data.getGrades() < 60) {
return ++numoffailed;
}
if (current.left != null) {
return NumberfailStudents(numoffailed, current.left);
}
if (current.right != null) {
return NumberfailStudents(numoffailed, current.right);
}
}
return numoffailed;
}
If the if-condition that checking the current is null should return numoffailed
not numofpassed
I have updated the NumberFailStudents
to match the naming convention
private int NumberFailStudents(int numOfFailed, BNode current) {
if (current == null)
return numOfFailed;
if (current.data.getGrades() < 60)
++numOfFailed;
if (current.left != null)
numOfFailed = NumberFailStudents(numOfFailed, current.left);
if (current.right != null)
numOfFailed = NumberFailStudents(numOfFailed, current.right);
return numOfFailed;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.