CSE143 Notes for Friday, 11/14/14

I began by discussing the next programming assignment. The idea is to construct a binary tree that has information about a number of different kinds of things. We use yes/no questions to distinguish between them.

Initially it constructs a tree with just one leaf node containing "computer":

                +---+      +------------+
    overallRoot | +-+-->   | "computer" |
                +---+      +------------+
In this particular program, leaf nodes contain the names of objects and branch nodes contain questions. Whenever we get to a leaf node, we have no choice left but to guess that particular thing. So if we use the tree above, we'd always start by asking whether the object happens to be computer. If the user says yes, then we've correctly guessed the object and we give the message:

        Great, I got it right!
When we exit the program, the current tree is written to a file called question.txt. We saw that with just this simple tree composed of one leaf node, the file looks like this:

        A:
        computer
Then we explored what happens when the user thinks of something other than a computer as their object. In that case, we expand our tree to incorporate the new kind of object as well. I asked the class for suggestions of what to think of and someone said "dog". To incorporate this into our tree, we need to replace the leaf node with a branch node that has a question and we want that node to have two leaves: our old object ("computer") and this new object. We start by asking the user what their object is. I said "dog." Then we ask the user for a question that distinguishes between their object and our object. We said, "Is it alive?"

The plan is to replace our old leaf node with a branch node containing this question and with the old and new objects as leaves. But we don't know which one to put to the left and which one to put to the right. To figure that out, we have to ask the user what the answer is for their object. We said that the answer for "dog" is yes. I mention in the assignment writeup that we'll follow the convention that yes answers go to the left and no answers go to the right. So we replace the old root with this new tree:

                +---+    +---------------+
    overallRoot | +-+--> | "Is it alive? |
                +---+    +---------------+
                               /   \
                              /     \
                      +-------+     +------------+
                      | "dog" |     | "computer" |
                      +-------+     +------------+
When we exited the program, it wrote this information to question.txt:

        Q:
        Is it alive?
        A:
        dog
        A:
        computer
The information is stored using a preorder traversal of the tree. When I ran the program again, I told it to read back in this file. I asked people to think of another object and someone said "taco." So the program began by asking if our object is alive. I said no. So then it reached the leaf node with "computer" in it. Whenever the program reaches a leaf node, it has no choice but to make that guess. So it asked us whether our object is computer and we said no. So it asked for a question to distinguish the two. Someone said, "Can you eat it?". Then it asked what the answer is for "taco" and we said yes. So now the tree becomes:

                +---+    +---------------+
    overallRoot | +-+--> | "Is it alive? |
                +---+    +---------------+
                               /   \
                              /     \
                      +-------+     +-------------------+
                      | "dog" |     | "Can you eat it?" |
                      +-------+     +-------------------+
                                            /   \
                                           /     \
                                  +--------+     +------------+
                                  | "taco" |     | "computer" |
                                  +--------+     +------------+
This process continues as long as the user wants to keep guessing. When the program finishes executing, you write out the contents to question.txt using a preorder traversal. That way, if the user wants to, they can start the next time with this as the initial tree. That would allow you to grow this tree bigger and bigger each time the game is played.

We saw that after adding this second object, the program wrote the following to question.txt:

        Q:
        Is it alive?
        A:
        dog
        Q:
        Can you eat it?
        A:
        taco
        A:
        computer
I also pointed out that the zip file for the assignment includes a file called bigquestion.txt that has almost 10 thousand entries for animals. You have to rename the file to question.txt, but then your program should be able to read it in and play the game.

I spent the rest of the time discussing examples of new Java 8 features. The program is available on the calendar for today and contains comments that explain the different code snippets that we considered.


Stuart Reges
Last modified: Fri Nov 14 17:01:58 PST 2014