Professional Documents
Culture Documents
2
Balanced Parentheses
• Each “(”, “{”, or “[” must be paired with a
matching “)”, “}”, or “[”
– correct: ( )(( )){([( )])}
– correct: ((( )(( )){([( )])}
– incorrect: )(( )){([( )])}
– incorrect: ({[ ])}
– incorrect: (
3
Balanced parentheses in an Expression
Algorithm :Balanced Parentheses
• If the element is a starting bracket( ‘{‘, ‘[‘, ‘(‘ ), Push it to the
Stack.
• Similarly for closing bracket( ‘}’, ‘]’, ‘)’ ), pop an element
from he Stack.
• Compare the poped element with the closing bracket. While
the popped element is not the matching starting bracket, we
can conclude that the parentheses are not balanced.
• If the elements are not a starting or closing bracket, we can
assume that they are other characters of the expression. As a
result, we can skip that iteration using the continue statement.
• Finally, if the Stack is empty after the traversal, the
parentheses are said to be balanced else it is not balanced.
Reversing a Word
• We can use a stack to reverse the letters in a
word.
• Read each letter in the word and push it onto
the stack
• When you reach the end of the word, pop the
letters off the stack and print them out.
6
string WELCOME, then on reversing it would be
EMOCLEW.
Notations for Arithmetic Expression
There are three notations to represent an
arithmetic expression:
• Infix Notation
• Prefix Notation
• Postfix Notation
Infix Notation
Operators Symbols
Parenthesis ( ), {}, [ ]
Exponents ^
Multiplication and *, /
Division
Addition and +,-
Subtraction
Prefix Notation
• The prefix notation places the operator before the
operands.
• This notation was introduced by the Polish mathematician
and hence often referred to as polish notation.
Example:
• +AB
• -CD
1. AB+CDE/*-F+
2. ABCD^E-FGH*+^*+I-
3. ABC+*D/
4. A B + C –
5. A B C * +
6. A B + C D - /
7. A B + C D - * E + F G + /
8. KL+MN∗−OPW∗U/V/T∗+Q+
Evaluation of postfix expressions
Example
*
Computation of Postfix Expressions
WHILE more input items exist
Get an item
IF item is an operand
stack.Push(item)
ELSE
stack.Pop (operand2)
stack.Pop (operand1)
Compute result as(operand1 operator operand2)
stack.Push(result)
stack.Pop(result)
Algorithm for Evaluation of Postfix Expression
1. Add ’)’ to postfix expression.
2. Read postfix expression Left to Right until ) encountered
3. If operand is encountered, push it onto Stack
4. If operator is encountered, Pop two elements
1. A -> Top element
2. B-> Next to Top element
3. Evaluate Result= B operator A
4. Push Result onto Stack
5. Set answer = pop
6. END
expression :
7 8 2 * 4 /+
Expression: 456*+
Postfix expressions: Algorithm using
stacks
*
Evaluation of postfix exprssion: 2 * (4+3) - 5
Infix to postfix conversion
• Use a stack for processing operators (push and pop
operations).
• Scan the sequence of operators and operands from
left to right and perform one of the following:
• output the operand,
• push an operator of higher precedence,
• pop an operator and output, till the stack top contains
operator of a lower precedence and push the present
operator.
26
Infix to Postfix Conversion
Requires operator precedence information
28
Consider the infix expression exp = “a+b*c+d”
infix expression exp = “a+b*c+d”
infix expression exp = “a+b*c+d”
infix expression exp = “a+b*c+d”
infix expression exp = “a+b*c+d”
34
35
Expression: A * (B + C * D) + E becomes :A B C D * + * E +
int main() {
char exp[] = "a+b*(c^d-e)^(f+g*h)-i";
// Function call
infixToPostfix(exp);
return 0;