Table of Contents

Tree - Order

About

The term tree order describes how a tree is traversed.

It can also be a graph data structures, selecting an arbitrary node as the root.

Type

h
        /  |  \
      /    |   \
    d      e    g
   /|\          |
  / | \         f
 a  b  c

The traversals are classified by the order in which the nodes are visited:

Not for a tree (directed algorithm)

Example

Java

/* Node is an object of your implementation with a getChildren function */
TreeTraverser<Node> traverser = new TreeTraverser<Node>() {
    @Override
    public Iterable<Task> children(Object node) {
        return node.getChildren();
    }
};
Node root = ....

Then you can iterate over the tree with a for loop:

for (Task task : traverser.breadthFirstTraversal(root)) { ... }
for (Task task : traverser.preOrderTraversal(root)) { ... }
for (Task task : traverser.postOrderTraversal(root)) { ... }

Documentation / Reference