Solved

How to search within a Tree

Posted on 2010-11-24
3
371 Views
Last Modified: 2012-05-10
Is it possible to search for a treenode within in tree by the treenode text?  I have a windows form application C# application that allows users to create new tree nodes and assign ID as text.  I need to make sure the ID value they assign is unique within the parent.  

For example,

Tree
   - Category 1   (ID = 001)
      - SubCat 1   (ID = 031)
      - SubCat 2   (ID = 091)
      - SubCat 3   (ID =   ---->  If the user trys to enter 091, 031 or 001 for the ID, then I need to                                           display an error.  How can I search within a tree?
  -  Category 2   (ID = 004)

           
0
Comment
Question by:Hojoformo
3 Comments
 
LVL 10

Expert Comment

by:Tyler Laczko
ID: 34208583
use embedded for loops to access the trees within the trees.

compare their values.
0
 
LVL 85

Assisted Solution

by:Mike Tomlinson
Mike Tomlinson earned 100 total points
ID: 34208601
When you add the TreeNodes, use the ID value as the KEY:
http://msdn.microsoft.com/en-us/library/57aa8e09.aspx

Then you can use the Nodes.Find() method to determine if a node with that key already exists:
http://msdn.microsoft.com/en-us/library/system.windows.forms.treenodecollection.find.aspx
0
 
LVL 29

Accepted Solution

by:
anarki_jimbel earned 400 total points
ID: 34208987
Honestly, I would do another way. I would use a dictionary to store references to all nodes, and use id as aq key:

The idea is outlined in code below.

The main reason for that is performance. Also, it's quite handy - using an id you may find any node very quickly. Search in dictionaries MUCH faster than in trees. For big trees this might be important.My approach does not consume much additional memory.

Drawback - you need to maintain bot collections - your tree and the dictionary.
TreeView tv = new TreeView();
            Dictionary<int, TreeNode> nodeHash = new Dictionary<int, TreeNode>();

            TreeNode node = new TreeNode();
            int someNewID = 5;
            
            if(!nodeHash.ContainsKey(someNewID))
            {
                node.Text = someNewID.ToString();
                tv.Nodes.Add(node);
                nodeHash.Add(someNewID, node);
            }
            {
                MessageBox.Show("This ID already exists");
            }

Open in new window

0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question