Workspace Infix To Postfix
0d
Back to questionsDSA Visualizers: 388 Coding Questions

Infix to Postfix visualizer

Learn Infix to Postfix with a step-by-step Stacks & Queues visualizer, JavaScript source code and a worked example. Practice operations and expressions.

INTERACTIVE EXECUTION STUDIO
LEARN BY DOING

Predict. Execute. Understand.

Follow the source code, inspect live state changes, and predict the next operation before revealing it.

1 / 8Execution step
Reference example · JavaScript
Inspect reference input
{
  "input": "a+b*c",
  "mode": "post"
}

Algorithm source

JavaScript reference implementation · highlighted trace statement
const solve = ({
input,mode
},s)=>{
let stack=[],o=[],rank={
"+":1,"-":1,"*":2,"/":2
};
for(let x of input){
if(/[a-z]/.test(x))o.push(x);
else if(x==="(")stack.push(x);
else if(x===")"){
while(stack.at(-1)!=="(")o.push(stack.pop());
stack.pop()
}else{
while(stack.length&&rank[stack.at(-1)]>=rank[x])o.push(stack.pop());
stack.push(x)
}
s("Read token "+x,{
Operators:stack,Postfix:o
})
}
while(stack.length){
o.push(stack.pop());
s("Flush operators",{
Operators:stack,Postfix:o
})
}
if(mode==="post")return o.join("");
let a=[];
for(let x of o){
if(/[a-z]/.test(x))a.push(x);
else{
let r=a.pop(),l=a.pop();
a.push(x+l+r)
}
s("Build prefix expression",{
Stack:a
})
}
return a[0]
};

Execution canvas

Active Changed
STEP 1

Read token a

Inspect the highlighted code and the state below. Click individual cells or nodes to see their values.

Operators0 items

Empty at this step.

Postfix1 items
Keyboard: Space to play · Left / Right to stepExplore next problem

Worked example and algorithm walkthrough

Topic: Stacks & Queues · Operations and expressions

Follow the computed execution on the reference input. Advance one step at a time, inspect the changing values and compare your prediction with the final output.

Example input

{
  "input": "a+b*c",
  "mode": "post"
}

Execution steps

  1. Read token a
  2. Read token +
  3. Read token b
  4. Read token *
  5. Read token c
  6. Flush operators

Computed output

"abc*+"
JavaScript reference implementation
const solve = ({
  input,mode
},s)=>{
  let stack=[],o=[],rank={
    "+":1,"-":1,"*":2,"/":2
  };
  for(let x of input){
    if(/[a-z]/.test(x))o.push(x);
    else if(x==="(")stack.push(x);
    else if(x===")"){
      while(stack.at(-1)!=="(")o.push(stack.pop());
      stack.pop()
    }else{
      while(stack.length&&rank[stack.at(-1)]>=rank[x])o.push(stack.pop());
      stack.push(x)
    }
    s("Read token "+x,{
      Operators:stack,Postfix:o
    })
  }
  while(stack.length){
    o.push(stack.pop());
    s("Flush operators",{
      Operators:stack,Postfix:o
    })
  }
  if(mode==="post")return o.join("");
  let a=[];
  for(let x of o){
    if(/[a-z]/.test(x))a.push(x);
    else{
      let r=a.pop(),l=a.pop();
      a.push(x+l+r)
    }
    s("Build prefix expression",{
      Stack:a
    })
  }
  return a[0]
};

Continue learning

Practice related DSA questions in the same topic.