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. Other Discussions
  3. The Weird and The Wonderful
  4. Recursion schmecursion!

Recursion schmecursion!

Scheduled Pinned Locked Moved The Weird and The Wonderful
data-structures
8 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.
  • P Online
    P Online
    PIEBALDconsult
    wrote on last edited by
    #1

    I found a novel (or at least weird and wonderful) way to avoid recursion when traversing the nodes in a TreeView. :jig: Here's a teaser:

    System.Windows.Forms.TreeNode nod = this.tvMain.SelectedNode as System.Windows.Forms.TreeNode ;

    System.Collections.Generic.Stack<System.Collections.IEnumerator> sub =
    new System.Collections.Generic.Stack<System.Collections.IEnumerator>() ;

    sub.Push ( nod.Nodes.GetEnumerator() ) ;

    Stay tuned for an article. :badger:

    S B B 3 Replies Last reply
    0
    • P PIEBALDconsult

      I found a novel (or at least weird and wonderful) way to avoid recursion when traversing the nodes in a TreeView. :jig: Here's a teaser:

      System.Windows.Forms.TreeNode nod = this.tvMain.SelectedNode as System.Windows.Forms.TreeNode ;

      System.Collections.Generic.Stack<System.Collections.IEnumerator> sub =
      new System.Collections.Generic.Stack<System.Collections.IEnumerator>() ;

      sub.Push ( nod.Nodes.GetEnumerator() ) ;

      Stay tuned for an article. :badger:

      S Offline
      S Offline
      Super Lloyd
      wrote on last edited by
      #2

      I see where this is coming! One can say it's inspired by the way way assembly works! :P But the important question: does it blend? Err... I mean.. is it fast?

      A new .NET Serializer All in one Menu-Ribbon Bar Taking over the world since 1371!

      1 Reply Last reply
      0
      • P PIEBALDconsult

        I found a novel (or at least weird and wonderful) way to avoid recursion when traversing the nodes in a TreeView. :jig: Here's a teaser:

        System.Windows.Forms.TreeNode nod = this.tvMain.SelectedNode as System.Windows.Forms.TreeNode ;

        System.Collections.Generic.Stack<System.Collections.IEnumerator> sub =
        new System.Collections.Generic.Stack<System.Collections.IEnumerator>() ;

        sub.Push ( nod.Nodes.GetEnumerator() ) ;

        Stay tuned for an article. :badger:

        B Offline
        B Offline
        Brisingr Aerowing
        wrote on last edited by
        #3

        Is it anything like this extension method[^]?

        What do you get when you cross a joke with a rhetorical question? The metaphorical solid rear-end expulsions have impacted the metaphorical motorized bladed rotating air movement mechanism. Do questions with multiple question marks annoy you???

        P 1 Reply Last reply
        0
        • B Brisingr Aerowing

          Is it anything like this extension method[^]?

          What do you get when you cross a joke with a rhetorical question? The metaphorical solid rear-end expulsions have impacted the metaphorical motorized bladed rotating air movement mechanism. Do questions with multiple question marks annoy you???

          P Online
          P Online
          PIEBALDconsult
          wrote on last edited by
          #4

          Only in how it works; not in what it does. I don't think that will work with TreeNodes because TreeNodes are not themselves IEnumerable and the Nodes property is a non-generic IEnumerable. I can't even add that finally because IEnumerator (the non-generic one) is not IDisposable. The Nodes Property also has the benefit of never being null, and the TreeNodeCollection has a Count Property. What I'm doing also needs to know the stack depth for each Node. There are other things that are unique to what I'm doing that a general method like that is unlikely to support. And mine uses only one while loop and one Peek. Why are they so wasteful? :-D And, yes, I made an Extension Method for TreeNodes -- it's an alternative to ToString(). Thanks for showing me that. :thumbsup: Interesting to see that I'm not the only one to think of this technique. Hey, that one doesn't yield the root object? I think it needs work.

          B 1 Reply Last reply
          0
          • P PIEBALDconsult

            Only in how it works; not in what it does. I don't think that will work with TreeNodes because TreeNodes are not themselves IEnumerable and the Nodes property is a non-generic IEnumerable. I can't even add that finally because IEnumerator (the non-generic one) is not IDisposable. The Nodes Property also has the benefit of never being null, and the TreeNodeCollection has a Count Property. What I'm doing also needs to know the stack depth for each Node. There are other things that are unique to what I'm doing that a general method like that is unlikely to support. And mine uses only one while loop and one Peek. Why are they so wasteful? :-D And, yes, I made an Extension Method for TreeNodes -- it's an alternative to ToString(). Thanks for showing me that. :thumbsup: Interesting to see that I'm not the only one to think of this technique. Hey, that one doesn't yield the root object? I think it needs work.

            B Offline
            B Offline
            Brisingr Aerowing
            wrote on last edited by
            #5

            A quick manual trace and it adds the root IEnumerator and then enumerates it, yielding the objects. Then is goes on to get more enumerators. It is used here[^] to flatten a directory tree to a list of files and folders.

            What do you get when you cross a joke with a rhetorical question? The metaphorical solid rear-end expulsions have impacted the metaphorical motorized bladed rotating air movement mechanism. Do questions with multiple question marks annoy you???

            P 1 Reply Last reply
            0
            • B Brisingr Aerowing

              A quick manual trace and it adds the root IEnumerator and then enumerates it, yielding the objects. Then is goes on to get more enumerators. It is used here[^] to flatten a directory tree to a list of files and folders.

              What do you get when you cross a joke with a rhetorical question? The metaphorical solid rear-end expulsions have impacted the metaphorical motorized bladed rotating air movement mechanism. Do questions with multiple question marks annoy you???

              P Online
              P Online
              PIEBALDconsult
              wrote on last edited by
              #6

              Yes, the enumerator of the root, but not the root itself.

              1 Reply Last reply
              0
              • P PIEBALDconsult

                I found a novel (or at least weird and wonderful) way to avoid recursion when traversing the nodes in a TreeView. :jig: Here's a teaser:

                System.Windows.Forms.TreeNode nod = this.tvMain.SelectedNode as System.Windows.Forms.TreeNode ;

                System.Collections.Generic.Stack<System.Collections.IEnumerator> sub =
                new System.Collections.Generic.Stack<System.Collections.IEnumerator>() ;

                sub.Push ( nod.Nodes.GetEnumerator() ) ;

                Stay tuned for an article. :badger:

                B Offline
                B Offline
                Bernhard Hiller
                wrote on last edited by
                #7

                PIEBALDconsult wrote:

                System.Windows.Forms.TreeNode nod = this.tvMain.SelectedNode as System.Windows.Forms.TreeNode ;

                Why do you complain? That cast is really safe, isn't it? :-D And I am nodding my head for his great capabilities in naming of variables.

                P 1 Reply Last reply
                0
                • B Bernhard Hiller

                  PIEBALDconsult wrote:

                  System.Windows.Forms.TreeNode nod = this.tvMain.SelectedNode as System.Windows.Forms.TreeNode ;

                  Why do you complain? That cast is really safe, isn't it? :-D And I am nodding my head for his great capabilities in naming of variables.

                  P Online
                  P Online
                  PIEBALDconsult
                  wrote on last edited by
                  #8

                  Yes, that cast is very safe. However, originally I was casting to a custom derived TreeNode type, I could probably remove it now.

                  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