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
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 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...
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 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
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 integer that is not positive. The values should be separated by single blank spaces. Declare any variables that are needed. (In C language please).
Write an algorithm to delete an element from a hash table that uses linear probing as...
Write an algorithm to delete an element from a hash table that uses linear probing as its clash resolution strategy. Analyze your algorithm and show the results using order notation?
Java Code using Stack Write a program that opens a text file and reads its contents...
Java Code using Stack Write a program that opens a text file and reads its contents into a stack of characters, it should read character by character (including space/line change) and push into stack one by one. The program should then pop the characters from the stack and save them in a second text file. The order of the characters saved in the second file should be the reverse of their order in the first file. Ex input file: Good...
Write a program which reads an input file. It should assume that all values in the...
Write a program which reads an input file. It should assume that all values in the input file are integers written in decimal. Your program should read all integers from the file and print their sum, maximum value, minimum value, and average. Use the FileClient class here (from a previous reading) as an example. You'll need to create a file to be used as input to test your program, as well. Your program should work whether the integers are separated...
(JAVA) Write an application that reads three nonzero values entered by the user and determines and...
(JAVA) Write an application that reads three nonzero values entered by the user and determines and prints whether they could represent the sides of a triangle. Enter three sizes, separated by spaces(decimals values are acceptable): 4.5·5.5·3.5 A triangle could measure 4.50, 5.50, by 3.50.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT