【问题标题】:infix to prefix time and space complexity前缀时间和空间复杂度的中缀
【发布时间】:2012-11-09 05:08:15
【问题描述】:

我一直在编写一个 Java 程序,使用操作数堆栈和运算符堆栈从中缀表示法转换为前缀表示法。我已经根据此处最佳答案中的伪代码实现了一个工作转换器:

conversion from infix to prefix

但是,我现在正在尝试计算上述算法的时间和空间复杂度。

我认为空间复杂度必须是 O(n),因为我们只有两个堆栈来存储它们之间共享的输入。

考虑到时间复杂度,我不完全确定,是不是因为必须将每个子部分从中缀转换为前缀,所以是 O(n^2)?我不太确定这部分。

基本上我的问题是:我的空间复杂度结果是否正确,算法的时间复杂度是多少?

非常感谢!

编辑: 这是算法的伪代码:

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

【问题讨论】:

  • 贴出你的代码或伪代码
  • 好的,我已经添加了我的伪代码。实际代码要长得多,而且不容易理解。
  • 不!运行时间为 O(N)。参考这个stackoverflow.com/questions/5305215/…

标签: complexity-theory time-complexity prefix infix-notation space-complexity


【解决方案1】:

是的,O(n^2) 看起来是正确的 - 因为本质上你有一个外部和一个内部 while 循环。

编辑:O(m *n) 其中 m

【讨论】:

  • 是的,绝对不是 O(n)。它是 O(n^2) 或 O(n log n)。我在想可能是 O(n log n) 因为我们不会在内部 while 循环中每次都遍历整个输入,但我并不完全确定。你怎么看?
  • 为什么是 lg (n) 。我同意它不是 n,而是一个子集,所以如果你想准确的话,你可以说 O(m*n)。其中 m
  • 哦,好的。不,我不确定。我只是有点困惑,因为我看到后缀的中缀是 O(n log n) 但可能是错误的:wiki.answers.com/Q/…
猜你喜欢
  • 2014-01-28
  • 2018-09-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-08-14
  • 2022-06-10
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多