Solved

How to search within a Tree

Posted on 2010-11-24
3
379 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 86

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 30

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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
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…

751 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