COSC 1030 Introduction to Computer Science II
Assignment 3: Recursion
Solution

To print this page, print the following version: This Page (2up Postscript)


4) Finding the Connection Between Recursive Pictures and Recursive Programs.


6) For the complete code, see ass4.


7) Formal Proof of Correctness

The following is an outline of a proof of correctness for a recursive program. Fill in the missing details required to prove the correctness of your program EvalRec.

For every integer k >=0, let S(k) be the formal statement that "The routine EvalRec returns the correct value for every value of x, y, and z and for every polynomial (pointed to by n) that has k nodes in it."

The overall goal is to prove that EvalRec returns the correct answer on every input. It is sufficient to prove that "forall k>=0 S(k)".

The method is proof by Strong Induction:
       i.e. prove that for every k>=0, [S(0) and ... and S(k-1)] => S(k)
       Note for k=0, the assumption is empty and you must prove S(0).

Let k be an arbitrary integer >= 0. Assume by way of strong induction that [S(0) and ... and S(k-1)].

Because we assumed [S(0) and ... and S(k-1)] and then proved S(k), it follows that [S(0) and ... and S(k-1)] => S(k). Because we did this for an arbitrary value of k>=0, it must be true for all values of k>=0.

By the method of Strong Induction, we can conclude that "forall k>=0 S(k)", i.e. EvalRec works.


8) Tracing the execution of the recursive program PrettyPut

       PrettyPut  ________________________________
                 | no parameters                 |
                 | puts the polynomial pointed   |
                 | to by poly                    |
                 |_______________________________|
                                |
       PPutRec   _______________V________________
                 |dir = root                    |
                 |prefix =                       |
                 | ""                            |
                 |print:                         |
                 |       |-- 1                   |
                 | -- * -|                       |
                 |       |           |-- 2       |
                 |       |     |- * -|           |
                 |       |     |     |     |-- 3 |
                 |       |     |     |- + -|     |
                 |       |     |           |-- 4 |
                 |       |- + -|                 |
                 |             |-- 5             |
                 |_______________________________|
                     |                      |
_____________________V___________ __________V______________________ 
|dir = left                     | |dir = right                    |
|prefix =                       | |prefix =                       | 
| bbbbbb                        | | bbbbbb                        | 
|print:                         | |print:                         |
|       |-- 1                   | |       |           |-- 2       |
|_______________________________| |       |     |- * -|           |
                                  |       |     |     |     |-- 3 |
                                  |       |     |     |- + -|     |
                                  |       |     |           |-- 4 |
                                  |       |- + -|                 |
                                  |             |-- 5             |
                                  |_______________________________|
                                      |                       |
                             _________|                       |
_____________________________V___ ____________________________V____
|dir = left                     | |dir = right                    |
|prefix =                       | |prefix =                       |
| bbbbbb|bbbbb                  | | bbbbbbbbbbbb                  |
|print:                         | |print:                         |
|       |           |-- 2       | |             |-- 5             |
|       |     |- * -|           | |_______________________________|
|       |     |     |     |-- 3 |                                 
|       |     |     |- + -|     |
|       |     |           |-- 4 |
|_______________________________|
    |                       |
    |                       |__________
____V____________________________ ____V____________________________
|dir = left                     | |dir = right                    |
|prefix =                       | |prefix =                       |
| bbbbbb|bbbbbbbbbbb            | | bbbbbb|bbbbb|bbbbb            |
|print:                         | |print:                         |
|       |           |-- 2       | |       |     |     |     |-- 3 |
|_______________________________| |       |     |     |- + -|     |
                                  |       |     |           |-- 4 |
                                  |_______________________________|
                                      |                       |
                             _________|                       |
_____________________________V___ ____________________________V____
|dir = left                     | |dir = right                    |
|prefix =                       | |prefix =                       |
| bbbbbb|bbbbb|bbbbb|bbbbb      | | bbbbbb|bbbbb|bbbbbbbbbbb      |
|print:                         | |print:                         |
|       |     |     |     |-- 3 | |       |     |           |-- 4 |
|_______________________________| |_______________________________|