Preface :
Up to this point, all of our three traversal algorithms visit each node exactly once. For instance, the inorder traversal visit the node between visiting the left subtree and the right subtree. We need a more general tree traversal algorithm for some applications, one that will visit each node more than once. The Euler tour traversal provides a solution. We assume that the edges and nodes of a tree T are contained in a walkway with walls on both sides. The Euler tour is a walk around T, touching each node as we encounter it, always keeping the wall on our right. The tour visits each node three times :
If the node is a leaf, all of the visits are combined into a single visit. The walk in below figure traverses an expression tree. The directed edges trace the Euler tour beginning with the root. We encounter nodes in the following order :
Tour visits : + * a * - d - e - * + / b / c / +
The following pseudo-code description of the algorithm summarize the Euler tour. A single visit to a leaf node and multiple visits to a nonleaf node are simply denoted by "visit" althrough, in practice, an implementation does not perform the same action.
The recursive algorithm allows us to pause three times to perform a visit. You should note that the Euler tour generalizes the inorder, postorder and preorder three traversals. For instance, if the visit on the left and the vist on the right do nothing, the Euler tour is equivalent to an inorder traversal.
An expression tree provides a good application of a Euler tour. The traversal includes visits that add parentheses and that access the value of a node. The result is a string that represent an equivalent fully parenthesized expression. The algorithm is straightforward. A visit to a leaf node (operand) inserts the operand in the string. For a nonleaf node (operator), insert a '(' as the visit on the left, insert the operator as the visit from below and insert a ')' as the visit on the right. The static method fullParen() in the BinaryTree class implements the algorithm :
Supplement :
* Wiki : Eulerian Path
Up to this point, all of our three traversal algorithms visit each node exactly once. For instance, the inorder traversal visit the node between visiting the left subtree and the right subtree. We need a more general tree traversal algorithm for some applications, one that will visit each node more than once. The Euler tour traversal provides a solution. We assume that the edges and nodes of a tree T are contained in a walkway with walls on both sides. The Euler tour is a walk around T, touching each node as we encounter it, always keeping the wall on our right. The tour visits each node three times :
If the node is a leaf, all of the visits are combined into a single visit. The walk in below figure traverses an expression tree. The directed edges trace the Euler tour beginning with the root. We encounter nodes in the following order :
Tour visits : + * a * - d - e - * + / b / c / +
The following pseudo-code description of the algorithm summarize the Euler tour. A single visit to a leaf node and multiple visits to a nonleaf node are simply denoted by "visit" althrough, in practice, an implementation does not perform the same action.
The recursive algorithm allows us to pause three times to perform a visit. You should note that the Euler tour generalizes the inorder, postorder and preorder three traversals. For instance, if the visit on the left and the vist on the right do nothing, the Euler tour is equivalent to an inorder traversal.
An expression tree provides a good application of a Euler tour. The traversal includes visits that add parentheses and that access the value of a node. The result is a string that represent an equivalent fully parenthesized expression. The algorithm is straightforward. A visit to a leaf node (operand) inserts the operand in the string. For a nonleaf node (operator), insert a '(' as the visit on the left, insert the operator as the visit from below and insert a ')' as the visit on the right. The static method fullParen() in the BinaryTree class implements the algorithm :
Supplement :
* Wiki : Eulerian Path
This message was edited 4 times. Last update was at 19/10/2010 10:05:15
沒有留言:
張貼留言