Homework Assignment 2
Assignment
-
(50 points) Give a recursive method for removing all the elements from a
stack.
- Provide and explain pseudocode
- Statement and proof of time complexity. Be clear of the assumptions on
the ADT implementations.
- Statement and proof of memory complexity. Be clear of the assumptions on
the ADT implementations.
-
(50 points) Postfix notation is an unambiguous way of writing an arithmetic
expression without parenthesis. It is defined so that if
(exp1) op
(exp2) is a normal fully parenthesized expression whose
operation is op, the postfix notation
is pexp1 pexp2
op, where pexp1
is the postfix version of exp1 and
pexp2 is the postfix version of
exp2. So, for example, the postfix version of
((5+2)*(8-3))/4 is 5 2 + 8 3 - * 4 /. Describe a
nonrecursive way of evaluating an expression in postfix notation.
- Provide and explain pseudocode
- Statement and proof of time complexity. Be clear of the assumptions on
the ADT implementations.
- Statement and proof of memory complexity. Be clear of the assumptions on
the ADT implementations.
-
Bonus.
(10 points) Suppose you have a stack
S containing
n elements and a queue Q that is initially empty.
Describe how you can use Q to scan S to see if it
contains a certain element x, with the additional constraint
that your algorithm must return the elements back to S in their
original order. You may only use S, Q, and a
constant number of other primitive variables.
- Provide and explain pseudocode
- Statement and proof of time complexity. Be clear of the assumptions on
the ADT implementations.
- Statement and proof of memory complexity. Be clear of the assumptions on
the ADT implementations.