build a tree using c#

Posted on 2008-11-04
Last Modified: 2013-12-17
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

Question by:suzan_
    LVL 26

    Assisted Solution

    by:Anurag Thakur
    LVL 13

    Expert Comment

    LVL 1

    Expert Comment


    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.

    LVL 3

    Accepted Solution

    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


    Author Comment

    thanks all for help

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
    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!
    how to add IIS SMTP to handle application/Scanner relays into office 365.
    Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

    759 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

    13 Experts available now in Live!

    Get 1:1 Help Now