Docsity

Tree Traversals & Expression Trees in CS2110: Pre, Post, In-order & Postfix, Study notes of Astrophysics

Partial preview of the text

Download Tree Traversals & Expression Trees in CS2110: Pre, Post, In-order & Postfix and more Study notes Astrophysics in PDF only on Docsity!

ASTS, GRAMMARS, PARSING,

TREE TRAVERSALS

Lecture 14 CS2110 – Fall 2018 1

Announcements

2 ¨ Today: The last day to request prelim regrades ¨ Assignment A4 due next Thursday night. Please work on it early and steadily. Watch the two videos on recursion on trees before working on A4! ¨ Next week’s recitation. Learn about interfaces Iterator and Iterable. There will be 15 minutes of videos to watch. Then, in recitation, you will fix your A3 so that a foreach loop can be used on it. DLL d= new DLL(); … for (Integer i : d) { … }

Pre-order, Post-order, and In-order

4

2 1

Pre-order traversal:

  1. Visit the root
  2. Visit the left subtree (in pre-order)
  3. Visit the right subtree

Pre-order, Post-order, and In-order

5

2 1

Post-order traversal

  1. Visit the left subtree (in post-order)
  2. Visit the right subtree
  3. Visit the root Pre-order traversal - (^) *** 2 1 + 1 0** 2 1 * 1 0 + -

Pre-order, Post-order, and In-order

7

2 1

Post-order traversal Pre-order traversal - (^) *** 2 1 + 1 0 2 1 * 1 0 + -** In-order traversal (2 * 1)^ -^ (1 + 0) To avoid ambiguity, add parentheses around subtrees that contain operators.

In Defense of Postfix Notation

8

¨ Execute expressions in postfix notation by reading

from left to right.

¨ Numbers: push onto the stack.

¨ Operators: pop the operands off the stack, do the

operation, and push the result onto the stack.

In Defense of Postfix Notation

10

¨ Execute expressions in postfix notation by reading

from left to right.

¨ Numbers: push onto the stack.

¨ Operators: pop the operands off the stack, do the

operation, and push the result onto the stack.

In Defense of Postfix Notation

11

¨ Execute expressions in postfix notation by reading

from left to right.

¨ Numbers: push onto the stack.

¨ Operators: pop the operands off the stack, do the

operation, and push the result onto the stack.

In Defense of Postfix Notation

13

¨ Execute expressions in postfix notation by reading

from left to right.

¨ Numbers: push onto the stack.

¨ Operators: pop the operands off the stack, do the

operation, and push the result onto the stack.

In Defense of Postfix Notation

14

¨ Execute expressions in postfix notation by reading

from left to right.

¨ Numbers: push onto the stack.

¨ Operators: pop the operands off the stack, do the

operation, and push the result onto the stack.

In Defense of Prefix Notation

16

¨ Function calls in most programming languages use

prefix notation: like add(37, 5).

¨ Some languages (Lisp, Scheme, Racket) use prefix

notation for everything to make the syntax simpler.

(define (fib n) (if (<= n 2) 1 (+ (fib (- n 1) (fib (- n 2)))))

Determine tree from preorder and

postorder

18

Suppose inorder is B C A E D

preorder is A B C D E

Can we determine the tree uniquely?

Determine tree from preorder and

postorder

20 Suppose inorder is B C A E D preorder is A B C D E The root is A. Left subtree contains B C Right subtree contains E D Now figure out left, right subtrees using the same method. From the above: For left subtree For right subtree: inorder is: B C inorder is: E D preorder is: B C preorder is: D E root is: B root is: D Right subtree: C left subtree: E

Expression trees: in code

21 public class int implements Expr { private int v; public int eval() { return v; } public String inorder() { return " " + v + " "; } } public class Sum implements Expr { private Expr left, right; public int eval() { return left.eval() + right.eval(); } public String infinorder() { return "(" + left.infix() + "+" + right.infix() + ")"; } } public interface Expr { String inorder(); // returns an inorder representation int eval(); // returns the value of the expression }