build a tree using c#

I am hoping that someone can help me on how to build a tree using c#
My basic need is to be able to
1. Define a tree
2. Define a parent node
3. Define children of that parent
4. Create children of a child node.

Consider all the nodes to just be letters
I am trying to build a tree to search for prefix, for example if the prefix is "un" then the parent is "u" and the child is "n".

Can anyone help me with this?

Consider also the following example (maybe it will help?)

Parent / Child /Child
A / B / C
A / C
A / D
B / E
B / F
C / F
C / G

Thanks in advance for any help

Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.


I would say the best way to implement a tree concept in .NET is to follow the composite design pattern. This pattern is the crux behing the tree concept and could be used for traversing the tree as well. The best reading for this is at

So my feeling is in addition to Display you can have method like Search childern and returns a list of Nodes. This propogration works out well. The treeview in .NET I guess as well as Explorer in Windows follows the same concept.

I'm not sure I understand exactly what you're looking for, but here's the basic code for a tree:
    public class TreeElement
        private List<TreeElement> _children = new List<TreeElement>();
        private string _letter;
        public List<TreeElement> Children
            get { return _children; }
        public string Letter
            get { return _letter; }
        public bool HasChildren
            get { return (_children.Count > 0); }
        public TreeElement(string letter)
            _letter = letter;
//In another file:
public void TreeTest()
            TreeElement parent = new TreeElement("A");
            TreeElement child = new TreeElement("B");
            TreeElement grandchild = new TreeElement("C");
        private void WriteChildren(TreeElement element)
            //write this element
            //now write the children
            if (element.HasChildren)
                foreach(TreeElement child in element.Children)

Open in new window


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
suzan_Author Commented:
thanks all for help
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.