Click here to Skip to main content
15,895,084 members
Home / Discussions / C#
   

C#

 
GeneralRe: URL's Pin
Gregory Bryant1-Aug-09 16:28
Gregory Bryant1-Aug-09 16:28 
GeneralRe: URL's Pin
Richard Andrew x641-Aug-09 16:32
professionalRichard Andrew x641-Aug-09 16:32 
GeneralRe: URL's Pin
Gregory Bryant1-Aug-09 16:38
Gregory Bryant1-Aug-09 16:38 
QuestionCrystalReport ReportSource not from a file path but a byte array. Possible? Pin
Wendell.S1-Aug-09 11:10
Wendell.S1-Aug-09 11:10 
QuestionPrevent same user to login [modified] Pin
gal0001-Aug-09 10:32
gal0001-Aug-09 10:32 
AnswerRe: Prevent same user to login Pin
Abhijit Jana1-Aug-09 11:00
professionalAbhijit Jana1-Aug-09 11:00 
GeneralRe: Prevent same user to login Pin
Luc Pattyn1-Aug-09 11:25
sitebuilderLuc Pattyn1-Aug-09 11:25 
GeneralRe: Prevent same user to login Pin
Eddy Vluggen1-Aug-09 14:46
professionalEddy Vluggen1-Aug-09 14:46 
GeneralRe: Prevent same user to login Pin
Luc Pattyn1-Aug-09 14:55
sitebuilderLuc Pattyn1-Aug-09 14:55 
GeneralRe: Prevent same user to login Pin
Xmen Real 1-Aug-09 18:05
professional Xmen Real 1-Aug-09 18:05 
QuestionSingleton Pattern - When Use Pin
dataminers1-Aug-09 8:15
dataminers1-Aug-09 8:15 
AnswerRe: Singleton Pattern - When Use Pin
Not Active1-Aug-09 9:12
mentorNot Active1-Aug-09 9:12 
GeneralRe: Singleton Pattern - When Use Pin
dataminers1-Aug-09 10:14
dataminers1-Aug-09 10:14 
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 

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.