taocp

This site has no text of the book. Keep your own copy of TAOCP next to it. See what is checked.

Exercise · Volume 1 · 2.3-16

Exercise 2.3-16

not stated yet in Lean solution written

Rating20
KindK8, compute by machine: find a number or an object by a large computation from the written solution
SectionSection 2.3 of Volume 1

The task

Draw trees like Fig. 21 for (a) 2(a−b/c)2(a - b/c) and (b) a+b+5ca + b + 5c.

The exact wording is in your copy of the book.

Hints

Open one hint at a time. Try the exercise again after each.

Hint 1

Put the operator that is applied last at the root, and the operands as children. Division and multiplication bind before subtraction and addition.

Solution

Show the solution

Each interior node is an operator. Its children are its operands in the order of the formula.

(a) The last operation is the product of 22 and (a−b/c)(a - b/c). The factor (a−b/c)(a - b/c) is a difference of aa and the quotient b/cb/c.

        x
       / \
      2   -
         / \
        a   /
           / \
          b   c

(b) The sum a+b+5ca + b + 5c has three terms. Read from the left, it is (a+b)+5c(a + b) + 5c.

        +
       / \
      +   x
     / \ / \
    a  b 5  c

If a sum of three terms is allowed to be one node of degree 3, the tree is the root ++ with the three children aa, bb and ×\times with children 55 and cc. Addition is associative, so a different grouping, a+(b+5c)a + (b + 5c), gives a third tree.

Check

Python3 parsed both formulas with its own parser and printed the structure as nested tuples. For (a) it printed ('Mult', 2, ('Sub', 'a', ('Div', 'b', 'c'))). For (b) it printed ('Add', ('Add', 'a', 'b'), ('Mult', 5, 'c')). Both agree with the pictures.

Remarks

A formula defines a tree only after the rules of precedence and association are fixed. In (b) the rule that addition is taken from the left gives one tree out of three.

This solution is drafted, and a person has not reviewed it yet. The source.

In Lean

not stated yet The exercise is not stated in Lean yet.

The status is computed from the Lean build. The node of the map.

Something is wrong? Report it.