I want to return number of values that is the most frequently in BinaryTree. I have BinaryClass which contain a lot of methods as add, contain, isEmpty, counter, iterator and other.I tried to implement this method public int getMaxFrequency() but I get a problem StackOverFlowException at markerd row.
When I run my code I get StackOverFlow Exception, anyone can help me please,
I'm new in BinaryTree.
Help me please.
enter code here
public class TreeSetCounter<T extends Comparable<T>> implements Iterable<T>{
public Node<T> root;
int size;
int count=0;
public TreeSetCounter() {
root = null;
size = 0;
}
public int counter(T t) {
return counterRecursive(root, t);
}
public int counterRecursive(Node<T> root, T t) {
int count = 0;
if(root == null) {
return 0;
}
if(root.value.equals(t)) {
count++;
}
count = count + counterRecursive(root.left, t)+ counterRecursive(root.right, t);
return count; }
public int getMaxFrequency(){
return inorder(root);
}
public int inorder( Node<T> prev) {
int count = 1, max = 0;
if (root == null) {
return 0;}
List<T> list = new ArrayList<>();
inorder(root.left); // I get the Exception att this row code.
while (prev != null) {
if (root.value == prev.value)
count++;
else
count = 1;
}
if (count > max) {
max = count;
list.clear();
list.add(root.value);
} else if (count == max) {
list.add(root.value);
}
prev = root;
inorder(root.right);
return max;
}
enter code here
Node.java
public class Node <T>{
T value;
int counter;
Node<T> left;
Node<T> right;
Node(T value, int count) {
this.value = value;
right = null;
left = null;
this.counter= count;
}
enter code here
public static void main(String[] args) {
TreeSetCounter <String> tsc= new TreeSetCounter<String>();
tsc.add("java");
tsc.add("java");
tsc.add("not");
tsc.add("cool");
tsc.add("java");
tsc.add("is");
tsc.add("java");
tsc.add("good");
System.out.println(tsc.getMaxFrequency());}