Herbert G. Mayer, PSU CS Status 7/9/2014. CS 201 Computer Systems Programming Chapter 7 “ Printing Binary Trees ”. Syllabus. Arithmetic Expressions and Trees Infix Without Parentheses Infix With Parentheses Postfix Without Parentheses Prefix Without Parentheses Interesting Examples
Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.
Status 7/9/2014
CS 201Computer Systems ProgrammingChapter 7“Printing Binary Trees”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 /
which in turn have higher priority than + or 
Infix: ( ( x – a ) / b )
Postfix: x a – b /
Prefix: / – x a b
/

b
x
a
/ stands for division operator, with higher precedence than, say, –
Infix: ( ( a ^ ( b – c ) ) / d )
Postfix: a b c  ^ d /
Prefix: / ^ a – b c d
Wrong: a ^ ( ( b – c ) / d )
/
^
d
a

b
c
Input: ( a + x ) / b prints as: a + x / b misleading
Input: ( a + x ) / b prints as: ( ( a + x ) / b )  OK
// 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 if
} //end Print_Postfix
Input: a ^ ( b – c ) / d prints as: a b c  ^ d /  OK
// 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
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