• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2004
  • Last Modified:

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
suzan


0
suzan_
Asked:
suzan_
2 Solutions
 
Abh4ITCommented:
Hi,

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
http://dofactory.com/Patterns/PatternComposite.aspx

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.

HTH
0
 
torrie01Commented:
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");
            parent.Children.Add(child);
 
            TreeElement grandchild = new TreeElement("C");
            child.Children.Add(grandchild);
 
            WriteChildren(parent);
        }
 
        private void WriteChildren(TreeElement element)
        {
            //write this element
            Console.WriteLine(element.Letter);
 
            //now write the children
            if (element.HasChildren)
            {
                foreach(TreeElement child in element.Children)
                {
                    WriteChildren(child);
                }
            }
        }

Open in new window

0
 
suzan_Author Commented:
thanks all for help
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now