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.
Stacks & Their Applications COP Stacks  A stack is a data structure that stores information arranged like a stack.  We have seen stacks before.
PSUCS322 HM 1 Languages and Compiler Design II IR Code Generation I Material provided by Prof. Jingke Li Stolen with pride and modified by Herb Mayer PSU.
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 Example: Stack of plates in cafeteria.
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.
1 CS 162 Introduction to Computer Science Chapter 8 Pointers Herbert G. Mayer, PSU Status 11/20/2014.
Stacks21 Stacks II Adventures in Notation. stacks22 The trouble with infix... Rules for expression evaluation seem simple -- evaluate expression left.
Arithmetic Expressions
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.
4/17/2017 Section 9.3 Tree Traversal ch9.3.
Kymberly Fergusson CSE1303 Part A Data Structures and Algorithms Summer Semester 2003 Lecture A12 – Binary Trees.
1 CS 410 Mastery in Programming Chapter 7 Hints for: Symbolic Differentiation Herbert G. Mayer, PSU CS status 7/26/2011.
1 CS308 Data Structures An application of binary trees: Binary Expression Trees.
Tree Traversal. Traversal Algorithms preorder inorder postorder.
Performing Computations C provides operators that can be applied to calculate expressions: example: tax is 8.5% of the total sale expression: tax =
Binary Trees. Node structure Data A data field and two pointers, left and right.
Chapter Chapter Summary Introduction to Trees Applications of Trees (not currently included in overheads) Tree Traversal Spanning Trees Minimum.
Computer Science 112 Fundamentals of Programming II Expression Trees.
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.
DATA STRUCTURE & ALGORITHMS CHAPTER 3: STACKS. 2 Objectives In this chapter, you will: Learn about stacks Examine various stack operations Discover stack.
Trees.ppt1 Introduction Many data structures are linear –unique first component –unique last component –other components have unique predecessor and successor.
Binary Trees 2 Overview Trees. Terminology. Traversal of Binary Trees. Expression Trees. Binary Search Trees.
1 CS 163 Data Structures Chapter 9 Building, Printing Binary Trees Herbert G. Mayer, PSU Status 5/21/2015.
Binary Trees Definition A binary tree is: (i) empty, or (ii) a node whose left and right children are binary trees typedef struct Node Node; struct Node.
Data Structures – Week #3 Stacks. 9.Mart.2012Borahan Tümer, Ph.D.2 Outline Stacks Operations on Stacks Array Implementation of Stacks Linked List Implementation.
CHAPTER 3 STACK CSEB324 DATA STRUCTURES & ALGORITHM.
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)
Review 1 Polish Notation Prefix Infix Postfix Precedence of Operators Converting Infix to Postfix Evaluating Postfix.
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.
ECE 103 Engineering Programming Chapter 4 Operators Herbert G. Mayer, PSU CS Status 6/19/2015 Initial content copied verbatim from ECE 103 material developed.
1 CS 201 Computer Systems Programming Chapter 7 “Printing Binary Trees” Herbert G. Mayer, PSU CS Status 7/9/2014.
Review Use of Stack Introduction Stack in our life Stack Operations
CS 163 Data Structures Chapter 10 Symbolic Differentiation
MEMORY REPRESENTATION OF STACKS
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
COMPUTER 2430 Object Oriented Programming and Data Structures I
Data Structures – Week #3
Stack application: postponing data usage
PART II STACK APPLICATIONS
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
Trees, part 2 Lecture 13 CS2110 – Spring 2019
ECE 103 Engineering Programming Chapter 64 Tree Implementation
Stack.
Data Structures – Week #3
CS 201 Computer Systems Programming Chapter 7 “Printing Binary Trees”
Presented by : Aman Gupta PGT CS KV No.1, Narimedu, Madurai
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 11/1/2013

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

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 /

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, –

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

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;

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

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

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