Transformation from prefixed record to record
-
How to convert the prefix (Policy Note) into an infixed record. The record can contain: '(', ')', '+', '-', '*', '', '0..9', 'a..z'. What data structures should be used?
-
An obvious honest algorithm is to dissolve the Polish record in a tree. Having a tree, it's possible to build any form of recording (and to optimize, compil and perform anything).
For the record.
− 5 * 6 7
You are taking the following steps:
- You see the minus, you're setting up a binary site with two Operandas.
- Read the first Operand.
- See?
5
, that's the first Operand, you're putting in an allotment for him.
- See?
- Read the second Operand.
- See?
*
, you're doing a binary surgery for him with twoandas. - Read the first Operand.
- See?
6
♪ Allocate a leaf knot with a constant ♪ - See?
7
♪ Allocate a leaf knot with a constant ♪
- See?
- See?
For a specific purpose, the result can be simplified and not the tree built, but the data must be released immediately. Algorithm will be like this:
- Read one token.
- If it's a constant, it's a result.
- If it's a binary operation, remembrance her type, get the meaning of the operars, put it in brackets to avoid thinking about the priority of operators.
- supplement the rules for other types of nodes