Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Yes. Think of how a RPN calculator works. Now, instead of calculating the value, instead you add the operation to the tree. So, for example, <code>2 3 4 + *</code>, when you get to the +, then rather than putting 7 on the stack, you put <code>(+ 3 4)</code> on the stack. And similarly when you get to the * (your stack will look like <code>2 (+ 3 4) *</code> at that stage), it becomes <code>(* 2 (+ 3 4))</code>.</p> <p>This is prefix notation, which you then have to convert to infix. Traverse the tree left-to-right, depth first. For each "inner level", if the precedence of the operator is lower, you will have to place the operation in brackets. Here, then, you will say, <code>2 * (3 + 4)</code>, because the + has lower precedence than *.</p> <p>Hope this helps!</p> <p>Edit: There's a subtlety (apart from not considering unary operations in the above): I assumed left-associative operators. For right-associative (e.g., <code>**</code>), then you get different results for <code>2 3 4 ** **</code> ⇒ <code>(** 2 (** 3 4))</code> versus <code>2 3 ** 4 **</code> ⇒ <code>(** (** 2 3) 4)</code>.</p> <p>When reconstructing infix from the tree, both cases show that the precedence doesn't require bracketing, but in reality the latter case needs to be bracketed (<code>(2 ** 3) ** 4</code>). So, for right-associative operators, the left-hand branch needs to be higher-precedence (instead of higher-or-equal) to avoid bracketing.</p> <p>Also, further thoughts are that you need brackets for the right-hand branch of <code>-</code> and <code>/</code> operators too.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload