Java What would be the correct coding for the questions Ques

Java, What would be the correct coding for the questions

Question 10 worth 2 marks write a Java code to recursively count all the leaves in a general tree. Add the count Leaves method, and its implementation to this Node class: public class Node public List

Solution

Solution Question:10

public int countLeaves()
{
int count=0;
List<Node> nodes = new ArrayList<Node>();
if (isLeaf()) {
nodes.add(this);
} else {
for (Node child: children) {
nodes.addAll(child.getAllLeafNodes());
count++;
}
}
return count;
}

  

Solution Question:11

String binarySearch(Entry [] array, int key)
{
int first,last ,middle,n;
String result;
n=array.length; // length of array.
first = 0;
last = n - 1;
middle = (first+last)/2;

while (first <= last) {
if (array[middle] < key)
first = middle + 1;
else if (array[middle] == key) {
result=\"key is found\";
break;
}
else
last = middle - 1;

middle = (first + last)/2;
}
if (first > last)
result=\"key is not found\";

return result;   
}

public boolean isLeaf() {
return children == null || children.isEmpty();
}
Java, What would be the correct coding for the questions Question 10 worth 2 marks write a Java code to recursively count all the leaves in a general tree. Add

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site