Algorithm for creating a list from a hierarchy?
-
I have a hierarchy of objects. For simplicity's sake, let's say I have a TreeView containing a hierarchy of TreeNodes. I'd like to add these TreeNodes to an ArrayList, with the top-most level TreeNodes being added at the beginning of the list, 2nd-level TreeNodes added next, 3rd level nodes after that, and so on. Anyone know of an efficient algorithm to do this? Or should I just resort to manually going down each hierarchy level, adding each node to the list as I go along?
Tech, life, family, faith: Give me a visit. I'm currently blogging about: Homosexuality in Christianity Judah Himango
-
I have a hierarchy of objects. For simplicity's sake, let's say I have a TreeView containing a hierarchy of TreeNodes. I'd like to add these TreeNodes to an ArrayList, with the top-most level TreeNodes being added at the beginning of the list, 2nd-level TreeNodes added next, 3rd level nodes after that, and so on. Anyone know of an efficient algorithm to do this? Or should I just resort to manually going down each hierarchy level, adding each node to the list as I go along?
Tech, life, family, faith: Give me a visit. I'm currently blogging about: Homosexuality in Christianity Judah Himango
This is not a depth-first tree traversal, so you probably need to create array of arrays:
ArrayList[] lists = new ArrayList[DEPTHS_COUNT];
then go through all nodes using recursion:void Traverse(TreeNode tn, ref ArrayList[] lists, int depth) { lists[depth].Add(tn); if (tn.Nodes.Count > 0) foreach (TreeNode node in tn.Nodes) Traverse(node, ref lists, depth + 1); }
...and the last step is to split lists into one list:ArrayList nodeList = new ArrayList(); foreach (ArrayList list in lists) foreach (TreeNode tn in list) nodeList.Add(tn);
Is that what you need? -
I have a hierarchy of objects. For simplicity's sake, let's say I have a TreeView containing a hierarchy of TreeNodes. I'd like to add these TreeNodes to an ArrayList, with the top-most level TreeNodes being added at the beginning of the list, 2nd-level TreeNodes added next, 3rd level nodes after that, and so on. Anyone know of an efficient algorithm to do this? Or should I just resort to manually going down each hierarchy level, adding each node to the list as I go along?
Tech, life, family, faith: Give me a visit. I'm currently blogging about: Homosexuality in Christianity Judah Himango
Hi Judah, It's not difficult. Use following call
ArrayList = ListOfTree(myTree);
and implement ListOfTree with this:
static ArrayList ListOfTree(TreeView tree)
{
ArrayList list = new ArrayList();
// Add 1st level:
list.AddRange(tree.Nodes);// Add 2nd, 3rd, 4th,... level
for (int idx = 0; idx < list.count; idx++)
{
list.AddRange(((TreeNode)list[idx]).Nodes);
}// return the result list
return list;
}Example: You have this tree:
Tree
|-A1
| |-B1
| | |-C1
| | +-C2
| +-B2
| +-C3
+-A2
+-B3First you add the Nodes of the tree:
List := A1 A2 [from Tree]
Second you add each child of the already listed nodes: A1 A2
List := A1 A2 B1 B2 [from A1]
List := A1 A2 B1 B2 B3 [from A2]And so on for each child of B1 .. B3
List := A1 A2 B1 B2 B3 C1 C2 [from B1]
List := A1 A2 B1 B2 B3 C1 C2 C3 [from B2]
List := A1 A2 B1 B2 B3 C1 C2 C3 [from B3]And so on for each child of C1 .. C3
List := A1 A2 B1 B2 B3 C1 C2 C3 [from C1]
List := A1 A2 B1 B2 B3 C1 C2 C3 [from C2]
List := A1 A2 B1 B2 B3 C1 C2 C3 [from C3]Have fun, Niedzi
-
Hi Judah, It's not difficult. Use following call
ArrayList = ListOfTree(myTree);
and implement ListOfTree with this:
static ArrayList ListOfTree(TreeView tree)
{
ArrayList list = new ArrayList();
// Add 1st level:
list.AddRange(tree.Nodes);// Add 2nd, 3rd, 4th,... level
for (int idx = 0; idx < list.count; idx++)
{
list.AddRange(((TreeNode)list[idx]).Nodes);
}// return the result list
return list;
}Example: You have this tree:
Tree
|-A1
| |-B1
| | |-C1
| | +-C2
| +-B2
| +-C3
+-A2
+-B3First you add the Nodes of the tree:
List := A1 A2 [from Tree]
Second you add each child of the already listed nodes: A1 A2
List := A1 A2 B1 B2 [from A1]
List := A1 A2 B1 B2 B3 [from A2]And so on for each child of B1 .. B3
List := A1 A2 B1 B2 B3 C1 C2 [from B1]
List := A1 A2 B1 B2 B3 C1 C2 C3 [from B2]
List := A1 A2 B1 B2 B3 C1 C2 C3 [from B3]And so on for each child of C1 .. C3
List := A1 A2 B1 B2 B3 C1 C2 C3 [from C1]
List := A1 A2 B1 B2 B3 C1 C2 C3 [from C2]
List := A1 A2 B1 B2 B3 C1 C2 C3 [from C3]Have fun, Niedzi
Excellent, thanks.
Tech, life, family, faith: Give me a visit. I'm currently blogging about: Homosexuality in Christianity Judah Himango
-
I have a hierarchy of objects. For simplicity's sake, let's say I have a TreeView containing a hierarchy of TreeNodes. I'd like to add these TreeNodes to an ArrayList, with the top-most level TreeNodes being added at the beginning of the list, 2nd-level TreeNodes added next, 3rd level nodes after that, and so on. Anyone know of an efficient algorithm to do this? Or should I just resort to manually going down each hierarchy level, adding each node to the list as I go along?
Tech, life, family, faith: Give me a visit. I'm currently blogging about: Homosexuality in Christianity Judah Himango
Judah Himango wrote: efficient algorithm Anyway u look at a tree traversal, it will almost allways be an O(n) operation. xacc-ide 0.0.15 now with C#, MSIL, C, XML, ASP.NET, Nemerle, MyXaml and HLSL coloring - Screenshots