+ All Categories
Home > Documents > noter ch.3 Modeling recursive structure by class hierarchy ... · Recursive data structures noter...

noter ch.3 Modeling recursive structure by class hierarchy ... · Recursive data structures noter...

Date post: 19-May-2021
Category:
Upload: others
View: 19 times
Download: 0 times
Share this document with a friend
64
Recursive data structures noter ch.3 Modeling recursive structure by class hierarchy Recursive traversal of structure
Transcript
Page 1: noter ch.3 Modeling recursive structure by class hierarchy ... · Recursive data structures noter ch.3 Modeling recursive structure by class hierarchy Recursive traversal of structure.

Recursive data structuresnoter ch.3

Modeling recursive structure by class hierarchy

Recursive traversal of structure

Page 2: noter ch.3 Modeling recursive structure by class hierarchy ... · Recursive data structures noter ch.3 Modeling recursive structure by class hierarchy Recursive traversal of structure.

Recursive data structure• Recursive structure:

– list, tree• Object oriented representation

– Interface– Implementing classes

• Traversal of recursive structure– External recursive method– Internal recursive method (Composite design pattern)– Visitor design pattern

• Building a recursive structure– Parsing using mutually recursive methods

Page 3: noter ch.3 Modeling recursive structure by class hierarchy ... · Recursive data structures noter ch.3 Modeling recursive structure by class hierarchy Recursive traversal of structure.

File system

Page 4: noter ch.3 Modeling recursive structure by class hierarchy ... · Recursive data structures noter ch.3 Modeling recursive structure by class hierarchy Recursive traversal of structure.

File system

• A directory containing (smaller) file systems

• Or a single file

Page 5: noter ch.3 Modeling recursive structure by class hierarchy ... · Recursive data structures noter ch.3 Modeling recursive structure by class hierarchy Recursive traversal of structure.

Arithmetic expression

• An expression is either– a constant, or– an operator and two smaller expressions

Page 6: noter ch.3 Modeling recursive structure by class hierarchy ... · Recursive data structures noter ch.3 Modeling recursive structure by class hierarchy Recursive traversal of structure.

Expression as binary tree

• Recursive structureNode

Leaf

A binary tree is• an internal node (root)

and two smaller trees, or• a leaf

Page 7: noter ch.3 Modeling recursive structure by class hierarchy ... · Recursive data structures noter ch.3 Modeling recursive structure by class hierarchy Recursive traversal of structure.

Classes for modeling recursive expression

public interface Tree { }

public class Leaf implements Tree {private int value;public Leaf(int n) { value = n; }public int getValue() { return value; }

}

public class Node implements Tree {private Operator op;private Tree left;private Tree right;public Node(Tree l, Operator o, Tree r){ op = o; left = l; right = r; }

public Operator getOp() { return op; }public Tree getLeft() { return left; }public Tree getRight() { return right; }

}

Page 8: noter ch.3 Modeling recursive structure by class hierarchy ... · Recursive data structures noter ch.3 Modeling recursive structure by class hierarchy Recursive traversal of structure.

Enumeration type: Operatorpublic enum Operator {PLUS("+"), MINUS("-"), MULT("*"), DIV("/");private String name;private Operator(String name) { this.name = name; }public String toString() { return name; }public static Operator parseOp(String s) {

if (Operator.PLUS.name.equals(s)) return Operator.PLUS;if (Operator.MINUS.name.equals(s)) return Operator.MINUS;if (Operator.MULT.name.equals(s)) return Operator.MULT;if (Operator.DIV.name.equals(s)) return Operator.DIV;throw new UnsupportedOperationException(s);

}public int apply(int left, int right) {

switch (this) {case PLUS: return left + right; case MINUS: return left - right; case MULT: return left * right; case DIV: return left / right; default:

throw new UnsupportedOperationException(this.toString());}

}}

Page 9: noter ch.3 Modeling recursive structure by class hierarchy ... · Recursive data structures noter ch.3 Modeling recursive structure by class hierarchy Recursive traversal of structure.

representation of an expression

Tree t =new Node(new Leaf(6),Operator.PLUS,new Node(new Node(new Leaf(8),Operator.MULT,new Leaf(2)),Operator.MINUS,new Leaf(1)

));

Page 10: noter ch.3 Modeling recursive structure by class hierarchy ... · Recursive data structures noter ch.3 Modeling recursive structure by class hierarchy Recursive traversal of structure.

Recursive bullet list

• In webbrowser • html source code<ul>

<li>a simple bullet list</li><li>containing smaller lists</li><li><ul>

<li>a smaller sublist</li><li>

<ul><li>a tiny list</li><li>with several entries</li>

</ul></li><li>look: recursive lists!</li>

</ul></li>

</ul>

Page 11: noter ch.3 Modeling recursive structure by class hierarchy ... · Recursive data structures noter ch.3 Modeling recursive structure by class hierarchy Recursive traversal of structure.

QUIZ UML and recursion

Which UML diagram models recursive bullet lists best?

1.

4.

2.

3.

5. I don’t know

Page 12: noter ch.3 Modeling recursive structure by class hierarchy ... · Recursive data structures noter ch.3 Modeling recursive structure by class hierarchy Recursive traversal of structure.

Traversal of recursive structure

• traversal– calculate value of expression– print expression

• traversal techniques– external recursive method– internal mutually recursive methods

(composite pattern)– visitor pattern

Page 13: noter ch.3 Modeling recursive structure by class hierarchy ... · Recursive data structures noter ch.3 Modeling recursive structure by class hierarchy Recursive traversal of structure.

traversal: expression evaluation• post order traversal: visit children (subexpressions)

before visiting root node (operator)

evaluate(v):if v is a leaf:return number stored at v

elsex = evaluate(left subexpression stored at v)y = evaluate(right subexpression stored at v)return x o y (where o is operator stored at v)

Page 14: noter ch.3 Modeling recursive structure by class hierarchy ... · Recursive data structures noter ch.3 Modeling recursive structure by class hierarchy Recursive traversal of structure.

evaluation by single external recursive method

public int evaluate(Tree w) {int answer;if ( w instanceof Leaf ) answer = ((Leaf)w).getValue();else {Tree l = ((Node)w).getLeft();Tree r = ((Node)w).getRight();Operator o = ((Node)w).getOp();int left_answer = evaluate(l);int right_answer = evaluate(r);answer = o.apply(left_answer,right_answer);

}return answer;

}

instanceof-test necessary

Java technicality:lots of casts obscures the code

Page 15: noter ch.3 Modeling recursive structure by class hierarchy ... · Recursive data structures noter ch.3 Modeling recursive structure by class hierarchy Recursive traversal of structure.

Recursive bullet list• In webbrowser • html source code

<ul><li>a simple bullet list</li><li>containing smaller lists</li><li><ul>

<li>a smaller sublist</li><li>

<ul><li>a tiny list</li><li>with several entries</li>

</ul></li><li>look: recursive lists!</li>

</ul></li>

</ul>

• UML model

Page 16: noter ch.3 Modeling recursive structure by class hierarchy ... · Recursive data structures noter ch.3 Modeling recursive structure by class hierarchy Recursive traversal of structure.

public String print(HTML h) {String result = "";if (h instanceof Text) result = h.getText();else {

result += "<ul>";for (HTML k : h.getEntries())

result += "<li>"+print(k)+"</li>";result += "</ul>";

}return result;

}

How should the compiler errors be fixed?1. There should be no errors – update java compiler to newer version!2. Declare getText() and getEntries() in interface HTML3. Type cast h to Text and BulletList, respectively4. Fix in some other way5. I don’t know

QUIZTraversal by externalrecursive method

Page 17: noter ch.3 Modeling recursive structure by class hierarchy ... · Recursive data structures noter ch.3 Modeling recursive structure by class hierarchy Recursive traversal of structure.

internal mutually recursive methods• Ensure that all classes implementing Tree define a getValue method

• In interface Treepublic abstract int getValue();

• in class Leaf:public int getValue() { return value; }

• in class Node:public int getValue() {

return op.apply(left.getValue(),right.getValue());

}

instanceof-tests and casts are no longer

necessary!

Page 18: noter ch.3 Modeling recursive structure by class hierarchy ... · Recursive data structures noter ch.3 Modeling recursive structure by class hierarchy Recursive traversal of structure.

Node expr1 =new Node(new Leaf(8),Operator.MULT,new Node(new Leaf(5),Operator.PLUS,new Leaf(2)

));

Example: 8 * (5 + 2)

Page 19: noter ch.3 Modeling recursive structure by class hierarchy ... · Recursive data structures noter ch.3 Modeling recursive structure by class hierarchy Recursive traversal of structure.

• in class Leaf:public int getValue() { return value; }

• in class Node:public int getValue() {

int l = left.getValue();int r = right.getValue();return op.apply(l,r);

}

Example: 8 * (5 + 2)expr1.getValue()

Page 20: noter ch.3 Modeling recursive structure by class hierarchy ... · Recursive data structures noter ch.3 Modeling recursive structure by class hierarchy Recursive traversal of structure.

Example: 8 * (5 + 2)expr1.getValue()

Page 21: noter ch.3 Modeling recursive structure by class hierarchy ... · Recursive data structures noter ch.3 Modeling recursive structure by class hierarchy Recursive traversal of structure.

Example: 8 * (5 + 2)expr1.getValue()

Page 22: noter ch.3 Modeling recursive structure by class hierarchy ... · Recursive data structures noter ch.3 Modeling recursive structure by class hierarchy Recursive traversal of structure.

Example: 8 * (5 + 2)expr1.getValue()

Page 23: noter ch.3 Modeling recursive structure by class hierarchy ... · Recursive data structures noter ch.3 Modeling recursive structure by class hierarchy Recursive traversal of structure.

Example: 8 * (5 + 2)expr1.getValue()

Page 24: noter ch.3 Modeling recursive structure by class hierarchy ... · Recursive data structures noter ch.3 Modeling recursive structure by class hierarchy Recursive traversal of structure.

Example: 8 * (5 + 2)expr1.getValue()

Page 25: noter ch.3 Modeling recursive structure by class hierarchy ... · Recursive data structures noter ch.3 Modeling recursive structure by class hierarchy Recursive traversal of structure.

Example: 8 * (5 + 2)expr1.getValue()

Page 26: noter ch.3 Modeling recursive structure by class hierarchy ... · Recursive data structures noter ch.3 Modeling recursive structure by class hierarchy Recursive traversal of structure.

Example: 8 * (5 + 2)expr1.getValue()

Page 27: noter ch.3 Modeling recursive structure by class hierarchy ... · Recursive data structures noter ch.3 Modeling recursive structure by class hierarchy Recursive traversal of structure.

Recursive bullet list• In webbrowser • html source code

<ul><li>a simple bullet list</li><li>containing smaller lists</li><li><ul>

<li>a smaller sublist</li><li>

<ul><li>a tiny list</li><li>with several entries</li>

</ul></li><li>look: recursive lists!</li>

</ul></li>

</ul>

• UML model

Page 28: noter ch.3 Modeling recursive structure by class hierarchy ... · Recursive data structures noter ch.3 Modeling recursive structure by class hierarchy Recursive traversal of structure.

QUIZ

Code in class BulletList:

public String getText() {String result = "<ul>";for (HTML h : getEntries())

result += "<li>"+h.getText()+"</li>";return result+"</ul>";

}

Traversal by internal recursive method

How should the compiler error be fixed?1. There should be no errors – update java

compiler to newer version!2. Declare getText() in interface HTML3. Type cast h to Text4. Type cast h to BulletList5. Fix in some other way6. I don’t know

Page 29: noter ch.3 Modeling recursive structure by class hierarchy ... · Recursive data structures noter ch.3 Modeling recursive structure by class hierarchy Recursive traversal of structure.

Comparing traversal techniques

• external recursive method:– obscures code by instanceof test and casts

• internal mutually recursive methods:– for each new kind of traversal it is necessary to mess

around with the code of all classes to insert new methods

• Visitor pattern (avoids above problems):– inserts a single method in all classes once and for all– these methods make call back to problem specific

external methods

Page 30: noter ch.3 Modeling recursive structure by class hierarchy ... · Recursive data structures noter ch.3 Modeling recursive structure by class hierarchy Recursive traversal of structure.

Visitor design pattern

• Decoupling of Tree hierarchy and problem specific traversal.

• The Tree hierarchy is prepared by adding an accept method capable of performing callback

• A problem specific traversal (such as value computation) requires an implementation of interface TreeVisitor

Page 31: noter ch.3 Modeling recursive structure by class hierarchy ... · Recursive data structures noter ch.3 Modeling recursive structure by class hierarchy Recursive traversal of structure.

• EvaluateVisitor is the client.• Mutually recursive accept / visitNode• Recursion stops when calling visitLeaf

Page 32: noter ch.3 Modeling recursive structure by class hierarchy ... · Recursive data structures noter ch.3 Modeling recursive structure by class hierarchy Recursive traversal of structure.

Modification of Tree classes• The recursive structure is prepared (once and for all) by adding an

accept method to Tree and all its implementing classes

• In interface Treepublic <T> T accept(TreeVisitor<T> v) ;

• In class Leafpublic <T> T accept(TreeVisitor<T> v){return v.visitLeaf(this);

}

• In class Nodepublic <T> T accept(TreeVisitor<T> v){return v.visitNode(this);

}

callback to problem specific method

callback to problem specific methods defined

by external visitor

Page 33: noter ch.3 Modeling recursive structure by class hierarchy ... · Recursive data structures noter ch.3 Modeling recursive structure by class hierarchy Recursive traversal of structure.

Problem specific TreeVisitor• A problem specific traversal requires an implementation

of interface TreeVisitor:public interface TreeVisitor<T> {

public T visitLeaf(Leaf l);public T visitNode(Node n);

}

• For traversal, the TreeVisitor methods send the tree object an accept message.

• The accept methods in turn make call back to the appropriate visitLeaf or visitNode method

• methods visitLeaf/visitNode and accept are mutually recursive.

Page 34: noter ch.3 Modeling recursive structure by class hierarchy ... · Recursive data structures noter ch.3 Modeling recursive structure by class hierarchy Recursive traversal of structure.

Visitor example: expression evaluation

• instanceof test not needed (handled by call back)

class EvaluateVisitor implements TreeVisitor<Integer> {

public Integer visitLeaf(Leaf l) {return l.getValue();

}

public Integer visitNode(Node n) {int l = n.getLeft().accept(this);int r = n.getRight().accept(this);return n.getOp().apply(l,r);

}}

Page 35: noter ch.3 Modeling recursive structure by class hierarchy ... · Recursive data structures noter ch.3 Modeling recursive structure by class hierarchy Recursive traversal of structure.

• In class EvaluateVisitorpublic Integer visitLeaf(Leaf l){ return l.getValue(); }

public Integer visitNode(Node n) {int l = n.getLeft().accept(this);int r = n.getRight().accept(this);return n.getOp().apply(l,r);

}• In class Leafpublic <T> T accept(TreeVisitor<T> v){return v.visitLeaf(this);

}

• In class Nodepublic <T> T accept(TreeVisitor<T> v){return v.visitNode(this);

}

Example: 8 * (5 + 2)expr1.accept(new EvaluateVisitor())

Page 36: noter ch.3 Modeling recursive structure by class hierarchy ... · Recursive data structures noter ch.3 Modeling recursive structure by class hierarchy Recursive traversal of structure.

Example: 8 * (5 + 2)expr1.accept(new EvaluateVisitor())

Page 37: noter ch.3 Modeling recursive structure by class hierarchy ... · Recursive data structures noter ch.3 Modeling recursive structure by class hierarchy Recursive traversal of structure.

Example: 8 * (5 + 2)expr1.accept(new EvaluateVisitor())

Page 38: noter ch.3 Modeling recursive structure by class hierarchy ... · Recursive data structures noter ch.3 Modeling recursive structure by class hierarchy Recursive traversal of structure.

Example: 8 * (5 + 2)expr1.accept(new EvaluateVisitor())

Page 39: noter ch.3 Modeling recursive structure by class hierarchy ... · Recursive data structures noter ch.3 Modeling recursive structure by class hierarchy Recursive traversal of structure.

Example: 8 * (5 + 2)expr1.accept(new EvaluateVisitor())

Page 40: noter ch.3 Modeling recursive structure by class hierarchy ... · Recursive data structures noter ch.3 Modeling recursive structure by class hierarchy Recursive traversal of structure.

Example: 8 * (5 + 2)expr1.accept(new EvaluateVisitor())

Page 41: noter ch.3 Modeling recursive structure by class hierarchy ... · Recursive data structures noter ch.3 Modeling recursive structure by class hierarchy Recursive traversal of structure.

Example: 8 * (5 + 2)expr1.accept(new EvaluateVisitor())

Page 42: noter ch.3 Modeling recursive structure by class hierarchy ... · Recursive data structures noter ch.3 Modeling recursive structure by class hierarchy Recursive traversal of structure.

Example: 8 * (5 + 2)expr1.accept(new EvaluateVisitor())

Page 43: noter ch.3 Modeling recursive structure by class hierarchy ... · Recursive data structures noter ch.3 Modeling recursive structure by class hierarchy Recursive traversal of structure.

Example: Expression evaluation

• Given some treeTree t = ...

• Evaluate by external recursive methodevaluate(t)

• or by internal mutually recursive methodst.getValue()

• or by using visitor patternt.accept(new EvaluateVisitor())

Page 44: noter ch.3 Modeling recursive structure by class hierarchy ... · Recursive data structures noter ch.3 Modeling recursive structure by class hierarchy Recursive traversal of structure.

Recursive bullet list• In webbrowser • html source code

<ul><li>a simple bullet list</li><li>containing smaller lists</li><li><ul>

<li>a smaller sublist</li><li>

<ul><li>a tiny list</li><li>with several entries</li>

</ul></li><li>look: recursive lists!</li>

</ul></li>

</ul>

• UML model

Page 45: noter ch.3 Modeling recursive structure by class hierarchy ... · Recursive data structures noter ch.3 Modeling recursive structure by class hierarchy Recursive traversal of structure.

QUIZ

public class PrintVisitor implements HTMLVisitor<String> {

public String visitText(Text t) { return t.getText(); }

public String visitBulletList(BulletList b) {String result = "<ul>";for (HTML h : b.getEntries()) result += "<li>"+ +"</li>";return result+"</ul>”;

}}

What code should replace ?1. b.getText()2. b.accept(this)3. b.accept(new PrintVisitor());4. h.getText()5. h.accept(this)6. h.accept(new PrintVisitor());7. None of the above8. I don’t know

Traversal by visitor

Page 46: noter ch.3 Modeling recursive structure by class hierarchy ... · Recursive data structures noter ch.3 Modeling recursive structure by class hierarchy Recursive traversal of structure.

traversal: printing expression• in order traversal: visit left child (subexpression) before

visiting root node (operator), and finally visit right child

text(v):if v is a leaf:return number

elsereturn "("+ text( left subexpression)+ operator+ text( right subexpression )+ ")"

Page 47: noter ch.3 Modeling recursive structure by class hierarchy ... · Recursive data structures noter ch.3 Modeling recursive structure by class hierarchy Recursive traversal of structure.

Printing expression using visitor• Computing String representation of expression (printing)

class PrintVisitor implements TreeVisitor<String> {

public String visitLeaf(Leaf l) {return new Integer(l.getValue()).toString();

}

public String visitNode(Node n) {return ("(" + n.getLeft().accept(this)

+ n.getOp()+ n.getRight().accept(this) + ")");

}}• Application: System.out.println(t.accept(new PrintVisitor()));

Page 48: noter ch.3 Modeling recursive structure by class hierarchy ... · Recursive data structures noter ch.3 Modeling recursive structure by class hierarchy Recursive traversal of structure.

Traversal example: drawing expression

state : a current drawing position (x,y)initially (x,y) = (0,0)

drawSymbol(s):increment x and draw s;

draw(v):if v is a leaf:drawSymbol( number );

elseincrement y;draw( left subexpression );decrement y;drawSymbol( operator );increment y;draw( right subexpression );decrement y;

pseudocode ignores connection lines.

Page 49: noter ch.3 Modeling recursive structure by class hierarchy ... · Recursive data structures noter ch.3 Modeling recursive structure by class hierarchy Recursive traversal of structure.

Adding connection lines to drawingstate : a current drawing position (x,y)

initially (x,y) = (0,0)drawSymbol(s): // returns position where s is drawnincrement x and draw s;return (x,y)

draw(v): // returns where root of expression is drawnif v is a leaf:return drawSymbol( number );

elseincrement y;l = draw( left subexpression );decrement y;c = drawSymbol( operator );draw line from l to c;increment y;r = draw( right subexpression );decrement y;draw line from r to c;return c;

drawing of connection lines requires that

draw methods return positions for later use

Page 50: noter ch.3 Modeling recursive structure by class hierarchy ... · Recursive data structures noter ch.3 Modeling recursive structure by class hierarchy Recursive traversal of structure.

class Draw ...class DrawVisitor extends JComponent implements

TreeVisitor<Point> {private static final int UNIT = 30;private Tree t;private Point pen_pos;private Graphics2D g;public DrawVisitor(Tree t) {this.t = t;JFrame f = new JFrame();f.add(this);f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);f.setSize(400,400);f.setVisible(true);

}...public void paintComponent(Graphics g) {this.g = (Graphics2D)g;pen_pos = new Point(UNIT,UNIT);t.accept(this);

}}

Page 51: noter ch.3 Modeling recursive structure by class hierarchy ... · Recursive data structures noter ch.3 Modeling recursive structure by class hierarchy Recursive traversal of structure.

... implements TreeVisitorprivate Point drawSymbol(Object ob) {pen_pos.x += UNIT;g.drawString(ob.toString(),pen_pos.x,pen_pos.y-4);return (Point) pen_pos.clone();

}public Point visitLeaf(Leaf l) {return drawSymbol( new Integer(l.getValue()) );

}public Point visitNode(Node n) {pen_pos.y += UNIT;Point left_pos = n.getLeft().accept(this);pen_pos.y -= UNIT;Point node = drawSymbol(n.getOp());g.draw(new Line2D.Double(left_pos,node));pen_pos.y += UNIT;Point right_pos = n.getRight().accept(this);pen_pos.y -= UNIT;g.draw(new Line2D.Double(right_pos,node));return node;

}

Page 52: noter ch.3 Modeling recursive structure by class hierarchy ... · Recursive data structures noter ch.3 Modeling recursive structure by class hierarchy Recursive traversal of structure.

• Actual drawing made by new DrawVisitor(t)

• where Tree t = new Node(new Leaf(6),Operator.PLUS,

new Node(new Node(new Leaf(8),Operator.MULT,new Leaf(2)),Operator.MINUS,new Leaf(1)));

Page 53: noter ch.3 Modeling recursive structure by class hierarchy ... · Recursive data structures noter ch.3 Modeling recursive structure by class hierarchy Recursive traversal of structure.

Recursive bullet list• In webbrowser • html source code

<ul><li>a simple bullet list</li><li>containing smaller lists</li><li><ul>

<li>a smaller sublist</li><li>

<ul><li>a tiny list</li><li>with several entries</li>

</ul></li><li>look: recursive lists!</li>

</ul></li>

</ul>

• UML model

Page 54: noter ch.3 Modeling recursive structure by class hierarchy ... · Recursive data structures noter ch.3 Modeling recursive structure by class hierarchy Recursive traversal of structure.

QUIZ

public class PrintVisitor implements HTMLVisitor<String> {

public String visitText(Text t) { return t.getText(); }

public String visitBulletList(BulletList b) {String result = "<ul>";for (HTML h : b.getEntries())

result += "<li>"+h.accept(this)+"</li>";return result+"</ul>”;

}} How would you declare accept in interface HTML?

1. public <E> E accept(HTMLVisitor<E> v);2. public E accept(HTMLVisitor<E> v);3. public String accept(HTMLVisitor<String> v);4. public String accept(PrintVisitor v);5. I don’t know

Traversal by visitor

Page 55: noter ch.3 Modeling recursive structure by class hierarchy ... · Recursive data structures noter ch.3 Modeling recursive structure by class hierarchy Recursive traversal of structure.

Parsing an expression

Tree t =new Node(

new Leaf(6),Operator.PLUS,new Node(

new Node(new Leaf(8),Operator.MULT,new Leaf(2)),Operator.MINUS,new Leaf(1)

));

Easier:

Tree t = new Parser(“6+(8*2-1)”).parseExpression();

Page 56: noter ch.3 Modeling recursive structure by class hierarchy ... · Recursive data structures noter ch.3 Modeling recursive structure by class hierarchy Recursive traversal of structure.

Parsing an expression

• Problem:– build the recursive data structure for

arithmetic expressions such as3 + 4 * 5

(3 + 4) * 51 – (2 – (3 – (4 – 5)))

• Precedence rules: – * and / take precedence over + and –– may overrule using parentheses ( ... )

Page 57: noter ch.3 Modeling recursive structure by class hierarchy ... · Recursive data structures noter ch.3 Modeling recursive structure by class hierarchy Recursive traversal of structure.

Syntax diagram for expression

number

Page 58: noter ch.3 Modeling recursive structure by class hierarchy ... · Recursive data structures noter ch.3 Modeling recursive structure by class hierarchy Recursive traversal of structure.

Syntax tree for two expressions

Page 59: noter ch.3 Modeling recursive structure by class hierarchy ... · Recursive data structures noter ch.3 Modeling recursive structure by class hierarchy Recursive traversal of structure.

Mutually recursive methods

• Implement 3 methods that call each other recursivelyparseExpressionparseTermparseFactor

• An ExpressionTokenizer is used to group input in tokens. A token being a string of digits or one of "+", "-", "*", "/", "(", ")". Methods:peekTokennextToken

Page 60: noter ch.3 Modeling recursive structure by class hierarchy ... · Recursive data structures noter ch.3 Modeling recursive structure by class hierarchy Recursive traversal of structure.

public class Expression Parser {public ExpressionParser(String anExpression) {

tokenizer = new ExpressionTokenizer(anExpression);}

public Tree parseExpression() { ... }

public Tree parseTerm() { ... }

public Tree parseFactor() { ... }

private ExpressionTokenizer tokenizer;}

Page 61: noter ch.3 Modeling recursive structure by class hierarchy ... · Recursive data structures noter ch.3 Modeling recursive structure by class hierarchy Recursive traversal of structure.

public Tree parseExpression() {Tree t = parseTerm();while ("+".equals(tokenizer.peekToken())

|| "-".equals(tokenizer.peekToken())) {Operator op = Operator.parseOp(

tokenizer.nextToken());Tree t2 = parseTerm();t = new Node(t,op,t2);

}return t;

}

Page 62: noter ch.3 Modeling recursive structure by class hierarchy ... · Recursive data structures noter ch.3 Modeling recursive structure by class hierarchy Recursive traversal of structure.

public int parseTerm() {Tree t = parseFactor();while ("*".equals(tokenizer.peekToken())

|| "/".equals(tokenizer.peekToken())) {Operator op = Operator.parseOp(

tokenizer.nextToken());Tree t2 = parseFactor();t = new Node(t,op,t2);

}return t;

}

Page 63: noter ch.3 Modeling recursive structure by class hierarchy ... · Recursive data structures noter ch.3 Modeling recursive structure by class hierarchy Recursive traversal of structure.

public int parseFactor() {Tree t;if ("(".equals(tokenizer.peekToken())) {

tokenizer.nextToken();t = parseExpression();tokenizer.nextToken(); // read ")"

} elset = new Leaf(

Integer.parseInt(tokenizer.nextToken()));return t;

}

Page 64: noter ch.3 Modeling recursive structure by class hierarchy ... · Recursive data structures noter ch.3 Modeling recursive structure by class hierarchy Recursive traversal of structure.

QUIZpublic Tree parseTerm() { }

if ("!".equals(tokenizer.peekToken())) {tokenizer.nextToken();return new BoolNode(

BoolOperator.NOT,parseFactor());}return parseFactor();

BoolTree t = parseFactor();if ("!".equals(tokenizer.peekToken())) {

tokenizer.nextToken();t = new BoolNode(BoolOperator.NOT,t);

}return t;

a

b

(Part of) syntax diagram for Boolean expression

Which code could replace ?

1. a2. b3. a and b4. Neither a nor b5. I don’t know

Parse Expression


Recommended