4 Replies - 3730 Views - Last Post: 22 March 2012 - 03:24 PM Rate Topic: -----

#1 ChaosEvaUnit   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 33
  • Joined: 18-November 11

Searching a Binary Search Tree for a String within an Object

Posted 22 March 2012 - 09:17 AM

Greetings.
Currently building a database of Criminals using a Binary Search Tree.
This is all being front-ended by a GUI in Visual Studio.
Criminal consists of general attributes,Name/Age/Height etc, but also has a LinkedList<String> of Offences.
Once the Criminal is made it is put into the BST.
I then need to be able to search a Criminal by Name(string) which I'm not sure how to do. I have a Contains method in my BST class which takes a Criminal as a parameter and I also have a CompareTo method in my Criminal class, but also takes a Criminal as a parameter. So I'm too sure how to go about doing.
I'm not going to include all my code, that would be a bit much. So I'm going to try and include just the relevant snippets for ease of reading.

        private void searchCbutton_Click(object sender, EventArgs e)
        {
            //This is where I have no clue of what method to call and how to go about it.
            
            /*Criminal tempC = new Criminal();
            searchCfield.Text = tempC.Name;
            BST.Contains();
            searchCfield.Text.CompareTo(tempC);*/
        }


            Boolean contains(Node<T> tree, T Item) //Binary Search Tree Contains Methods
            {
                if (tree == null)
                    return false;

                if (Item.CompareTo(tree.Data) == 0)
                    return true;

                else if (Item.CompareTo(tree.Data) > 0)
                    return contains(tree.Right, Item);

                else if (Item.CompareTo(tree.Data) < 0)
                    return contains(tree.Left, Item);

                return false;
            }

            public Boolean Contains(T Item)
            {
                return contains(root, Item);
            }


            public int CompareTo(Object obj) //Criminal CompareTo
            {
                Criminal other = (Criminal)obj;
                return name.CompareTo(other.name);
            }


So I think I have the code I need to do it, just not sure how to go about doing it.
Because I'm just searching by Name, which is a string, and not passing an entire Criminal through as a search term, its confusing me a bit.
If you need to see any more code just let me know.
Thanks.

Is This A Good Question/Topic? 0
  • +

Replies To: Searching a Binary Search Tree for a String within an Object

#2 negligible   User is offline

  • D.I.C Regular

Reputation: 62
  • View blog
  • Posts: 302
  • Joined: 02-December 10

Re: Searching a Binary Search Tree for a String within an Object

Posted 22 March 2012 - 09:35 AM

Have you considered using a collection? Setting the name as the key and then looping through the KeyValuePairs to find the instance of criminal paired with that name.

Dictionary<String, Criminal>
Was This Post Helpful? 0
  • +
  • -

#3 ChaosEvaUnit   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 33
  • Joined: 18-November 11

Re: Searching a Binary Search Tree for a String within an Object

Posted 22 March 2012 - 09:45 AM

View Postnegligible, on 22 March 2012 - 09:35 AM, said:

Have you considered using a collection? Setting the name as the key and then looping through the KeyValuePairs to find the instance of criminal paired with that name.

Dictionary<String, Criminal>


That seems like a plausible way and albeit easier.
It's just the fact that it's for Uni work and we've been given a specification of what methods we need and what they do. And judging by what needs to be done for the task; this finding a criminal by name business, seems to be the only part that makes the creation of the BSTree Contains and Criminal CompareTo methods relevant.
Was This Post Helpful? 0
  • +
  • -

#4 negligible   User is offline

  • D.I.C Regular

Reputation: 62
  • View blog
  • Posts: 302
  • Joined: 02-December 10

Re: Searching a Binary Search Tree for a String within an Object

Posted 22 March 2012 - 10:03 AM

In my experience at University you can do pretty much what ever you want as long as you explain why, in fact you'll get graded higher for demonstrating you've done further extra research.

"I researched blah-de-blah, I found that is sucked compared to blah-de-blah"

Of course you're lecturer will be trying to teach you certain techniques, which may be foundation for something later on, so you should still make sure you learn and understand their method.

So by learning all the different ways to skin a cat you know when which method would be most appropriate. Some have benefits over others, like I suspect using a BST may be a tinsy bit less processor intensive as less values have to be checked.

Not used a BST myself though (hence no code advice on the coding of it).

This post has been edited by negligible: 22 March 2012 - 10:04 AM

Was This Post Helpful? 0
  • +
  • -

#5 Curtis Rutland   User is offline

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 5104
  • View blog
  • Posts: 9,283
  • Joined: 08-June 10

Re: Searching a Binary Search Tree for a String within an Object

Posted 22 March 2012 - 03:24 PM

Quote

"I researched blah-de-blah, I found that is sucked compared to blah-de-blah"


Sounds like a great way to get a failing grade in the classes I took. Instructors in lower level classes aren't looking for how clever you are, they just want you to grasp the course material. This sounds like it might be a data structures class, so if they told you to use a BST, then use a BST. Don't use a dictionary, because that's a different data structure (basically, it's a hash table).

They told you to use a BST to teach you how to use them and to teach you what they're good for.



I'm guessing the Contains method was written by your instructor. Think about this: Contains is basically a method that searches out and finds the entry in your BST. Therefore, you should be able to use the same logic, but instead of returning the bool, return the Criminal (which is in this case is tree.Data).

So, you'll have to change the parameter a bit. You need to search by name, so take a string as a parameter. But look at CompareTo. CompareTo only uses name to do it's comparison, so any criminal object with Name "X" will match any other criminal object with Name "X". So, you can take a string as the parameter, create a temporary Criminal in the method, then perform your search the same way Contains does, and return the object instead of true/false at the end (or null if not found).



View Postnegligible, on 22 March 2012 - 12:03 PM, said:

Some have benefits over others, like I suspect using a BST may be a tinsy bit less processor intensive as less values have to be checked.


Actually, since you're suggesting using a dictionary, it probably would be more "processor intensive" to use the BST. Read up on how Hashtables (which is what a dictionary is) work:

http://en.wikipedia....wiki/Hash_table
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1