cs230 Lecture Notes Week 10 Monday Exam Tomorrow! Homework 7 solutions -------------------- 4 designs and their pros and cons Array/Vector implementation of a tree ------------------------------------- Using abstract class to improve encapsulation --------------------------------------------- Tree does the traversal A member of the Visitable abstract class defines what it means to visit a node Abstract class def'n (in Visitable.java) public interface Visitable { public void visit (); } Member class def'n (in Token.java) public class Token implements Visitable { String str; public Token (String str) { this.str = str; } public void visit () { System.out.print (str + " "); } } Abstract tree builder (in Translator.java) String expr = "1 2 3 * +"; StringTokenizer st = new StringTokenizer (expr, " +-*/", true); String token = st.nextToken(); Tree tree = new Tree (new Token (token), null, null)); The infix generator is basically an inorder traversal of the expression tree, except that it might have to insert parentheses under certain conditions.