递归地按名称搜索二进制搜索树

时间:2016-05-05 18:08:20

标签: c# search recursion binary-search-tree

我有一个二叉搜索树,存储国家财务状况的详细信息。我已经添加了所有国家/地区。现在我想按国家名称搜索。

我的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 
    }
}

0 个答案:

没有答案