1 / 10

Podcast Ch17c

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.

loriw
Download Presentation

Podcast Ch17c

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 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

  2. 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.

  3. 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.

  4. 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 / +

  5. 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

  6. 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.

  7. 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; }

  8. 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();

  9. 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))

  10. Student Question • Can you think of another application that would benefit from an Euler traversal?

More Related