CSE 111, Fall 2000

Great Ideas in Computer Science

Lecture Notes #12

PROGRAMMING IN PASCAL:
DECISION TREES

1.  Decision Trees

*    Decision tree = an algorithm for soloving a
                            problem by asking a sequence
                            of questions, & making decisions
                            (i.e., choices)

e.g.)    Course advisor DT:

            Computer asks you questions in order to
            advise you on what courses to take:
            e.g.) suppose choice between
                    CSE 101, 111, 113, 115

        Your question to computer:
            "Should I take 101, 111, 113, or 115?"

        Computer's questions to you, and your
        answers, and computer's further questions,
        and your further answers, etc., make up
        the DT.

        --    Click here for image of tree in "postscript".
        --    Click here for image of tree in "PDF".

*    DTs are called "trees", since they sort of look
    like trees if you look at them upside down.
        --    The first box (at the top) is called the "root".
        --    Each "yes/no" line is called a "branch".
        --    And the suggestion boxes ("Take 115") are
                called "leaves"
 

2.  DTs for playing games

*    Can also use DTs to have computer play a game
    with you.

    --    instead of questions/answers,
            there are moves/countermoves

i.e.)    DT is also an algorithm for playing a game
        i.e.)    for solving the problem:  what move
                should I make next?

e.g.)    5-NIM:

        Playing board:  row of 5 boxes
        (you can also play 1-NIM, 2-NIM, ..., 1000-NIM)

 ___ ___ ___ ___ ___
|   |   |   |   |   |
|___|___|___|___|___|
        Rules:
            Player 1:  puts 1 or 2 Xs at left end
            Player 2:  puts 1 or 2 Os in next boxes
            etc., until no more moves can be made;
            First player to mark last box wins.

        e.g.)

 ___ ___ ___ ___ ___
|   |   |   |   |   |
| X | X | O | O | X |
|___|___|___|___|___|
                        Player 1 wins!

        Possible DT for 5-NIM:
 
 

                computer says:
                --------------
                | Your move. |
                --------------
                     /\
   human's move:   X/  \XX
                   /    \
                  /      \
                 /        \
                /          \
               /            \
       -----------         -----------
       |I play O;|         |I play O;|
       |your move|         |your move|
       -----------         -----------
           /\                     /\                
          /  \                   /  \
        X/    \XX              X/    \XX
        /      \               /      \
       /        \             /        \
------------ ------------  -----------  ----------
|I play OO;| |I play 00;|  |I play O;|  |You win!|
| I win!   | | I win!   |  | I win!  |  ----------
------------ ------------  -----------
Issues:
    1-    This isn't complete!
            Player 2 (computer) can make OO moves,
            which I haven't shown.

            *    one way to handle this:
                    figure out computer's best strategy;
                    then put that in the DT

    2-    How to get computer to actually play Nim:

            *    in general, how to get computer to
                 run a DT program
 

3.  Programming in Pascal

a)    How to input & output data (information)

        *    DT program has to be able to print
            questions

b)    Sample Pascal program:


        program printdata;
        begin
          writeln('This is a');
          writeln('test program')
        end.

       *    same program, with comments:


program printdata;
      {"program" is a reserved word       }
      {like Karel's "beginning-of-program"}

{Name:  Bill Rapaport               }
{Date:  2 Oct 00                    }
{Anything within braces is a comment}

begin {a reserved word indicating start}
      {of a sequence of instructions;  }
      {here, it's like Karel's         }
      {"beginning-of-execution"        }

  writeln('This is a');
      {semicolons separate instructions}
  writeln('test program')
  {"This is a" & "test program" are data}

end.
   {"end" is a reserved word indicating}
   {the end of a sequence; here, it's like}
   {Karel's "end-of-execution"}

   {The period ends the program;}
   {it's like Karel's "end-of-program"}


You can type either of these into a Unix file,
compile them, and execute them.

The method I showed in lecture was this
(you'll get more detail in HW #5,
including a fix you'll have to make to your
".cshrc" file before you can access the Pascal
compiler)

    1)    Use pico to create a file containing your
            program
    2)    Save the program & exit pico
    3)    Use the "pc" command to compile the
            program
    4)    Execute the "a.out" file that the compiler
            creates.


Copyright © 2000 by William J. Rapaport (rapaport@cse.buffalo.edu)

file: 111F00/lecturenotes12.02oc00.html