Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 CS 410 Mastery in Programming Chapter 8 Printing Binary Trees Herbert G. Mayer, PSU CS status 7/30/2011.

Similar presentations


Presentation on theme: "1 CS 410 Mastery in Programming Chapter 8 Printing Binary Trees Herbert G. Mayer, PSU CS status 7/30/2011."— Presentation transcript:

1 1 CS 410 Mastery in Programming Chapter 8 Printing Binary Trees Herbert G. Mayer, PSU CS status 7/30/2011

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

3 3 Arithmetic Expressions and Trees a + x ^ c + a^ xc Infix:( a + ( x ^ c ) ) Postfix:a x c ^ + Prefix:+ a ^ x c

4 4 Arithmetic Expressions and Trees ( x – a ) / b / - b ax Infix:( ( x – a ) ) / b Postfix:x a – b / Prefix:/ - x a b

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

6 6 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 if } //end Print_No_Paren Input: ( a + x ) / b prints as: a + x / b -- misleading

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

8 8 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

9 9 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 if } //end Print_Prefix Input: ( a + x ) / b prints as: / + a x b -- OK

10 10 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

11 11 Use of Postfix Polish Postfix notation is a natural for code generation targeted for stack machines Polish Postfix notation is a natural for code generation targeted for stack machines Operands are needed first: Two for dyadic, or one for monadic operations Operands are needed first: Two for dyadic, or one for monadic operations Once generated and available on stack, execute next operation Once generated and available on stack, execute next operation Easy for compiler writer, natural for stack machine, poor for execution, as all references are through memory: top of stack Easy for compiler writer, natural for stack machine, poor for execution, as all references are through memory: top of stack


Download ppt "1 CS 410 Mastery in Programming Chapter 8 Printing Binary Trees Herbert G. Mayer, PSU CS status 7/30/2011."

Similar presentations


Ads by Google