Postfix Expressions
From charlesreid1
About
Postfix expressions:
- expressions in which the operation being specified occurs after the two operands, in a nested way
Example: each postfix expression evaluates to 9.
5 4 +
2 7 * 4 1 + -
1 1 + 1 1 + + 1 1 + 1 1 + + +
Stacks
Postfix expressions can be evaluated by pushing the expressions onto a stack, where the stack deals with expressions. Expressions can consist of a single node (a number), or two expressions and an operator (making the expression definition recursive - like a tree).
In terms of stacks, we can push digits onto the stack UNTIL we reach a symbol, then apply the symbol to the next two expressions on the stack, turn the result into an expression, and push the result expression onto the stack.
Trees
To represent a postfix expression with an expression tree, we can use a binary tree - particularly, we have to have a proper binary tree. (Each node can have zero or two children.)
We look at the whole expression one piece at a time, pushing the pieces onto the stack, until we reach an operator, whereupon we pop two elements off the stack, and apply the operator to them. The result becomes a new element, and gets pushed onto the stack.
Flags
| Computer Science notes on computer science topics on the wiki, for educational and learning purposes
Part of the 2017 CS Study Plan.
Python/Exceptions · Python/Assertions · Python/Decorators Python/Os (os module) · Python/Strings Python/Splat · Python/Iterators · Python/Generators Python/Comparators · Python/Lambdas
Builtin features of Java: Java/Exceptions · Java/Assertions · Java/Memory · Java/Interfaces Java/Generics · Java/Decorators · Java/Diamond Notation Java/Iterators · Java/Iterable · Iterators vs Iterable Java/Comparators · Java/Comparable · Comparators vs Comparable Java/Numeric · Java/TypeChecking · Java/Testing · Java/Timing · Java/Profiling Documentation: Javadocs · Java/Documentation Tools and functionality: Java/URLs · Java/CSV External libraries: Guava · Fastutil · Eclipse Collections OOP: OOP Checklist · Java/Abstract Class · Java/Encapsulation · Java/Generics
|
See also: