0

Picture a File Explorer-style window with the folders fully expanded. Starting at an arbitrary node, I press the down arrow to move down through the tree. Maybe I'm reading it wrong but I couldn't find a name for this kind of traversal (note: this article only applies to binary trees; I can't find a good reference on N-ary traversals and their names).

In my code I've struggled to find a good name for this. I've used GetForwardEnumerator(), GetNextNode(), etc. The closest I can come to expressing this is that it's the "next node in the tree", but that only makes sense if you look at the tree like a file explorer window as I've described.

What's the standard name for this kind of traversal/enumeration, and how might I name methods and properties that perform it? For now I'm settling for .NextNode. It's uncomfortably close to .NextSibling, but it's the best way I've found to communicate the intent.

Sample code:

/// <summary>
/// Gets the next node.
/// </summary>
/// <value>
/// The next tree node if it exists; otherwise, <c>null</c>.
/// </value>
public SimpleTreeNode<T> NextNode
{
    get
    {
        if (this.Children.Count > 0)
        {
            return this.Children[0];
        }
        else if (this.NextSibling != null)
        {
            return this.NextSibling;
        }
        else
        {
            SimpleTreeNode<T> parentNode = this.Parent;
            while (parentNode != null)
            {
                SimpleTreeNode<T> nextSiblingNode = parentNode.NextSibling;
                if (nextSiblingNode != null)
                {
                    return nextSiblingNode;
                }

                parentNode = parentNode.Parent;
            }
        }

        return null;
    }
}
System.Cats.Lol
  • 1,620
  • 1
  • 26
  • 47
  • I think what you're describing is a straightforward in-fix traversal, as explain in the link you cited. – BJ Myers Oct 29 '14 at 20:28
  • @nintendojunkie do you mean an in-order traversal? That would imply visiting the left subtree first, plus this is an N-ary tree. That article wasn't the greatest fit I'll admit--it only covers binary trees. Still looking for a good intent-revealing name as applied to this specific kind of N-ary traversal. – System.Cats.Lol Oct 31 '14 at 18:07
  • Wow... must have been tired that day. The answer below is correct - this is a pre-order traversal. As for binary vs. N-ary trees, the nomenclature is the same. – BJ Myers Nov 03 '14 at 16:59

1 Answers1

1

I believe you're talking about Pre-order traversal (so named because the parent is visited before the children). If you look at the image in the Wikipedia article you reference:

Pre-order Traversal

The folder-structure equivalent would be:

F
|-B
| |-A
| |-D
|   |-C
|   |-E
|-G 
| |-I
|   |-H

In a typical fully-open folder structure, the order of traversal is:

  1. Visit the parent folder
  2. visit the sub-folders in order (recursively)

Which is the definition of pre-order traversal.

Community
  • 1
  • 1
D Stanley
  • 149,601
  • 11
  • 178
  • 240
  • OK--I guess `.NextPreOrderNode` or `.NextNodePreOrder` would be fitting property names. Good call, I just didn't get the right tree in mind when I turned my head sideways :) – System.Cats.Lol Oct 31 '14 at 18:12
  • PS - For future reference, if you take the time to answer a question, giving the asker an upvote is nice. – System.Cats.Lol Oct 31 '14 at 21:53