Click here to Skip to main content
15,883,623 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
i have a binary search tree that stores details of countries financial situation,

i have added all the countries now i just want to search them by countryname

i have a find method in my BST class that is supposed to do that but it doesnt work, it doesent print anything when i call it;

C#
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;


in my form class i also have this code

C#
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 should be details of the countries selected from the listbox

C#
class BSTree<T> : BinTree<T> where T : IComparable
   {
       public BSTree()
       {
           root = null;
       }

       public void InsertItem(T item)
       {
           insertItem(item, ref root);
       }

       private void insertItem(T item, ref Node<T> tree)
       {
           if (tree == null)
               tree = new Node<T>(item);

           else if (item.CompareTo(tree.Data) < 0)
               insertItem(item, ref tree.Left);

           else if (item.CompareTo(tree.Data) > 0)
               insertItem(item, ref tree.Right);

       }

       private int max(int x, int y)
       {
           if (y > x)
               return y;

           return x;
       }

       public int Height()
       {
           return height(root);
       }

       private int height(Node<T> tree)
       //Return the max level of the tree
       {
           if (tree == null)
               return 0;
           return 1 + max(height(tree.Left), height(tree.Right));
       }

       public Boolean Contains(T item)
       {
           Node<T> node = root;
           return contains(root, item);
       }

       private Boolean contains(Node<T> node, T item)
       {
           if (node == null)
               return false;

           //if (node.Data == item)
           if (item.CompareTo(node.Data) < 0)
               return contains(node.Left, item);
           if (item.CompareTo(node.Data) > 0)
               return contains(node.Right, item);
           return true;
       }


node class

C#
    class Node<T> where T : IComparable
        
    {
        
        private T data;
        public Node<T> Left, Right;


        public Node(T item)
        {
            data = item;
            Left = null;
            Right = null;
        }
        public T Data
        {
            set { data = value; }
            get { return data; }
        }


    }
}


What I have tried:

i've tried everything that i could think of including debugging but i didnt get much insight
Posted
Updated 5-May-16 9:31am
v2
Comments
Sergey Alexandrovich Kryukov 5-May-16 14:25pm    
Why recursively?
—SA
corupted 5-May-16 14:29pm    
i was told to use the recursive algorithm instead of iterative, is it possible to do it recursively?
Sergey Alexandrovich Kryukov 5-May-16 15:19pm    
It's of course possible. How else? What's the problem? Use recursion. You never tried it.
And, as to the debugging: it's not about "trying". It's about using the debugger until you figure out the problem.
—SA
BillWoodruff 5-May-16 14:43pm    
I'm pretty sure it will be easy to use Linq to do what you want, but to be more specific I think I would need to understand the exact structure of your "binary tree" ... what is stored in the left/right child-nodes ?

Also, I suggest that you consider storing the data in some 'object form, like in instances of a struct, or class, rather than as strings that you'll have to call split on with every search: that's incredibly inefficient.
corupted 5-May-16 14:52pm    
I have update my question with the binary search tree class and node class, i also have a country class where i store all the country information i then load that class into the binary tree as my data

Quote:
i've tried everything that i could think of including debugging but i didnt get much insight
At least, using the debugger, you should be able to see if find returns something and what, if it fails, you will see why too.
It is complicated to say something else without the data.
 
Share this answer
 
v2
Comments
BillWoodruff 6-May-16 1:04am    
My vote of #1. This is not any kind of "solution." It's just a quickly written general comment with some (good) advice for the OP. Do you really need rep-points this badly ?
Patrice T 6-May-16 2:41am    
Not that much. It is just that I don't see any obvious flaw, but without a complete piece of code; it is complicated to get the logic.
The OP says that he used the debugger but did not get much insight. I am sure that he misuse the debugger.
First of all, you should prefer non-recursive solutions in such cases. A tree can have any depth, so, theoretically speaking, you can face stack overflow (and even practically, for big data sets). Also, recursion may compromise your performance. Trees are perfectly searched iteratively.

Recursion should be used in more appropriate situations. Say, you have some metadata with classes (meta-classes). The inheritance depth is never too high, so you can easily walk up the hierarchy in any direction. There are many other cases when recursion is good.

Now, about recursion. It looks like you don't know what is that. Otherwise you would at least try it. Please see:
Recursion — Wikipedia, the free encyclopedia[^],
Recursion (computer science) — Wikipedia, the free encyclopedia[^].

Isn't that clear already? If not, I don't want to give you the whole solution. You really need to do all the basic work all by yourself, otherwise you could not learn anything. So, you need only one hint: during execution of the code of find, you have to call find — on other node. Also, you need to break out of recursion eventually and not allow "infinite" recursion. Please see: Broken Recursion[^]. :-)

By the way, did you get my saying referenced above? Not that this joke is really funny, but, in a way, it's true. Another thing is funny: not only the post talks about "infinite" (or not) recursion, but the nature of the speculations is recursive reasoning, also "infinite". :-)

—SA
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900