Search code examples
data-structurestreetree-traversalgraph-traversalpreorder

Is the Euler Tour algorithm essentially the same as Pre-order traversal?


I'm trying to learn about the Euler Tour algorithm and why it's popular for tree traversal. However, I'm failing to see the difference between a Euler Tour and a Pre-order traversal of a tree.

Let's say you have the tree:

     A
    / \
   B   E
  / \   \
 C   D   F

If you performed the euler tour algorithm, it would be:

A -> B -> C -> B -> D -> B -> A -> E -> F -> E -> A

But what's the purpose of this? It just seems like the exact same version of recursive pre-order:

A -> B -> C -> D -> E -> F

Obviously, in Euler Tour, you have each node value at least twice in the path, but that's only due to the recursive nature of the algorithm when you program it. If you wanted, you could do the same calculations you were doing with Euler Tour... with Pre-order, right?

If somebody could help explain Euler Tour and why it's used over other traversals, that'd be very much appreciated. Thanks.


Solution

  • With the Euler tour you can derive additional information from the result.

    You can for example see if a node is a leaf. This would be the case if the predecessor and successor of a node are the same.

    Additionally you would be able to calculate the depth of a node by adding +1 to a counter for every forward leg and subtracting 1 for every backward leg.

    Those information are often useful when dealing with trees in your algorithms.