Click here to Skip to main content
15,898,035 members
Home / Discussions / C#
   

C#

 
Questionrecursive function to print data in the console in the output of a tree structure in c# Pin
darkyMats5-Oct-15 4:13
darkyMats5-Oct-15 4:13 
AnswerRe: recursive function to print data in the console in the output of a tree structure in c# Pin
CHill605-Oct-15 4:20
mveCHill605-Oct-15 4:20 
GeneralRe: recursive function to print data in the console in the output of a tree structure in c# Pin
darkyMats5-Oct-15 4:26
darkyMats5-Oct-15 4:26 
GeneralRe: recursive function to print data in the console in the output of a tree structure in c# Pin
CHill605-Oct-15 4:36
mveCHill605-Oct-15 4:36 
GeneralRe: recursive function to print data in the console in the output of a tree structure in c# Pin
darkyMats5-Oct-15 7:59
darkyMats5-Oct-15 7:59 
GeneralRe: recursive function to print data in the console in the output of a tree structure in c# Pin
CHill605-Oct-15 9:38
mveCHill605-Oct-15 9:38 
GeneralRe: recursive function to print data in the console in the output of a tree structure in c# Pin
darkyMats5-Oct-15 9:41
darkyMats5-Oct-15 9:41 
GeneralRe: recursive function to print data in the console in the output of a tree structure in c# Pin
CHill605-Oct-15 9:53
mveCHill605-Oct-15 9:53 
Ok. Caveat ... this is not necessarily the "best" solution.
First - here is my interpretation of your Area class - Note I have included the constructor suggested by @OriginalGriff
C#
public class Area
{
    public Area(int id, string description, int parentId)
    {
        Id = id;
        Description = description;
        ParentId = parentId;
    }

    public int Id { get; set; }
    public string Description { get; set; }
    public int ParentId { get; set; }
}
This means I can do the data set up as follows
C#
var areas = new List<Area>
{
    new Area(1, "Continent", -1),
    new Area(2, "Country", 1),
    new Area(3, "Province", 2),
    new Area(4, "City1", 3),
    new Area(5, "Suburb1", 6),
    new Area(6, "City2", 3),
    new Area(7, "Suburb2", 6),
    new Area(8, "Suburb3", 6),
    new Area(9, "Suburb4", 4),
    new Area(10, "House1", 7),
    new Area(11, "House3", 9),
    new Area(12, "House4", 8),
    new Area(13, "House5", 8),
    new Area(14, "House6", 7)
};
Now I need a recursive function. I chose to do this as an Extension of the List class but you could just have it as a private method within your program
C#
static class Extensions
{
    public static void PrintChildren(this List<Area> areas, Area a, int level)
    {
        Console.WriteLine("{0}{1}", new String('-', level), a.Description);

        var childrenIds = areas.Where(x => x.ParentId.Equals(a.Id)).ToList();

        if (childrenIds.Count <= 0) return;

        foreach (var c in childrenIds)
            PrintChildren(areas, c, level + 1);
    }
}

Things to note - I'm passing the "level of recursion" down the stack to make it easier to work out how many '-' characters to insert. And the function "calls itself" if the node has any children at all.
To start off the process just use
C#
areas.PrintChildren(areas[0], 0);
Console.ReadLine(); 
in your Main method.
It really is worth following this through in your IDE debugger - put a break point on
C#
Console.WriteLine("{0}{1}", new String('-', level), a.Description);
and look at your "Locals" window in the IDE. Try to follow what is actually happening. You can also right-click on the function name and "Show Call Stack on Code Map" to see what is happening.

This looks like homework, so remember your tutor may also spot this answer - make sure you do the debug bit so you understand what is going on.
GeneralRe: recursive function to print data in the console in the output of a tree structure in c# Pin
darkyMats5-Oct-15 10:36
darkyMats5-Oct-15 10:36 
GeneralRe: recursive function to print data in the console in the output of a tree structure in c# Pin
Ravi Bhavnani6-Oct-15 5:13
professionalRavi Bhavnani6-Oct-15 5:13 
GeneralRe: recursive function to print data in the console in the output of a tree structure in c# Pin
OriginalGriff5-Oct-15 4:44
mveOriginalGriff5-Oct-15 4:44 
GeneralRe: recursive function to print data in the console in the output of a tree structure in c# Pin
CHill605-Oct-15 5:58
mveCHill605-Oct-15 5:58 
AnswerRe: recursive function to print data in the console in the output of a tree structure in c# Pin
Gerry Schmitz5-Oct-15 10:52
mveGerry Schmitz5-Oct-15 10:52 
Questionclick serche button webbrouwser c# Pin
altar6664-Oct-15 10:19
altar6664-Oct-15 10:19 
AnswerRe: click serche button webbrouwser c# Pin
F-ES Sitecore4-Oct-15 22:39
professionalF-ES Sitecore4-Oct-15 22:39 
Questionusing toupper to uppercase an array index Pin
doughyi8u4-Oct-15 6:32
doughyi8u4-Oct-15 6:32 
AnswerRe: using toupper to uppercase an array index Pin
OriginalGriff4-Oct-15 6:39
mveOriginalGriff4-Oct-15 6:39 
GeneralRe: using toupper to uppercase an array index Pin
Agent__0074-Oct-15 17:58
professionalAgent__0074-Oct-15 17:58 
AnswerRe: using toupper to uppercase an array index Pin
Gerry Schmitz4-Oct-15 11:32
mveGerry Schmitz4-Oct-15 11:32 
AnswerRe: using toupper to uppercase an array index Pin
PANKAJMAURYA12-Oct-15 20:59
professionalPANKAJMAURYA12-Oct-15 20:59 
QuestionC# find a string in a 2010 Microsoft Word document Pin
Member 120318994-Oct-15 6:08
Member 120318994-Oct-15 6:08 
AnswerRe: C# find a string in a 2010 Microsoft Word document Pin
Ravi Bhavnani4-Oct-15 6:09
professionalRavi Bhavnani4-Oct-15 6:09 
GeneralRe: C# find a string in a 2010 Microsoft Word document Pin
Member 120318994-Oct-15 6:48
Member 120318994-Oct-15 6:48 
GeneralRe: C# find a string in a 2010 Microsoft Word document Pin
Pete O'Hanlon5-Oct-15 4:53
mvePete O'Hanlon5-Oct-15 4:53 
RantProgramming Pin
Member 115145063-Oct-15 14:59
Member 115145063-Oct-15 14:59 

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.