So I'm trying to search through a binary tree filled with a Country object stored in the node as T Data. The Country object as has String variable containing the name of the country (countryName), I want to search in a textbox for the countryName and to return a boolean value. So it would traverse the binary tree of Country objects and match the value in the textbox field with the countryName. One could store the values of the object separately in the nodes but I'd rather use generic types. Below are my searching methods.
public Boolean Contains(T item)
{
return contains(item, ref root);
}
private Boolean contains(T item, ref Node<T> tree)
{
if (tree != null)
{
if (item.CompareTo(tree.Data)==0)
{
Console.WriteLine("Found");
return true;
}
return contains(item, ref tree.Left) || contains(item, ref tree.Right);
}
else
{
return false;
}
}
And the Node structure
public T Data;
public Node<T> Left;
public Node<T> Right;
public int balanceFactor;
public Node(T Data)
{
this.Data = Data;
Left = null;
Right = null;
}