unique_ptr Expression Tree from Postfix
hardQuestion 718Build an expression tree from a postfix expression in which EVERY node is owned by a std::unique_ptr. Write no new and no delete of your own anywhere in this program.
Input (stdin): one postfix expression, tokens separated by whitespace — each a non-negative integer or one of +, -, *. Read tokens until the input runs out.
Use the classic stack method: a number becomes a leaf; an operator pops the top two subtrees (the FIRST one popped is the RIGHT operand) and becomes a node with those two children. When the input ends, the one tree left on the stack is the root.
Then print exactly two lines. For the input 2 3 + 4 * the output is exactly what is shown in Expected Output.
Line 1 is Infix: followed by the fully parenthesized infix form: a leaf prints just its number; an operator node prints (, the left side, a space, the operator, a space, the right side, then ). Line 2 is Value: followed by the integer the expression evaluates to.
This question checks for memory leaks. Include <memory>.
Write everything yourself, including main.
Expected output
Infix: ((2 + 3) * 4) Value: 20
= newline. The output ends with a newline, so print one after the last line.
Your output
Run your code to see the output here...