Click here to Skip to main content
15,887,135 members
Home / Discussions / C#
   

C#

 
QuestionTreeView Recursion Pin
Muammar©1-Aug-09 7:50
Muammar©1-Aug-09 7:50 
AnswerRe: TreeView Recursion Pin
Mike Ellison1-Aug-09 10:47
Mike Ellison1-Aug-09 10:47 
GeneralRe: TreeView Recursion Pin
Muammar©1-Aug-09 12:50
Muammar©1-Aug-09 12:50 
GeneralRe: TreeView Recursion Pin
OriginalGriff1-Aug-09 21:59
mveOriginalGriff1-Aug-09 21:59 
GeneralRe: TreeView Recursion Pin
Muammar©1-Aug-09 23:18
Muammar©1-Aug-09 23:18 
GeneralRe: TreeView Recursion Pin
OriginalGriff2-Aug-09 0:34
mveOriginalGriff2-Aug-09 0:34 
GeneralRe: TreeView Recursion Pin
Muammar©2-Aug-09 1:01
Muammar©2-Aug-09 1:01 
AnswerRe: TreeView Recursion Pin
N a v a n e e t h9-Aug-09 4:47
N a v a n e e t h9-Aug-09 4:47 
Sounds like a good question. As a first step, let us create a class named Node which will have Id and Name as properties, AddChild and FindNode as methods. Consider the following code,
public sealed class Node
{
    readonly List<Node> childNodes = new List<Node>();
    Node parent = null;
    public Node(int id, string name)
    {
        this.Id = id;
        this.Name = name;
    }

    public void AddChild(Node node)
    {
        node.SetParent(this);
        childNodes.Add(node);
    }

    public void SetParent(Node parent)
    {
        this.parent = parent;
    }

    public Node FindNode(int id)
    {
        Node result = childNodes.SingleOrDefault(node => node.Id == id);
        if (result == null)
        {
            foreach (Node node in childNodes)
                result = node.FindNode(id);
        }
        return result;
    }

    public void PrintTree(Action<Node> callback)
    {
        callback(this);
        foreach (Node node in childNodes)
            node.PrintTree(callback);
    }

    public override string ToString()
    {
        return string.Format("Id : {0}, Parent Name : {1}, Name : {2}", 
            Id, parent == null ? "No parent" : parent.Name, Name);
    }

    public int Id { get; private set; }

    public string Name { get; private set; }
}
You have done with a recursive tree implementation!

If you are confused, here is the explanation.

1 - Each node holds its children nodes
2 - When a child node is added to a node, it will set the child nodes parent and add it to the children collection.
3 - FindNode() does a recursive search in all its children and tries to find a node with the specified id. If it can't find a node, returns NULL.
4 - PrintTree does a recursive search and ask each node and its children to print itself. The callback supplied will be called and you can handle this callback and do something useful. In this example, I have just printed the name.

Here is how you use it in a console application,
Node baseLine = new Node(0, "BaseLine");
baseLine.AddChild(new Node(1, "A"));

// find A node to add A1 and A2 as its children
Node aNode = baseLine.FindNode(1);
aNode.AddChild(new Node(2, "A1"));
aNode.AddChild(new Node(3, "A2"));

baseLine.AddChild(new Node(4, "B"));

// find B node to add B1 and B2 as its children
Node bNode = baseLine.FindNode(4);
bNode.AddChild(new Node(5, "B1"));
bNode.AddChild(new Node(6, "B2"));

// printing the whole tree
baseLine.PrintTree(node => Console.WriteLine(node));
Console.ReadKey();
I wrote a blog post that explains the thought process in writing recursive methods. Read it here[^].

Hope that helps Smile | :)


GeneralRe: TreeView Recursion Pin
Muammar©9-Aug-09 9:53
Muammar©9-Aug-09 9:53 
GeneralRe: TreeView Recursion Pin
N a v a n e e t h9-Aug-09 16:31
N a v a n e e t h9-Aug-09 16:31 
GeneralRe: TreeView Recursion Pin
Muammar©10-Aug-09 2:19
Muammar©10-Aug-09 2:19 
GeneralRe: TreeView Recursion Pin
N a v a n e e t h10-Aug-09 7:40
N a v a n e e t h10-Aug-09 7:40 
GeneralRe: TreeView Recursion Pin
Muammar©10-Aug-09 7:49
Muammar©10-Aug-09 7:49 
GeneralRe: TreeView Recursion Pin
Muammar©10-Aug-09 8:04
Muammar©10-Aug-09 8:04 
GeneralRe: TreeView Recursion Pin
Muammar©10-Aug-09 8:14
Muammar©10-Aug-09 8:14 
GeneralSolved:) Pin
Muammar©10-Aug-09 8:21
Muammar©10-Aug-09 8:21 
GeneralA minor bug?? I hope :s [modified] Pin
Muammar©10-Aug-09 8:35
Muammar©10-Aug-09 8:35 
GeneralRe: A minor bug?? I hope :s Pin
N a v a n e e t h10-Aug-09 9:29
N a v a n e e t h10-Aug-09 9:29 
GeneralRe: A minor bug?? I hope :s Pin
Muammar©10-Aug-09 10:21
Muammar©10-Aug-09 10:21 
GeneralRe: TreeView Recursion [modified] Pin
Muammar©10-Aug-09 7:45
Muammar©10-Aug-09 7:45 
General[Message Deleted] Pin
Muammar©11-Aug-09 3:13
Muammar©11-Aug-09 3:13 
GeneralRe: TreeView Recursion Pin
N a v a n e e t h11-Aug-09 4:23
N a v a n e e t h11-Aug-09 4:23 
GeneralRe: TreeView Recursion Pin
Muammar©12-Aug-09 3:36
Muammar©12-Aug-09 3:36 
GeneralRe: TreeView Recursion Pin
N a v a n e e t h12-Aug-09 7:17
N a v a n e e t h12-Aug-09 7:17 
GeneralRe: TreeView Recursion Pin
Muammar©12-Aug-09 7:37
Muammar©12-Aug-09 7:37 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.