Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS 201 Computer Systems Programming Chapter 7 “Printing Binary Trees”

Similar presentations


Presentation on theme: "CS 201 Computer Systems Programming Chapter 7 “Printing Binary Trees”"— Presentation transcript:

1 CS 201 Computer Systems Programming Chapter 7 “Printing Binary Trees”
Herbert G. Mayer, PSU CS Status 11/1/2013

2 Syllabus Arithmetic Expressions and Trees Infix Without Parentheses
Infix With Parentheses Postfix Without Parentheses Prefix Without Parentheses Interesting Examples Use of Postfix

3 Arithmetic Expressions and Trees
Three typical notations for dyadic operations: Infix notation: write as the first the left operand, reading left-to- right, then list the dyadic operator, finally list the right operand For CPU: Order will not work for code emission, as the CPU needs both operands for processing the operator For humans: requires parentheses for proper operator precedence Note exception: programming language APL Postfix notation: write left operand first, then list the right operand, finally the operator This order will work for code emission, as operator has both operands available at processing time Needs no parentheses, and still obeys operator precedence Postfix notation AKA Polish Postfix, after Jan Łukasiewicz, 1920 Prefix notation: First list the operator, next the first (left) operand, finally the second (right) operand

4 Arithmetic Expressions and Trees
a + x ^ c Infix: ( a + ( x ^ c ) ) Postfix: a x c ^ + Prefix: + a ^ x c + a ^ x c ^ stands for exponentiation operator, with highest precedence: higher than * or /

5 Arithmetic Expressions and Trees
( x – a ) / b Infix: ( ( x – a ) ) / b Postfix: x a – b / Prefix: / – x a b / - b x a / stands for division operator, with higher precedence than, say, –

6 Arithmetic Expressions and Trees
a ^ ( b – c ) / d Infix: ( ( a ^ ( b – c ) ) / d ) Postfix: a b c - ^ d / Prefix: / ^ a – b c d / ^ d a - b c

7 Data Structure to Print Trees
// node has class: literal, identifier, or operator. // Parenthesized expressions have been reduced: no ( ) typedef enum { Literal, Identifier, Operator } NodeClass; typedef struct NodeType * NodePtr; // forward // actual node structure; using the forward pointers typedef struct NodeType { NodeClass Class; // 3 classes. Not C++ ‘class’ char Symbol; // stores ident or small literal int LitVal; // if Class == Literal: its value NodePtr Left; // left subtree NodePtr Right; // right subtree } s_node_tp;

8 Infix Without Parentheses
// Print in infix notation without parentheses ( ) void Print_No_Paren( NodePtr Root ) { // Print_No_Paren if ( Root ) { Print_No_Paren ( Root->Left ); if ( Root->Class == Literal ) { printf( "%d", Root->LitVal ); }else{ printf( "%c", Root->Symbol ); } //end if Print_No_Paren ( Root->Right ); } //end Print_No_Paren Input: ( a + x ) / b prints as: a + x / b  misleading

9 Infix With Parentheses
// Print in infix notation with parentheses ( and ) // though prints too many ( ) pairs void Print_Infix( NodePtr Root ) { // Print_Infix if ( Root ) { if ( Root->Class == Operator ) { // should have inverted printf( "(" ); } //end if Print_Infix( Root->Left ); if ( Root->Class == Literal ) { printf( "%d", Root->LitVal ); }else{ printf( "%c", Root->Symbol ); Print_Infix( Root->Right ); if ( Root->Class == Operator ) { printf( ")" ); } //end Print_Infix Input: ( a + x ) / b prints as: ( ( a + x ) / b ) -- OK

10 Postfix Without Parentheses
// Print in Polish Postfix notation, no parentheses void Print_Postfix( NodePtr Root ) { // Print_Postfix if ( Root ) { Print_Postfix( Root->Left ); Print_Postfix( Root->Right ); if ( Root->Class == Literal ) { printf( "%d", Root->LitVal ); }else{ printf( "%c", Root->Symbol ); } //end if } //end Print_Postfix Input: a ^ ( b – c ) / d prints as: a b c - ^ d / -- OK

11 Prefix Without Parentheses
// Prefix: operator executes when 2 operands found void Print_Prefix( NodePtr Root ) { // Print_Prefix if ( Root ) { if ( Root->Class == Literal ) { printf( "%d", Root->LitVal ); }else{ printf( "%c", Root->Symbol ); } //end if Print_Prefix( Root->Left ); Print_Prefix( Root->Right ); } //end Print_Prefix Input: ( a + x ) / b prints as: / + a x b -- OK

12 Interesting Examples Input 1: a + b * c ^ ( x – 2 * d ) / ( e – f )
Infix: ( a + ( ( b * ( c ^ ( x – ( 2 * d ) ) ) ) / ( e – f ) ) ) Postfix: a b c x 2 d * - ^ * e f - / + Prefix: + a / * b ^ c – x * 2 d – e f Input 2: 4 / x ^ ( k – l / m ) * 8 * x - & 9 + n Infix: ( ( ( ( ( 4 / ( x ^ ( k - ( l / m ) ) ) ) * 8 ) * x ) - ( & 9 ) ) + n ) Postfix: 4 x k l m / - ^ / 8 * x * 9 & - n + Prefix: + - * * / 4 ^ x – k / l m 8 x & 9 n

13 Use of Postfix Postfix, AKA Polish Postfix notation is a natural for code generation, not just for stack machines Operands are needed first: Two for dyadic, or one for monadic operations Once generated and available on stack, stack machine can execute the next operation Easy for compiler writer, natural for stack machine Stack poor for execution, as all references are through memory: top of stack Even a GPR architecture needs both operands available somewhere (in regs) to execute operator

14 References Łukasiewicz: http://www.calculator.org/Lukasiewicz.aspx


Download ppt "CS 201 Computer Systems Programming Chapter 7 “Printing Binary Trees”"

Similar presentations


Ads by Google