CPSC 225 Intermediate Programming Spring 2025

Lab 8: Binary Trees

Due: Thu Apr 17 at the start of lab

Introduction

The purpose of this lab is to practice working with binary trees.

About Due Dates

Labs are due at the start of lab on the date listed. Note: as the due date is an exam day, make sure you hand in your files before coming to lab, or arrive early enough so that you can hand in your files before the start of lab so that you do not lose time for the exam.

To be eligible for revise-and-resubmit, you must turn in something by the due date. Late work and extensions are generally not accepted/granted; see the posted late policy for more information.

About Collaboration and Getting Help

You may work with a partner if you wish. Both partners must actively contribute to the solution! You only need one handin for the group — be sure to put both teammates' names in every file.

Otherwise you may get help but you may not use other resources (friends, neighbors, websites, ChatGPT, etc) to produce answers. See the posted policy on academic integrity for more information.

Handin

To hand in your work:


Preliminaries

20 Questions

You may have played the game of 20 Questions — one person thinks of something and the other tries to guess what it is by asking only yes-no questions. It's called 20 Questions because traditionally the guesser only gets 20 guesses.

In this version, the user is the one thinking of the thing while the computer tries to guess it. There will not be any restriction on the number of questions — the computer will keep guessing until it runs out of questions.

The computer's knowledge of yes-no questions and things will be stored in a binary decision tree like the example shown. Each internal node will store a question; leaves will store the known things. (Note that this tree will not have dummy leaves — every node will store an element.) Use the convention that the left child of each node represents a "yes" answer to the question stored at that node and the right child represents a "no" answer. The computer's knowledge of the world will be built up as it plays the game.


Provided Code and Files

You've been provided with a version of the TreeNode class used in class, a main program for the game 20 Questions (TwentyQuestions), a saved game tree (tree.txt), and solutions for the exercises in the lab in compiled form (20Q.jar). This allows you to run the program and use all the functionality as you work on each piece.

The provided TreeNode class has been defined as a generic so that it can be used for any type of element (instead of the int-only version from class). Work with like the Java Collections classes that are defined as generics — remember that the element type is part of the name of the type and the constructor.


Exercises

Setup

Make sure you've read about 20 Questions above (in the Preliminaries section), then run the program to see how things should work. (Make sure you understand the output in each case.) In particular, try the following (in order):

The " ** provided version" messages indicate that you are using the provided solutions; you will replacing these with your own code in the exercises.

Tasks

All of your code should go into the provided TwentyQuestions.java file. You can do the three exercises in any order. Read the more detailed descriptions of each method given in each section before starting to write code for that method.

Hint: remember the patterns of movement for trees as discussed in class. When you are implementing something involving moving through a tree, first identify which pattern is applicable. That then gives you a template for your code.


Exercise #1: play

One round (or game) consists of the computer asking a series of questions until it guesses a thing, updating the tree if the guess is incorrect. That is, start at the root of the tree and as long as the current node is an internal node, ask the question stored at that node, get the user's response, and update the current node (go to the left child if the user responded yes and the right child if the user responded no). When a leaf is reached, the computer should guess the thing at the leaf and the user should be asked if the guess was correct. If the guess is correct, a victory message should be printed. If the guess is incorrect, the user should be prompted for the thing they were thinking of and a question which can be used to distinguish between the user's thing and the computer's guess. The new question and thing should then be added to the tree. Make sure that you know if the answer to the question should be yes or no for the user's thing so you can update the tree correctly.

Note: both questions and things entered by the user may contain spaces. Be sure to read an entire line of text when you read in one of these things.

Hint: Draw an example or two of a tree before and after adding the new information to understand what needs to be done to the tree. (This also helps you get the steps in the right order.)

A sample run of the program showing several rounds of the game is shown below. Bold underline indicates user input and italics indicates things printed out by the provided code; things not in bold, underline, or italics should be produced by your code in play. You do not need to reproduce the bold, underline, or italics — this is just to show what comes from where in the example.

(p)lay, print (t)ree, (f)ind thing, (s)ave, (l)oad, or (q)uit?  l

enter filename to load from: tree.txt

(p)lay, print (t)ree, (f)ind thing, (s)ave, (l)oad, or (q)uit?  t

does it have fur?
 does it lay eggs?
  platypus
  cat
 is it a reptile?
  lizard
  duck

(p)lay, print (t)ree, (f)ind thing, (s)ave, (l)oad, or (q)uit?  p

does it have fur?  yes
does it lay eggs?  yes
you're thinking of platypus!
   right? yes
I guessed it!

(p)lay, print (t)ree, (f)ind thing, (s)ave, (l)oad, or (q)uit?  p

does it have fur?  no
is it a reptile?  yes
you're thinking of lizard!
   right?  no
what were you thinking of?  dragon
a question which is 'yes' for dragon and 'no' for lizard: 
does it fly?

(p)lay, print (t)ree, (f)ind thing, (s)ave, (l)oad, or (q)uit?  t

does it have fur?
 does it lay eggs?
  platypus
  cat
 is it a reptile?
  does it fly?
   dragon
   lizard
  duck

Exercise #2: printQs

printQs should print the series of questions and answers that lead to a particular thing in the tree. The method's comments show the format that the output should take.

Hint: Break this into three tasks — first just get the right questions printed in some order; don't worry about the printing the answers or the order of the questions. Then get the questions printed in the right order — if they are coming out in reverse order, think about what you know about reversing things... Finally, print the "yes" or "no" answer for each question. How can you figure out this information?

A sample run of the program showing the usage of printQs is shown below. Bold underline indicates user input and italics indicates things printed out by the provided code; things not in bold, underline, or italics should be produced by your code in printQs. You do not need to reproduce the bold, underline, or italics — this is just to show what comes from where in the example.

(p)lay, print (t)ree, (f)ind thing, (s)ave, (l)oad, or (q)uit?  l

enter filename to load from: tree.txt

(p)lay, print (t)ree, (f)ind thing, (s)ave, (l)oad, or (q)uit?  t

does it have fur?
 does it lay eggs?
  platypus
  cat
 is it a reptile?
  lizard
  duck

(p)lay, print (t)ree, (f)ind thing, (s)ave, (l)oad, or (q)uit?  f

enter the thing to look for: cat

cat --
  does it have fur?  yes
  does it lay eggs?  no

(p)lay, print (t)ree, (f)ind thing, (s)ave, (l)oad, or (q)uit?  f

enter the thing to look for: lizard

lizard --
  does it have fur?  no
  is it a reptile?  yes

(p)lay, print (t)ree, (f)ind thing, (s)ave, (l)oad, or (q)uit?  f

enter the thing to look for: emu

emu is not a thing in this tree

Exercise #3: find

Note that only leaf nodes should be checked to see if they contain the thing being looked for — return null if no leaf nodes contain thing even if there is an internal node containing thing.


(Optional) If You Have Time

(This isn't required or graded, but is good to think about if you have extra time.)

Note: do not change any of the public method headers in the provided code, but you may add private helper methods if needed.