Jump to content

How to build a tree from list.

- - - - -

  • Please log in to reply
3 replies to this topic

#1
brunoleal

brunoleal

    Newbie

  • Members
  • Pip
  • 2 posts
Hi, I wonder if anyone can help me with this problem:

I've created a list with 3 references: parent, child and level reference.

The level indicates where the parent is located, for example: if the level is 0, it means that it's followed parent is located at the first column of the tree. If the level is 1, it's a child from some parent in the level 0 and it must appear at the second column followed by it's parent.

it's a little complicated to explain... the information is organized like an ordinary tree from Windows XP Explorer TreeView. Each parent might or might not have a child and the child might or not have a another child ....

The problem is: I have to organize the information from this list in another list that contains a folder name and the level of this folder. Then I can supply a method that build the treeview by the order of the information in the list.

If I wasn't clear enough I can post a print screen later.

#2
zoranh

zoranh

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 207 posts
You can create the tree by sorting the original list ascending by depth (i.e. level). Then traverse the list and add each item into the tree, which you can positively do because node's parent must have smaller level than the node, and hence parent node can be found in part of the tree which has already been built.

#3
brunoleal

brunoleal

    Newbie

  • Members
  • Pip
  • 2 posts
Zoranh, thank you for your response.

I'm newbie, so if a ask stupid questions, forgive me, ehehhe

I looked at the sort method and I couldn't find a way to sort it in the right way.

I posted a print of the list. How can I do it in C#?



All the best.

Attached Files



#4
Momerath

Momerath

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 242 posts
You'll need to implement your own compare class and use the correct sort method. You can find an example at this link




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users