Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Algorithm for creating a list from a hierarchy?

Algorithm for creating a list from a hierarchy?

Scheduled Pinned Locked Moved C#
htmlcomalgorithmsquestion
5 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    Judah Gabriel Himango
    wrote on last edited by
    #1

    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

    L L L 3 Replies Last reply
    0
    • J Judah Gabriel 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

      L Offline
      L Offline
      Libor Tinka
      wrote on last edited by
      #2

      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?

      1 Reply Last reply
      0
      • J Judah Gabriel 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

        L Offline
        L Offline
        Lars Niedziolka
        wrote on last edited by
        #3

        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
        +-B3

        First 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

        J 1 Reply Last reply
        0
        • L Lars Niedziolka

          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
          +-B3

          First 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

          J Offline
          J Offline
          Judah Gabriel Himango
          wrote on last edited by
          #4

          Excellent, thanks.

          Tech, life, family, faith: Give me a visit. I'm currently blogging about: Homosexuality in Christianity Judah Himango

          1 Reply Last reply
          0
          • J Judah Gabriel 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

            L Offline
            L Offline
            leppie
            wrote on last edited by
            #5

            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

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups