4

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.

Bob
  • 166
  • 3
  • 15

2 Answers2

0

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.

Lima Romeo
  • 16
  • 2
-1

Note that also the postorder information is present in your Euler tour. If just list each node the last time it is listed, we get

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

Unfortunately we cannot obtain the inorder (symmetric order) from the tour. In your example this is clear from looking at node E and its child F, there is no way we can actually see whether the child is left or right.

The Euler traversal method can be extended a little to include the three recursive orders of a tree (pre- in- post-). Follow the outline of the tree and visit each node three times, before entering the left child, between left and right child, and after the right child. If a child is missing, make consecutive visits.

We can extend your example in the following way, adding numbers to your earlier tour:

A1 -> B1 -> C123 -> B2 -> D123 -> B3 -> A2 -> E12 -> F123 -> E2 -> A3