L3. Prefix, Infix, and Postfix Conversion | Stack and Queue Playlist

preview_player
Показать описание

Рекомендации по теме
Комментарии
Автор

At timestamp 12:20 and 13:12 it should be not operator !st.empty() in while condition

lingasaikiran
Автор

0:00 Introduction and brief explanation of post, pre and infix
4:50 Infix to postfix
16:39 Infix to prefix
27:51 postfix to infix
35:28 prefix to infix
39:58 postfix to prefix
45:07 prefix to postfix

sandhyaranijam
Автор

At 25:53 it would be st.push instead of st.pop

AdityaRaj-wuvg
Автор

49:52
Striver does not miss a thing! I thought of the same thing while watching the video because the whole concept of infix-prefix-postfix is based on remembering things. But it might come in college academic exams to write the code for any of these conversions.

dhrroovv
Автор

heaps and strings too please...waiting for it from such a long time😭😭

mansidubey
Автор

Bro can eat 1000 coding questions in breakfast 🔥🔥🔥

SagarGhate-on
Автор

In the Infix to prefix problem, while evaluating the postfix of the modified input exp,
To maintain right associativity of ^ operator:
If the incoming character is ^ and the stack top is also ^, you should push the incoming ^ to the stack.
If the incoming character is ^ and the stack top has an operator with greater precedence (which, in this context, there isn't any, as ^ has the highest precedence), you should pop the stack.
But according to striver's code, @25:32, if the input is ^ and stack top is either ^ or something with greater priority... in both cases we are popping.

Correct me if I'm wrong somewhere.

shashank_
Автор

we dont need to mug up this, there is simple logic behind this
for infix to postfix, infix to prefix we need to learn it

but for

postfix to any (eg--> ab+)
prefix to any (eg--> +ab)

we try to move in the direction from where we get the both the operands before their operator,
this helps us to move the operator where-ever we want like, to the start (+ab) for prefix
(ab+) for postfix and (a+b) for infix

example->postfix to prefix (ab+ to +ab)
we move from front in postfix to put all operand in stack, then if we get an operand we pick up last 2 ele from stack put paranthesis and operator and put to stack top (ex-- operator top 2 top1) then at last return top

example->prefix to infix (+ab to a+b)
we move from back in the infix to get all operands push to stack, then if we get an operand we pick last 2 ele from stack put in paranthesis and operator ex-(top1 operator top2) then at last return stack top


only thing is to remember here is for
infix to postfix or infix to prefix and
in above case what to push like first (top1 operator top2 ) or (top2 operator top1 ) for this we can use common sense

Kshitijsingh-nf
Автор

Thanks I was waiting willingly for this

deepanshudahiya
Автор

sir while converting from infix to postfix the test condition of while loop should be while(!st.empty() &&

shreyasrk
Автор

infix to postfix will give segmentation fault inunbalanced paranthesis as st.pop() is outside the condition of checking for !st.empty()

kushjain
Автор

thank you very much sir for helping us... sir please bring strings and heaps playlist also please sir

shrutigupta
Автор

Infix to postfix
class Solution {
// Function to convert an infix expression to a postfix expression.
static int precedance(Character c){
switch(c){
case '+':
case '-':
return 1;

case '*':
case '/':
return 2;

case '^':
return 3;
}
return -1;
}
public static String infixToPostfix(String exp) {
// Your code here
String ans="";
Stack<Character> st = new Stack<>();

for(int i=0;i<exp.length();i++){
char c = exp.charAt(i);


ans+=c;
}
else if(c=='('){
st.push(c);
}
else if(c==')'){
while(!st.isEmpty() && st.peek()!='('){
ans+=st.pop();
}
st.pop(); // pop the '(' bracket
}
else{
while(!st.isEmpty() &&
ans+=st.pop();
}
st.push(c);
}
}
while(!st.isEmpty()){
ans+=st.pop();
}
return ans;
}
}

Prefix to Infix
class Solution {
static String preToInfix(String pre_exp) {
// code here
Stack<String> st = new Stack<>();
int i=pre_exp.length()-1;
while(i>=0){
char ch = pre_exp.charAt(i);


}
else{
String t1 = st.peek();
st.pop();
String t2 = st.peek();
st.pop();
String str = "("+t1+ch+t2+")";
st.push(str);
}
i--;
}
return st.peek();
}
}

Prefix to Postfix
class Solution {
static String preToPost(String pre_exp) {
// code here
Stack<String> st = new Stack<>();
int i=pre_exp.length()-1;
while(i>=0){
char ch = pre_exp.charAt(i);


}
else{
String t1 = st.peek();
st.pop();
String t2 = st.peek();
st.pop();
String str = t1+t2+ch;
st.push(str);
}
i--;
}
return st.peek();
}
}

Postfix to Prefix
class Solution {
static String postToPre(String post_exp) {
// code here
Stack<String> st = new Stack<>();
int i=0;
int n = post_exp.length();
while(i<n){
char ch = post_exp.charAt(i);


}
else{
String t1 = st.peek();
st.pop();
String t2 = st.peek();
st.pop();
String str = ch+t2+t1;
st.push(str);
}
i++;
}
return st.peek();
}
}

Postfix to Infix
class Solution {
static String postToInfix(String exp) {
// code here
Stack<String> st = new Stack<>();
int i=0;
int n = exp.length();
while(i<n){
char ch = exp.charAt(i);


}
else{
String t1 = st.peek();
st.pop();
String t2 = st.peek();
st.pop();
String str = "("+t2+ch+t1+")";
st.push(str);
}
i++;
}
return st.peek();
}
}

radhepatel
Автор

In infix to postfix he maintains left associativity of ^ by using priority(s[i]) <= priority(st.top())

Whereas in infix to prefix he maintains right associativity of ^ ?

Am I correct?

valendradangi
Автор

I think we have to check associativity of the operator after checking precedence before pushing in to the stack.

Chinniveeravalli
Автор

Thank youu sir, love the way you teach

arushahabib
Автор

At 12:45 it should be !st.empty() instead of st.empty()

darkwarrior
Автор

at 10:48 it should be a and last for small z for second case

RAVIKUMAR-ueot
Автор

Little confusing Infix to Prefix conversion but the rest is crystal clear

aryasharma
Автор

Utkarsh....you can keep those retakes....shows his dedication :)

ydtcod