Solved

How to search within a Tree

Posted on 2010-11-24
3
344 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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

747 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now