D Infix D Prefix D Postfix in Infix Notation Operator(S
Total Page:16
File Type:pdf, Size:1020Kb
Expression There are 3 ways to write an expression in computer science:- Infix Prefix Postfix Infix Notation In infix notation operator(s) is/are written in between the operands they operate on as: A + B Example: - Infix Notation: 8 - 5 * 4 + 2 / 3 Prefix Notation In Prefix notation operator(s) is/are written before the operands as: +A B Prefix notation is also known as Polish notation. Example:- Prefix Notation: * - 8 5 / + 4 2 3 Postfix Notation In postfix notation operator(s) is/are written after operands as: A B + Prefix notation is also known as Reverse Polish notation or Suffix notation. Example:- Postfix Notation: 8 5 - 4 2 + 3 / * Debate When you write an arithmetic expression such as 5 * 7, we know that the 5 is being multiplied by the 7 since the multiplication operator * appears between them in the expression. This type of notation is referred to as infix. This is simple but next example is not. Consider another infix example, 5 + 7 * 2. The operators + and * still appear between the operands, but there is a problem as given. 5 +7 * 2 12 * 2 24 5 +7 * 2 5 + 14 19 This expression seems ambiguous. Because same expression have two different interpretation and have two result. Which Think about this expression in one is the final result..? evaluated on digital machine (computer). Computer have no idea But we are human being, and we know the about precedence and associativity associativity and precedence concepts of rule. operator. Computer have two possibility If you are not aware about these concepts, either to evaluate above equal to 24 then let us takes a quick eye on precedence or 19. So which one is the right. For and associativity before moving further. a computer to take this decision by its own is difficult so we have to use other notations. Precedence Precedence defines the rule that specifies the order in which certain operations are performed in an expression. It defines that the operator of highest priority are evaluated first on operand. We can say that, a given expression containing more than two operators, it determines which operations should be calculated first. Operators with high precedence are evaluated before operators with low precedence. Operator ( ) Exponentiation (^) * / % + - For example, the multiplication operator (*) has higher preference than the addition operator (+), so the expression 2 + 3 * 4 equals 14, not 20. You can override precedence rules by taking parts of an expression in parentheses as given in the example below. For example, (2+3)*4 would evaluate to 20. Associativity The associativity of an operator determines that how operators of the same precedence are grouped in the absence of parentheses. If an operand in an expression is both preceded and followed by (for example, 12 * 4 / 2) those operators have equal precedence, then which operations to be performed first is determined by the "associativity" of the operators. Operators may be: Left-associative (Operations are grouped from the left). Right-associative (Operations are grouped from the right) Operator Associativity () Left to Right Exponentiation (^) Right to Left * / % Left to Right + - Left to Right Let us go in more detail why to use prefix and postfix notation if we already have infix notation to evaluate expression? Let us see with the help of an example:- Now I am going to evaluate the value of this expression 3 * 4 + 8 / 2 * 2. As I know given expression is in infix notation and to calculate the value we need to know two thing:- Operator precedence and associativity of operator. Since * and / have highest precedence than +. So for simplicity I put parentheses as: 3 * 4 + 8 / 2 * 2 ( 3 * 4 ) + ( 8 / 2 * 2 ) it means addition is performed at the end. ( 3 * 4 ) + ( 8 / 2 * 2 ) But in sub-expression (8/2*2), / and * have equal precedence so ( 8 / 2 ) * 2 is Right or 8 / ( 2 * 2 ). This ambiguity is resolved by using associativity of operators. * / % have equal precedence and have associativity from right to left. So Operations are grouped from right to left. ( 8 / 2 * 2 ) 8 / ( 2 * 2 ) is Right instead of ( 8 / 2 ) * 2 So the final result is 3 * 4 + 8 / 2 * 2( 3 * 4 )+( 8 / 2 * 2 )( 3 * 4 ) + (8 / ( 2 * 2 ) )14 From above example we can see that to calculate the value of infix expressions we need to know operator precedence and associativity of operator. But how computer evaluate these expressions. Computer have no idea about precedence and associativity but as a human we know. Computer is intelligent and fast but not smart than human being. So human being have developed other two notation known as prefix and postfix. Which is equivalent to infix notation. So computer uses prefix and postfix notation internally to evaluate the expressions. Computer takes expression in the form of infix but ultimately it is converted into either postfix or prefix. That’s by prefix and postfix notation are used. So we need to study how to converted infix notation to prefix or postfix notation. This stuff is for competition exam. So I haven’t gone through in much detail. But I have covered all the stuff which useful for solving question quickly and understanding the concepts. Conversion Infix to Prefix There are total of 6 conversion. Infix to Postfix Conversion can be done using two methods: Using Stack. Prefix to Infix Using Expression tree. Prefix to Postfix Conversion by using Expression tree is quite easy and fast as compared to other one. Postfix to Infix We will do conversion using expression tree. Postfix to Infix Before discussing the conversion using expression tree, you must have idea that how to traverse the tree using in-order, pre-order and post-order traversal. If you already know the traversal of tree then you can skip, otherwise go ahead with me. But I suggest everyone to go through once, because this will add extra knowledge to your existing one. I have discussed tree traversal in much detail later on. Here I am just discussing the traversal of tree. So to do the conversion first we need to construct Expression Tree. Then from the expression tree we can get:- Infix Notation by doing In-Order traversal of expression tree. Prefix Notation by doing preorder traversal of expression tree. Postfix Notation by doing In-Order traversal of expression tree. We can construct expression tree for infix, prefix and postfix notation. So how to construct a expression tree from a given notation, I’ll discuss construction of expression tree later on, first let us discuss what is expression tree and how it look like. Expression Tree Expression tree is a special kind of binary tree in which: Each leaf node represents an operand. Each non-leaf node represents a single binary operator. The left and right sub-trees of an operator node represent sub-expressions that must be evaluated before applying the operator at the root of the sub-tree. Non-Leaf node + represents an Operator Leaf node represents - / an Operand 4 3 * 2 6 5 Infix Notation for above expression tree is given as:- 4 – 3 + 6 * 5 / 2 Operator deep in the expression tree are evaluated first so given expression is evaluated as: 4 – 3 + ( (6*5) / 2 ) = 1 + 15 = 16 So from above we can see that operator at lower level have high precedence and operator at higher level have low precedence. Operator having higher precedence are evaluated first. Important Level in expression tree Indicate Precedence. The levels of the nodes in the tree indicate their relative precedence of evaluation. Parentheses are not required to indicate precedence. Operations at higher levels of the tree are evaluated later than those below them. The operation at the root is always performed last. In-Order Traversal In-order Traversal ( L N R ) Traverse the left Sub-tree (Recursively) Visit the root Node Traverse the right Sub-tree (Recursively) Example 1 L (Left Sub-tree) N (Root) R (Right Sub-tree) A B C So In-Order traversal is: B A C L (Left Sub-Tree) N (Root) R (Right Sub-Tree) A Recursively Recursively B C L N R L N R D E F G And so on…. So In-Order Traversal is : D B E A F C G Example 1 Find in-Order traversal of tree given below. + - H / * + * E - A B C D F G In-order Traversal is: A + B / C * D – E * F – G + H Example 2: Example 3: A A C B C B D E D E F F G G H I J In-order Traversal: D B G E A C H J F I In-order Traversal: D F B A C G E Pre-order Traversal:- Pre-order Traversal (N L R) First Visit the Root Traverse the left sub-tree (Recursively) Traverse the right sub-tree (Recursively) Example N (Root) L (Left Sub-tree) R (Right Sub-tree) A B C So Pre-Oder traversal is: A B C N (Root) L (Left Sub-tree) R (Right Sub-tree) A Recursively Recursively B C N L R N L R D E F G And so on…. So Pre-order traversal is: A B D E C F G Example 1 Find Preorder traversal of tree given below. + - H / * + * E - A B C D F G Preorder Traversal is: + - / + A B * C D * E – F G H Example 2: Example 3: A A B C B C D E F E D G H I F G J Preorder Traversal: A B D E G C F H J I Preorder Traversal: D F B A C G E Post-order Traversal: Post order Traversal ( L R N ) Traverse the Left sub-tree (Recursively) Traverse the Right sub-tree (Recursively) Visit the Root Example L (Left Sub-tree) R (Right Sub-tree) N (Root) A B C So Post-order traversal is: B C A L (Left Sub-tree) R (Right Sub-tree) N (Root) A Recursively Recursively B C L R N L R N D E F G And so on….