Question

In: Computer Science

Write an algorithm that uses a stack, reads from a stream of objects (operations and values)...

Write an algorithm that uses a stack, reads from a stream of objects (operations and values) in prefix order, and prints the result of that evaluation.

Trace 2 examples to demonstrate that your algorithm is valid.

Analyze your algorithm with respect to n (the number of objects in the stream)

Solutions

Expert Solution

  • Below is th detailed explanation of the the working of algorithm to evaluate a prefix expression using stack.
  • Evaluating prefix expression using stack.
  • Algorithm:

Evaluate(expression_string):

1. Start a pointer from end of expression_string and start traversing the expression_string till start of the string.

2. If pointer points to character which is operand then push it into stack.

3. If pointer points to character which is operator then pop out two elements from stack and apply the operator on the two operands popped out and push the value which comes back to the stack.

4. Now move backwards i.e, decrement the pointer which points to current character , now points to previous character and repeat from step 2.

5. At the end i.e, after traversing through all characters and evaluating and performing operations we have the answer of the prefix expression on the top of the stack so print it.

  • Example 1: +3*52

So pointer currently points to end of the string i.e, to character '2', so following the algorithm if current character is a operand then push it into stack.

stack: [2]

Now decrease pointer by 1 to as we move backwards in the expression string, so pointer now points to '5' , adding it to stack again as it is a operand.

stack: [2 5]

Now pointer points to '*' which is an operator so following step 3 of algorithm we pop two elements from stack and perform this operation using this operator so value comes = 5 * 2= 10, and pushing it back to stack.

stack: [10]

Now pointer points to '3' (after decreasing it by 1) so following step 2 push it into stack.

stack: [10 3]

Now pointer points to '+' so following step 3 pop out two elements from stack and apply operation i.e, 3 + 10 =13 and push it back to stack.

stack: [13]

Now as we traversed the while string , following step 5 we get our answer in the top of the tack i.e, 13.

  • Example 2: -3*2+61

So pointer currently points(step 1) to end of the string i.e, to character '1', so following the algorithm if current character is a operand then push it into stack(step 2).

stack: [1]

Now decrease pointer by 1(step 4) to as we move backwards in the expression string, so pointer now points to '6' , adding it to stack again as it is a operand.

stack: [1 6]

Now pointer points to '+' which is an operator so following step 3 of algorithm we pop two elements from stack and perform this operation using this operator so value comes = 6 + 1= 7, and pushing it back to stack.

stack: [7]

Now pointer points to '2' (after decreasing it by 1) so following step 2 push it into stack.

stack: [7 2]

Now pointer points to '*' so following step 3 pop out two elements from stack and apply operation i.e, 2 * 7 =14 and push it back to stack.

stack: [14]

Now pointer points to '3' (after decreasing it by 1) so following step 2 push it into stack.

stack: [14 3]

Now pointer points to '-' so following step 3 pop out two elements from stack and apply operation i.e, 3 - 14 =-11 and push it back to stack.

stack: [-11]

Now as we traversed the while string , following step 5 we get our answer in the top of the tack i.e, -11.

  • Time complexity analysis:

So as we can see form the above algorithm and examples we just use one traversal of the string to get our evaluated answer where in each iteration we either add the operand directly to stack or we perform operation after popping two elements from stack and then add it back to stack, so these are constant time operation i.e O(1) as accessing stack top element is done in O(1) time so overall we get n+some constant time operation where n is number if objects in stream(or the length of the expression string).

So time complexity comes out to be O(n) i.e, linear.

So if you still have any doubt regarding this solution please feel free to ask it in the comment section below and if it is helpful then please upvote this solution, THANK YOU.


Related Solutions

List the primitive operations used in the RC4 stream cipher algorithm for A) Key Stream Generation...
List the primitive operations used in the RC4 stream cipher algorithm for A) Key Stream Generation B) Bit Stream Encryption
write an implementation of the ADT stack that uses a resizeable array to represent the stack...
write an implementation of the ADT stack that uses a resizeable array to represent the stack items. Anytime the stack becomes full, double the size of the array. Maintain the stack's top entry at the end of the array. Please use c++ for this question.
IN C++ Write a program that reads in int values from the user until they enter...
IN C++ Write a program that reads in int values from the user until they enter a negative number like -1. Once the user has finished entering numbers, print out the highest value they’ve entered, the lowest value they’ve entered, and the total number of numbers they’ve entered. The negative number they entered should not be taken as one of the values entered.
Write a program that opens the file: "Lab6A_Data", reads all the values from the file, and...
Write a program that opens the file: "Lab6A_Data", reads all the values from the file, and calculates the following: A) The number of values in the file B) The sum of all the values in the file (a running total) C) The average of all the values in the file. D) The minimum value. E) The maximum value. F) The range of the data set of values. G) The number of times the value: '357' occurrs in the file. Display...
Write a program that reads in a continuous stream of strings representing a line of CSV...
Write a program that reads in a continuous stream of strings representing a line of CSV data in the format "NAME,AGE,EMAIL,DOB". Implement a function check_csv which verifies that each input string matches this format by ensuring that: • There are 4 tokens in the string corresponding to the 4 properties above. • The second token MUST be a number. • The fourth token should be in the format MM-DD-YYYY (hint: tokenize this as well). The function should return 0 if...
solution using stack Reversing a word or a sentence: Write an algorithm that will display a...
solution using stack Reversing a word or a sentence: Write an algorithm that will display a given word in a reverse order. - e.g. "Data Structures" will be "serutcurtS ataD".
Write a java class program to convert from INFIX TO POSTFIX Using stack operations
Write a java class program to convert from INFIX TO POSTFIX Using stack operations
Write a program that reads three values from the keyboard representing, respectively, an investors name, an...
Write a program that reads three values from the keyboard representing, respectively, an investors name, an investment amount, and an interest rate (you will expect the user to enter a number such as .065 for the interest rate, representing a 6.5% interest rate) . Your program should calculate and output (in currency notation) the future value of the investment in 10, 2 20 an 30 years using the following formula:   Future value =investment(1+interest rate)year Example of a test run: Enter...
Write a pyhton program that reads a stream of bits, e.g. 11001, and calculates and prints...
Write a pyhton program that reads a stream of bits, e.g. 11001, and calculates and prints the decimal value of the binary number represented by the entered bits, i.e. 25 in this case.
Write a loop that reads positive integers from standard input, printing out those values that are...
Write a loop that reads positive integers from standard input, printing out those values that are greater than 100, and that terminates when it reads an integerthat is not positive. The values should be separated by single blank spaces. Declare any variables that are needed. PLEASE ANSWER IN C
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT