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

Slides:



Advertisements
Similar presentations
Operators and Arithmetic Operations. Operators An operator is a symbol that instructs the code to perform some operations or actions on one or more operands.
Advertisements

INFIX, PREFIX, & POSTFIX EXPRESSIONS. Infix Notation We usually write algebraic expressions like this: a + b This is called infix notation, because the.
1 CS 410 Mastery in Programming Chapter 8 Printing Binary Trees Herbert G. Mayer, PSU CS Status 5/22/2013.
Expression Trees What is an Expression tree? Expression tree implementation Why expression trees? Evaluating an expression tree (pseudo code) Prefix, Infix,
Stacks - 3 Nour El-Kadri CSI Evaluating arithmetic expressions Stack-based algorithms are used for syntactical analysis (parsing). For example.
Prefix, Postfix, Infix Notation
Arithmetic Expressions Infix form –operand operator operand 2+3 or a+b –Need precedence rules –May use parentheses 4*(3+5) or a*(b+c)
1 CS 162 Introduction to Computer Science Chapter 9 Binary Trees Herbert G. Mayer, PSU Status 11/23/2014.
D ATA S TRUCTURE MSc.IT Ali Abdul Karem Habib. S TACK A PPLICATIONS Web browser: stores the addresses of recently visited sites on a stack. Each time.
Stacks21 Stacks II Adventures in Notation. stacks22 The trouble with infix... Rules for expression evaluation seem simple -- evaluate expression left.
Arithmetic Expressions
CS 206 Introduction to Computer Science II 03 / 06 / 2009 Instructor: Michael Eckmann.
Stacks21 Stacks II Adventures in Notation. stacks22 The trouble with infix... Rules for expression evaluation seem simple -- evaluate expression left.
Stacks & Queues Infix Calculator CSC 172 SPRING 2002 LECTURE 5.
CS 206 Introduction to Computer Science II 10 / 17 / 2008 Instructor: Michael Eckmann.
Kymberly Fergusson CSE1303 Part A Data Structures and Algorithms Summer Semester 2003 Lecture A12 – Binary Trees.
Infix, Postfix, Prefix.
1 CS 410 Mastery in Programming Chapter 8 Printing Binary Trees Herbert G. Mayer, PSU CS status 7/30/2011.
Tree Traversal. Traversal Algorithms preorder inorder postorder.
Postfix notation. About postfix notation Postfix, or Reverse Polish Notation (RPN) is an alternative to the way we usually write arithmetic expressions.
CS 206 Introduction to Computer Science II 03 / 16 / 2009 Instructor: Michael Eckmann.
Kymberly Fergusson CSE1303 Part A Data Structures and Algorithms Summer Semester 2003 Lecture A12 – Binary Trees.
CS 206 Introduction to Computer Science II 10 / 28 / 2009 Instructor: Michael Eckmann.
Chapter Chapter Summary Introduction to Trees Applications of Trees (not currently included in overheads) Tree Traversal Spanning Trees Minimum.
10.3 Tree Transversal. Pre/post fix notation and order See handout. a.bc.d e f g h i j k.
Stack Applications.
CSC 205 Programming II Postfix Expressions. Recap: Stack Stack features Orderly linear structure Access from one side only – top item Stack operations.
Lecture 17 Today’s Lecture –Instruction formats Little versus big endian Internal storage in the CPU: stacks vs. registers Number of operands and instruction.
DATA STRUCTURE & ALGORITHMS CHAPTER 3: STACKS. 2 Objectives In this chapter, you will: Learn about stacks Examine various stack operations Discover stack.
Binary Trees 2 Overview Trees. Terminology. Traversal of Binary Trees. Expression Trees. Binary Search Trees.
INFIX, PREFIX, & POSTFIX EXPRESSIONS Saurav Karmakar Spring 2007.
1 CS 163 Data Structures Chapter 9 Building, Printing Binary Trees Herbert G. Mayer, PSU Status 5/21/2015.
Linear Data Structures LIFO – Polish notation Context Saving.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 19: Stacks and Queues (part 2)
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 18: Stacks and Queues (part 2)
Prefix, Postfix and Infix. Infix notation  A-B/(C+D)  evaluate C+D (call the result X),  then B/X (call the result Y),  and finally A-Y.  The order.
Semantics (1).
Copyright © Curt Hill Stacks An Useful Abstract Data Type.
CHP-3 STACKS.
Data Structures Azhar Maqsood School of Electrical Engineering and Computer Sciences (SEECS-NUST) Binary Trees.
Prefix, Postfix, Infix Notation. Infix Notation  To add A, B, we write A+B  To multiply A, B, we write A*B  The operators ('+' and '*') go in between.
Semantics(1). 2 Symantec(1)  To provide an authoritative definition of the meaning of all language constructs for: 1.Programmers 2.Compiler writers 3.Standards.
1 CS 201 Computer Systems Programming Chapter 7 “Printing Binary Trees” Herbert G. Mayer, PSU CS Status 7/9/2014.
CSC 172 DATA STRUCTURES. A TALE OF TWO STRUCTURES.
Review Use of Stack Introduction Stack in our life Stack Operations
CS 163 Data Structures Chapter 10 Symbolic Differentiation
Intermediate code Jakub Yaghob
Infix to postfix conversion
Herbert G. Mayer, PSU CS status 7/29/2013
Paul Tymann and Andrew Watkins
Stacks Chapter 7 introduces the stack data type.
CO4301 – Advanced Games Development Week 2 Introduction to Parsing
ASTs, Grammars, Parsing, Tree traversals
CSC 172 DATA STRUCTURES.
COMPUTER 2430 Object Oriented Programming and Data Structures I
Stack application: postponing data usage
Data Structures and Algorithms
PART II STACK APPLICATIONS
CS 201 Computer Systems Programming Chapter 7 “Printing Binary Trees”
Stacks Chapter 5 Adapted from Pearson Education, Inc.
Stacks, Queues, and Deques
Section 9.3 by Andrew Watkins
A Closer Look at Instruction Set Architectures Chapter 5
Queue Applications Lecture 31 Mon, Apr 9, 2007.
Infix to Postfix Conversion
(Part 2) Infix, Prefix & Postfix
Trees, part 2 Lecture 13 CS2110 – Spring 2019
Queue Applications Lecture 31 Tue, Apr 11, 2006.
Stacks A stack is an ordered set of elements, for which only the last element placed into the stack is accessible. The stack data type is also known as.
Presentation transcript:

CS 201 Computer Systems Programming Chapter 7 “Printing Binary Trees” Herbert G. Mayer, PSU CS Status 9/27/2012

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

Arithmetic Expressions and Trees Three typical notations for dyadic operations: Infix notation: write/process first/left operand, then list the operator, finally the last/right operand Clearly this order will not work for code emission in a compiler. Operator needs both operands available Postfix notation: write/process first/left operand, then list the last/right operand, finally the operator This order will work for code emission, as operator has both operands available at execution time Needs no parentheses, and still obeys operator precedence Postfix AKA Polish Postfix, after Jan Łukasiewicz, 1920 Prefix notation: First list the operator, next the first operand, finally the last/right operand Will also not work for code emission in a compiler Also needs to parentheses, but stacks to handle precedences

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, highest precedence

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

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

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

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 ); Print_Infix( Root->Right ); printf( ")" ); } //end Print_Infix Input: ( a + x ) / b prints as: ( ( a + x ) / b ) -- OK

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

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

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

Use of Postfix 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 Once generated and available on stack, execute 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

References Łukasiewicz: http://www.calculator.org/Lukasiewicz.aspx http://cslibrary.stanford.edu/110/BinaryTrees.html