Im creating a method to count the frequency of a value that is the methods parameter. I have created a binary tree and put values into it, im now trying to count all the values that equals the parameter. The problem is im getting a infinite loop, can anybody see why:
public int counter(T value)
{
if (value == null) return 0;
Node<T> p = root, q = null; // q is the parent of p
int values = 0;
while (p != null) // checks p
{
int cmp = comp.compare(p.value,value);// compares
if(p.value.equals(value))
{
values++;
}
if (cmp < 0) { q = p; p = p.left; } // goes left in the tree
else if (cmp > 0) { q = p; p = p.rigth; } goes right in the tree
if ( p == null) return values;
}
return 0; // if no values found in the tree
}
Constructor:
private Node(T value, Node<T> l, Node<T> r)
{
this.value = value;
left = l; right = r;
}