Presentation is loading. Please wait.

Presentation is loading. Please wait.

Podcast Ch17a Title: Expression Trees

Similar presentations


Presentation on theme: "Podcast Ch17a Title: Expression Trees"— Presentation transcript:

1 Podcast Ch17a Title: Expression Trees
Description: Building an expression tree; buildExpTree method; Program 17.1 Participants: Barry Kurtz (instructor); John Helfert and Tobie Williams (students) Textbook: Data Structures for Java; William H. Ford and William R. Topp

2 Expression Trees A binary expression tree represents an arithmetic expression. In an expression tree each operator is an interior node whose children are operands or subexpressions. Operands are in leaf nodes.

3 Student Question Draw the expression tree for the following
a b + c - d * e / f % a b c d e f * / % a b c d e f * / %

4 Practice Problem Draw the expression trees for the following
a b + c - d * e f / % a b c d e * / f %

5 Expression Trees (continued)
The preorder and postorder traversals of a binary expression tree produce the prefix and postfix (RPN) notation for the expression. An inorder traversal generates the infix form of the expression, assuming that parentheses are not needed to determine the order of evaluation; the traversal can be modified to insert full parenthesization

6 Expression Trees (continued)
Preorder(Prefix): a / * b c d e Inorder(Infix): a + b * c / d - e Postorder(Postfix): a b c * d / + e -

7 Student Question The infix expression generated by an inorder traversal will often be incorrect due to parentheses in the original expression We can fix this by having the traversal insert one set of parentheses for each operand How would you modify the traversal to do this?

8 Practice Coding Write a method, call it fullParenInfix that scans an expression tree and returns as a string the fully parenthesized infix expression The previous discussion by John and Tobie should get you started

9 Building a Binary Expression Tree
Build an expression tree from a postfix expression using an iterative algorithm. An operand is a single character such as 'a' or 'b'. If the token is an operand, create a leaf node whose value is the operand and whose left and right subtrees are null. Push the node onto a stack of TNode references.

10 Building a Binary Expression Tree (continued)
If the token is an operator, create a new node with the operator as its value. Pop the two child nodes from the stack and attach them to the new node. The first child popped from the stack becomes the right subtree of the new node and the second child popped from the stack becomes the left subtree.

11 Building a Binary Expression Tree (continued)
a b c * +

12 Student Question Show the stack and the partial expression trees, step by step, for the expression a b c - d e / + *

13 Practice Problem Show the stack and the partial expression trees, step by step, for the expression a b - c d / e + *

14 buildExpTree() public static TNode<Character> buildExpTree(String postfixExp){ // newNode is a reference to the root of subtrees // we build, and newLeft/newRight are its children TNode<Character> newNode, newLeft, newRight; char token; // subtrees go into and off the stack ALStack<TNode<Character>> s = new ALStack<TNode<Character>>(); int i = 0, n = postfixExp.length(); // loop until i reaches the end of the string while(i != n) { // skip blanks and tabs in the expression while (postfixExp.charAt(i) == ' ' || postfixExp.charAt(i) == '\t') i++;

15 buildExpTree() (cont)
// if the expression has trailing whitespace, // we could be at the end of the string if (i == n) break; // extract the current token and increment i token = postfixExp.charAt(i); i++; // see if the token is an operator or operand if (token == '+' || token == '-' || token == '*' || token == '/') { // current token is an operator; pop two // subtrees off the stack newRight = s.pop(); newLeft = s.pop();

16 buildExpTree() (continued)
// create a new subtree with token as root and // subtrees newLeft and newRight and push it // onto the stack newNode = new TNode<Character>(token, newLeft,newRight); s.push(newNode); } else // must be an operand { // create a leaf node and push it onto the stack newNode = new TNode<Character>(token);

17 buildExpTree() (concluded)
// if the expression was not empty, the root of // the expression tree is on the top of the stack if (!s.isEmpty()) return s.pop(); else return null; }

18 Run of Program 17.1

19 Program 17.1 (continued) public void actionPerformed(ActionEvent ae) {
// obtain the expression the user typed String expression = expInput.getText(); // build the expression tree TNode<Character> root = BinaryTree.buildExpTree(expression); // output the expression and its tree textArea.append("Expression tree for " + expression + "\n\n"); textArea.append(BinaryTree.displayTree(root, 1) + "\n");

20 Program 17.1 (concluded) // output the scans
textArea.append("Preorder scan: " + BinaryTree.preorderDisplay(root) + "\n\n"); textArea.append("Inorder scan: " + BinaryTree.inorderDisplay(root) + "\n\n"); textArea.append("Postorder scan: " + BinaryTree.postorderDisplay(root) + "\n\n"); // clear the text field expInput.setText(""); }

21 Student Question Using the normal rules for infix arithmetic, did the inorder scan produce the correct infix expression? If you used the fully parenthesized scan, what expression would have been printed?


Download ppt "Podcast Ch17a Title: Expression Trees"

Similar presentations


Ads by Google