我有一个二叉搜索树,存储国家财务状况的详细信息。我已经添加了所有国家/地区。现在我想按国家名称搜索。
我的BST课程中有一个查找方法,但是当我调用它时它不会打印任何内容。
public object find(string country)
{
return find(country, root);
}
private object find(string item, Node<T> tree)
{
int compare;
while (tree != null)
{
string[] outcome = tree.Data.ToString().Split(',');
compare = outcome[0].CompareTo(item);
if (compare == 0)
{
return tree.Data;
}
else if (compare > 0)
{
tree = tree.Left;
}
else if (compare < 0)
{
tree = tree.Right;
}
}
return null;
此代码位于我的表单类:
public object findcountryInformation(string countryName)
{
object result = myTree.find(countryName);
return result;
}
private void button1_Click(object sender, EventArgs e)
{
string text = listBox1.GetItemText(listBox1.SelectedItem);
object result = findcountryInformation(text);
Console.WriteLine(result);
// expected output is details of the countries selected from the listbox
}
}