0% found this document useful (0 votes)
23 views7 pages

Ass 2

Uploaded by

Taimour Afridi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views7 pages

Ass 2

Uploaded by

Taimour Afridi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Shaheed Zulfikar Ali Bhutto Institute of Science & Technology University

DEPARTMENT OF ROBOTICS & ARTIFICIAL INTELLIGENCE

Total Marks: 04

Obtained Marks:

Data Structures and


Algorithms
Assignment # 02
Last Date of Submission: 30th October 2024

Submitted To: Mr. Saad Irfan Khan

Student Name: Muhammad Taimour

Registration Number: 23108226

DSA BS(AI)-3C SZABIST-ISB


Shaheed Zulfikar Ali Bhutto Institute of Science & Technology University

DEPARTMENT OF ROBOTICS & ARTIFICIAL INTELLIGENCE


Instructions: Copied or shown assignments will be marked zero. Late submissions are not
entertained in any case.

CLO 2 – C4 – PLO A

Q1: (2 Marks)
Convert the following Infix expression into a Prefix expression using Stack:
 A + B * (C + D) / F + D * E
 (a + b – c) * (e / f) – (g – h/i)

Q2: (2 Marks)
Emma, a developer, is working on a compiler that checks for balanced expressions in code. Her
task requires analyzing each line to ensure that every opening bracket—whether parentheses,
curly braces, or square brackets—has a corresponding closing bracket in the correct order.
Emma needs to carefully go through each expression step-by-step, verifying that it is balanced to
avoid syntax errors during compilation. Your task is to help her by showing each step to confirm
the balance of brackets in a given expression when using a stack.
 ((()]))

DSA BS(AI)-3C SZABIST-ISB


Shaheed Zulfikar Ali Bhutto Institute of Science & Technology University

Question 1:

1. Expression: A + B * (C + D) / F + D * E
Step 1: Reverse the Infix Expression
Reversed: E * D + F / )D + C ( * B + A
Step 2: Replace Parentheses
Modified: E * D + F / (D + C) * B + A
Step 3: Convert to Postfix
Output: ``
Stack: []
Processing:
E → Output: E
* → Stack: [*]
D → Output: E D
+ → Pop *, Output: E D * Stack: [+], Push +.
F → Output: E D * F
/ → Stack: [+, /]
( → Stack: [+, /, (]
D → Output: E D * F D
+ → Stack: [+, /, (, +]
C → Output: E D * F D C
) → Pop +, Output: E D * F D C +, Stack: [+, /]
Pop /, Output: E D * F D C + /, Stack: [+]
Pop +, Output: E D * F D C + / +, Stack: []
B → Output: E D * F D C + / + B
+ → Stack: [+]
A → Output: E D * F D C + / + B A
Pop +, Output: E D * F D C + / + B A +
Postfix: E D * F / C + B + A +
Step 4: Reverse Postfix
Prefix: + + A B / * D C F E

2. Expression: (a + b - c) * (e / f) - (g - h / i)
Step 1: Reverse the Infix Expression
Reversed: )i / h - g ( - )f / e ( * )c - b + a (
Step 2: Replace Parentheses
Modified: (i / h - g) - (f / e) * (c - (b + a))
DSA BS(AI)-3C SZABIST-ISB
Shaheed Zulfikar Ali Bhutto Institute of Science & Technology University

Step 3: Convert to Postfix


Output: ``
Stack: []
Processing:
i → Output: i
/ → Stack: [/]
h → Output: i h
- → Pop /, Output: i h /, Stack: [-]
g → Output: i h / g
) → Pop -, Output: i h / g -, Stack: []
- → Stack: [-]
( → Stack: [-, (]
f → Output: i h / g - f
/ → Stack: [-, (, /]
e → Output: i h / g - f e
) → Pop /, Output: i h / g - f e /, Stack: [-]
* → Stack: [-, *]
( → Stack: [-, *, (]
c → Output: i h / g - f e / c
- → Stack: [-, *, (, -]
( → Stack: [-, *, -, (]
b → Output: i h / g - f e / c b
+ → Stack: [-, *, -, (, +]
a → Output: i h / g - f e / c b a
) → Pop +, Output: i h / g - f e / c b a +, Stack: [-, *]
Pop -, Output: i h / g - f e / c b a + -, Stack: []
Pop *, Output: i h / g - f e / c b a + - *
Postfix: i h / g - f e / c b a + - *
Step 4: Reverse Postfix
Prefix: - * - + a b c / e f - g h / i

DSA BS(AI)-3C SZABIST-ISB


Shaheed Zulfikar Ali Bhutto Institute of Science & Technology University

Question 2:

To check if the expression ((())]) contains balanced brackets using a stack, we will follow these steps:
Create a Stack: This will help us track opening brackets as we encounter them.
Iterate Through Each Character: For every character in the expression:
 If it is an opening bracket (i.e., (, {, or [), push it onto the stack.
 If it is a closing bracket (i.e., ), }, or ]), check the stack. If it’s not empty and the top element
of the stack is the matching opening bracket, pop the stack. If they don’t match, the
expression is unbalanced.
Final Evaluation: After processing all characters, if the stack is empty, the brackets are balanced; if not,
they are unbalanced.
Step-by-Step Analysis of the Expression ((())]):
1. Start with an Empty Stack:
Stack: []
2. Examine Each Character:
First Character: (
 It's an opening bracket.
 Stack: ['(']
Second Character: (
 It's also an opening bracket.
 Stack: ['(', '(']
Third Character: (
 This is yet another opening bracket.
 Stack: ['(', '(', '(']
Fourth Character: )
 This is a closing bracket.
 Check the stack:
o The stack isn’t empty.
o The top item is (, which corresponds correctly to the closing bracket ).
 Pop the stack.
 Stack: ['(', '(']
Fifth Character: )
 Another closing bracket.
 Check the stack:
 The stack still isn’t empty.
o The top item is (, which matches ).
DSA BS(AI)-3C SZABIST-ISB
Shaheed Zulfikar Ali Bhutto Institute of Science & Technology University

o Pop the stack.


 Stack: ['(']
Sixth Character: ]
 This is a closing bracket.
 Check the stack:
o The stack isn’t empty.
o The top of the stack is (, which does not match ] (it should match with [).
 This mismatch indicates that the expression is unbalanced.
Conclusion:
The encounter of the closing bracket ] with the top of the stack being ( shows that the
brackets in the expression ((())]) are not balanced.
Final State:
Final Stack: ['('] (indicating an unmatched opening bracket remains)
Result: The expression is not balanced.

#include <iostream>
#include <stack>
using namespace std;
bool isBalanced(const string& expression) {
stack<char> stack;
for (char ch : expression) {
if (ch == '(' || ch == '{' || ch == '[') {
stack.push(ch);
} else if (ch == ')' || ch == '}' || ch == ']') {
if (stack.empty()) return false;
char top = stack.top();
if ((ch == ')' && top != '(') || (ch == '}' && top != '{') || (ch == ']' && top != '[')) {
return false;
}
stack.pop();
}
}
return stack.empty();
}
int main() {
string expression = "((())])";
if (isBalanced(expression)) {
cout << "The expression '" << expression << "' is balanced." << endl;
} else {
cout << "The expression '" << expression << "' is not balanced." << endl;
}
return 0;
}

DSA BS(AI)-3C SZABIST-ISB


Shaheed Zulfikar Ali Bhutto Institute of Science & Technology University

DSA BS(AI)-3C SZABIST-ISB

You might also like