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 827683
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T03:36:43+00:00 2026-05-15T03:36:43+00:00

I have the following structure: MyClass { guid ID guid ParentID string Name }

  • 0

I have the following structure:

MyClass {
  guid ID
  guid ParentID
  string Name
}

I’d like to create an array which contains the elements in the order they should be displayed in a hierarchy (e.g. according to their “left” values), as well as a hash which maps the guid to the indentation level.

For example:

ID     Name     ParentID
------------------------
1      Cats     2
2      Animal   NULL
3      Tiger    1
4      Book     NULL
5      Airplane NULL

This would essentially produce the following objects:

// Array is an array of all the elements sorted by the way you would see them in a fully expanded tree
Array[0] = "Airplane"
Array[1] = "Animal"
Array[2] = "Cats"
Array[3] = "Tiger"
Array[4] = "Book"

// IndentationLevel is a hash of GUIDs to IndentationLevels.
IndentationLevel["1"] = 1
IndentationLevel["2"] = 0
IndentationLevel["3"] = 2
IndentationLevel["4"] = 0
IndentationLevel["5"] = 0

For clarity, this is what the hierarchy looks like:

Airplane
Animal
  Cats
    Tiger
Book

I’d like to iterate through the items the least amount of times possible. I also don’t want to create a hierarchical data structure. I’d prefer to use arrays, hashes, stacks, or queues.

The two objectives are:

  1. Store a hash of the ID to the indentation level.
  2. Sort the list that holds all the objects according to their left values.

When I get the list of elements, they are in no particular order. Siblings should be ordered by their Name property.

Update: This may seem like I haven’t tried coming up with a solution myself and simply want others to do the work for me. However, I have tried coming up with three different solutions, and I’ve gotten stuck on each. One reason might be that I’ve tried to avoid recursion (maybe wrongly so). I’m not posting the partial solutions I have so far since they are incorrect and may badly influence the solutions of others.

  • 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-05-15T03:36:43+00:00Added an answer on May 15, 2026 at 3:36 am

    Wonsungi’s post helped a lot, however that is for a generic graph rather than a tree. So I modified it quite a bit to create an algorithm designed specifically for a tree:

    // Data strcutures:
    nodeChildren: Dictionary['nodeID'] = List<Children>;
    indentLevel: Dictionary['nodeID'] = Integer;
    roots: Array of nodes;
    sorted: Array of nodes;
    nodes: all nodes
    
    // Step #1: Prepare the data structures for building the tree
    for each node in nodes
      if node.parentID == NULL
        roots.Append(node);
        indentLevel[node] = 0;
      else
        nodeChildren[node.parentID].append(node);
    
    // Step #2: Add elements to the sorted list
    roots.SortByABC();
    while roots.IsNotEmpty()
      root = roots.Remove(0);
      rootIndentLevel = indentLevel[root];
      sorted.Append(root);
      children = nodeChildren[root];
      children.SortByABC();
      for each child in children (loop backwards)
        indentLevel[child] = rootIndentLevel + 1
        roots.Prepend(child)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In my rails view(index.html.erb), i have following structure <div> <%= render :partial => create
Say I have a class, something like the following; class MyClass { public: MyClass();
I have following structure of web application, for which I'm automating testing: <em unselectable=on
If I have a pure virtual class like the following: I have class structure
I have following structure for check-boxes <input type=checkbox name=reg_field['first_name'] value=1 /> First Name<br />
I have a DOM structure which looks something like below - <div id='MyDiv'> <span>Hello
I have the following structure public class MyClass : MyBaseClass<System.Int32> { } In a
I have following structure. #include <iostream> #include <fstream> #include <string> #include <sstream> #include <vector>
I have some class named myClass , and following structure in there: class myClass
I have following structure with example data: id season_id title 1 1 Intro 2

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.