Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8198339
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T05:55:51+00:00 2026-06-07T05:55:51+00:00

I have a List (.NET) and need to build an indented tree structure. Each

  • 0

I have a List (.NET) and need to build an indented tree structure. Each item in the list has a Position property indicating the level of indentation. The final structure should look something like this:

// (1) 1
//      (2) 1-1
//      (2) 1-2
//      (2) 1-3
// (1) 2
//      (2) 2-1
//          (3) 2-1-1
// (1) 3

The number inside the parentheses is the Position property. Each subsequent item at a given level should have a label indicated its count in the list of items at that level. Lower position levels will have sort of an outline format label, as shown in the sample. Keep in mind, I have to generate those labels correctly.

I honestly haven’t done recursive work for a while and, although I’m thinking that will be the best solution, I’m just stuck on how to get the job done. I may be over-complicating it. My thinking is that when I get to a “leaf,” I should remove that item from the list and return, but I’m not sure. Just spinning my wheels a bit.

Thanks,
Jay

UPDATE:

Here’s something close, but I’m having trouble figuring out the last case. So far, it does fine if the next item falls on the same level or is indented one level, but I need a case for when the next item in the list is in a position less than the current one (i.e., is indented farther to the left). Also, I’m not sure about my base case at the top of the method:

var sb = new StringBuilder();
BuildTree(sb, list, 0, 1, string.Empty);
return sb.ToString();

private void BuildTree(StringBuilder sb, List<Item> list, int currIndex, int count, string parentId)
{
    if (list.Count == currIndex)
    {
        return;
    }

    // Build my item.
    string currId = parentId == string.Empty ? count.ToString() : parentId + "-" + count;
    sb.Append(currId + "<br />");

    if (list[currIndex + 1].Position == list[currIndex].Position)
    {
        BuildTree(sb, list, currIndex + 1, count + 1, parentId);
    }

    if (list[currIndex + 1].Position > list[currIndex].Position)
    {
        BuildTree(sb, list, currIndex + 1, 1, currId);
    }
}

UPDATE:

An alternative implementation, but still failing for the case of a less-indented row:

private void BuildTree(StringBuilder sb, List<Item> list, int currIndex, int count, string parentId)
{
    if (list.Count == 0)
    {
        return;
    }

    // Build my item.
    string currId = parentId == string.Empty ? count.ToString() : parentId + "-" + count;
    sb.Append(currId + "<br />");

    if (list[currIndex + 1].Position == list[currIndex].Position)
    {
        BuildTree(sb, list, currIndex + 1, count + 1, parentId);
    }

    if (list[currIndex + 1].Position > list[currIndex].Position)
    {
        BuildTree(sb, list, currIndex + 1, 1, currId);
    }

    list.RemoveAt(currIndex);
}

UPDATE:

This is a hack, but it works. I’m basically building multiple sub-trees off the root. I’d prefer a proper solution rather than a hack, but this is my best attempt so far. Alternatives welcome:

var sb = new StringBuilder();
List<Person> list = GetTheList();
int cnt = 0;
while (list.Count > 0)
{
    BuildTree(sb, list, 0, ++cnt, string.Empty);
}

return sb.ToString();


private void BuildTree(StringBuilder sb, List<Person> list, int currIndex, int count, string parentId)
{
    // Build my item.
    string currId = parentId == string.Empty ? count.ToString() : parentId + "-" + count;
    sb.Append(currId + "<br />");

    if (list.Count > 1)
    {
        if (list[currIndex + 1].Position == list[currIndex].Position)
        {
            BuildTree(sb, list, currIndex + 1, count + 1, parentId);
        }

        if (list[currIndex + 1].Position > list[currIndex].Position)
        {
            BuildTree(sb, list, currIndex + 1, 1, currId);
        }
    }

    list.RemoveAt(currIndex);
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T05:55:52+00:00Added an answer on June 7, 2026 at 5:55 am
    var sb = new StringBuilder();
    List<Person> list = GetTheList();
    int cnt = 0;
    // The loop is necessary to iterate over the root elements.
    while (list.Count > 0)
    {
        BuildTree(sb, list, 0, ++cnt, string.Empty);
    }
    
    return sb.ToString();
    
    
    private void BuildTree(StringBuilder sb, List<Person> list, int currIndex, int count, string parentId)
    {
        string currId = parentId == string.Empty ? count.ToString() : parentId + "-" + count;
        sb.Append(currId + "<br />");
    
        if (list.Count > 1)
        {
            if (list[currIndex + 1].Position == list[currIndex].Position)
            {
                BuildTree(sb, list, currIndex + 1, count + 1, parentId);
            }
    
            if (list[currIndex + 1].Position > list[currIndex].Position)
            {
                BuildTree(sb, list, currIndex + 1, 1, currId);
            }
        }
    
        list.RemoveAt(currIndex);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following fiddle1 http://jsfiddle.net/y6tCt/35/ . I need to build the checkbox list
Need a list of Pcap.Net members or classes? Their website doesn't have much documentation
My project has some MyObject and the MyObject have a property of List<MyObject> .
I have a list of files, and i need to read them each in
Hi, I have a ASP.NET MVC 3 website and I need to build a
I have list of images with some simple mouseenter / mouseleave effect. http://jsfiddle.net/4vTCr/ I
I have the list box control (ASP.NET Control On aspx page, language C# ).
I have a list of object I wish to sort in C#.Net 3.5, the
I have a list comprehension operating on elements of an .NET array like obj.arr
In .NET, both array and list have Enumerable as ancestor, so a method that

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.