Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8807533
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T02:23:26+00:00 2026-06-14T02:23:26+00:00

I have been working on writing a Java program to convert from infix notation

  • 0

I have been working on writing a Java program to convert from infix notation to prefix notation using an operand stack and an operator stack. I have implemented a working converter based on the pseudocode in the top answer here:

conversion from infix to prefix

However I am now trying to work out the time and space complexity of the above algorithm.

I think that the space complexity must be O(n) because we just have two stacks that store the input shared between them.

Thinking about the time complexity, I’m not entirely sure, is it O(n^2) because of having to convert each subpart from infix to prefix? I’m not really sure about this part.

Basically my question is: is my space complexity result correct, and what is the time complexity of the algorithm?

Many thanks!

EDIT:
This is the pseudocode for the algorithm:

Algorithm ConvertInfixtoPrefix

Purpose: Convert and infix expression into a prefix expression. Begin 
// Create operand and operator stacks as empty stacks. 
Create OperandStack
Create OperatorStack

// While input expression still remains, read and process the next token.

while( not an empty input expression ) read next token from the input expression

// Test if token is an operand or operator 
if ( token is an operand ) 
// Push operand onto the operand stack. 
    OperandStack.Push (token)
endif

// If it is a left parentheses or operator of higher precedence than the last, or the stack is empty,
else if ( token is '(' or OperatorStack.IsEmpty() or OperatorHierarchy(token) > OperatorHierarchy(OperatorStack.Top()) )
// push it to the operator stack
    OperatorStack.Push ( token )
endif

else if( token is ')' ) 
// Continue to pop operator and operand stacks, building 
// prefix expressions until left parentheses is found. 
// Each prefix expression is push back onto the operand 
// stack as either a left or right operand for the next operator. 
    while( OperatorStack.Top() not equal '(' ) 
        OperatorStack.Pop(operator) 
        OperandStack.Pop(RightOperand) 
        OperandStack.Pop(LeftOperand) 
        operand = operator + LeftOperand + RightOperand 
        OperandStack.Push(operand) 
    endwhile

// Pop the left parthenses from the operator stack. 
OperatorStack.Pop(operator)
endif

else if( operator hierarchy of token is less than or equal to hierarchy of top of the    operator stack )
// Continue to pop operator and operand stack, building prefix 
// expressions until the stack is empty or until an operator at 
// the top of the operator stack has a lower hierarchy than that 
// of the token. 
    while( !OperatorStack.IsEmpty() and OperatorHierarchy(token) lessThen Or Equal to OperatorHierarchy(OperatorStack.Top()) ) 
        OperatorStack.Pop(operator) 
        OperandStack.Pop(RightOperand) 
        OperandStack.Pop(LeftOperand) 
        operand = operator + LeftOperand + RightOperand 
        OperandStack.Push(operand)
    endwhile 
    // Push the lower precedence operator onto the stack 
    OperatorStack.Push(token)
endif
endwhile 
// If the stack is not empty, continue to pop operator and operand stacks building 
// prefix expressions until the operator stack is empty. 
while( !OperatorStack.IsEmpty() ) OperatorStack.Pop(operator) 
OperandStack.Pop(RightOperand) 
OperandStack.Pop(LeftOperand) 
operand = operator + LeftOperand + RightOperand

OperandStack.Push(operand) 
endwhile

 // Save the prefix expression at the top of the operand stack followed by popping // the      operand stack.

print OperandStack.Top()

OperandStack.Pop()

End
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T02:23:27+00:00Added an answer on June 14, 2026 at 2:23 am

    Yep, O(n^2) looks correct – because essentially you have an outer and an inner while loop.

    Edit: O(m *n) where m <= n, but still quadractic

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So I have been working on writing a script that takes in input from
I am having trouble writing a query. I have been working with UNION on
I have been working on a large java application. It is quite parallel, and
I have a small open GL application I'm writing in Java using JOGL. I'm
So I have been working on a 2 player Tic-Tac-Toe game in java that
I have been working in a business writing advanced software apps, and obviously im
I have been working on writing a library of code for my future projects.
So lately I have been working on writing a simple compiler to better understand
I'm new to C++ programming, but have been working in C and Java for
So i'm writing a serial transmision program, and have just changed over to using

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.