100 likes | 123 Views
Podcast Ch17c. Title : Euler Tree Traversal Description : Euler traversal; program 17.3 Participants : Barry Kurtz (instructor); John Helfert and Tobie Williams (students) Textbook : Data Structures for Java; William H. Ford and William R. Topp. Euler Tree Traversal.
E N D
Podcast Ch17c • Title: Euler Tree Traversal • Description: Euler traversal; program 17.3 • Participants: Barry Kurtz (instructor); John Helfert and Tobie Williams (students) • Textbook: Data Structures for Java; William H. Ford and William R. Topp
Euler Tree Traversal • Up to this point, all of our tree traversal algorithms visit each node exactly once. For instance, the inorder traversal visits 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.
Euler Tree Traversal(continued) • The Euler tour is a walk around T, encountering each node three times: • On the left, before the Euler tour of the node's left subtree. • From below, as we finish the tour of the left subtree. • On the right, after we finish the Euler tour of the right subtree. • If the node is a leaf, we consider the visits to all occur at once.
Euler Tree Traversal(continued) • The walk in the figure traverses an expression tree. The directed edges trace the Euler tour beginning with the root. Tour visits: + * a * - d - e - * + / b / c / +
Euler Tree Traversal(continued) Algorithm eulerTour(TNode t): if t ≠ null if t is a leaf node visit t else visit t // on the left eulerTour(t.left); visit t; // from below eulerTour(t.right); visit t; // on the right
Euler Tree Traversal(continued) • Use an Euler tour to generate a fully parenthesized expression from an expression tree. • A visit to an operand inserts the operand in the string. • For an operator, output a "(" for the visit on the left, output the operator for the visit from below, and output a ")" for the visit on the right.
Euler Tree Traversal (concluded) // traverse an expression tree and display the // equivalent fully parenthesized expression public static <T> String fullParen(TNode<Character> t) { String s = ""; if (t != null) { if (t.left == null && t.right == null) s += t.nodeValue; // visit a leaf node else { s += "("; // visit on left s += fullParen(t.left); s += t.nodeValue; // visit from below s += fullParen(t.right); s += ")"; // visit on right } } return s; }
Program 17.3 import java.util.Scanner; import ds.util.TNode; import ds.util.BinaryTree; public class Program17_3 { public static void main(String[] args) { // prompt for the RPN expression Scanner keyIn = new Scanner(System.in); String postfixExp; // root of the expression tree TNode<Character> root; System.out.print ("Enter a postfix expresssion: "); postfixExp = keyIn.nextLine();
Program 17.3 (concluded) // build the expression tree root = BinaryTree.buildExpTree(postfixExp); // display the tree System.out.println("Expression tree"); System.out.println(BinaryTree.displayTree(root,1)); // output the full parenthesized expression System.out.print("Fully parenthesized expression:"); System.out.println(BinaryTree.fullParen(root)); } } Enter a postfix expresssion: a d e - * b c / + Expression tree + * / a - b c d e Fully parenthesized expression: ((a*(d-e))+(b/c))
Student Question • Can you think of another application that would benefit from an Euler traversal?