COMP225 Assignment 1 — Semester 1, 2008


Tree-structured databases: Part 1

Worth 5%

Due 11:59pm on Monday 7 April 2008.


Objectives

This assignment is designed to give you practice in programming with pointer-based trees. You will also learn how to save a tree in serialised format, and why that is important.

You must submit two files, using the electronic submission system . The first file should be called Guess.cpp, and should complete the code you need to solve the problem described below at Task 1. The second file should be called QuestionNode.h and should complete the code you need to solve the problem described below at Task 2.

The formats you should use for your files are specified below.

If you do not use the filenames stated above, or you do not use the file format specified below, then you will receive 0 (zero) marks.

Submit your work by the due date and time stated above.

Overview

A database is an arrangement of stored data which is set up so that the data can be queried easily. Almost every “information system” has inside it some kind of database. In some applications the database is not static, but rather must change dynamically whenever its content needs to be updated. A typical scenario could be to store data in an automated enquiry service, which is becoming typical of today’s telephone-enquiry services. The data base would store a selection of questions having “yes/no” answers in order to identify the category of the enquiry, with the system then giving the user some useful information most related to the identified category. The most advanced enquiry services allow the system actually to update itself in the case it “learns” a new category. In Assignment 1 and Assignment 2 you will be practise the basics of how to program a database system which can learn new categories and update itself automatically.

This Assignment

In this assignment you will be asked to program a pointer-based tree implementation of a simple “Animal/Vegetable/Mineral” game. You have been provided with a number of program and specification files, and one executable. The executable called Guess will run on titanic and gives you an idea of what your programs should do when they are correctly implemented, compiled and executed. The other files are as follows:
  1. QuestionNode.h This is the file which deals with the building blocks for a tree; it defines a tree node, and implements a constructor. It contains one unimplemented function which you are asked to implement for Task 2 in this assignment.
  2. Animal.h This contains some utility routines for implementing the game. You will need to use the function getAnswer defined there. This function takes two strings: the first is a question; the second is a string listing the expected answers. It outputs the question and the list of answers and then waits for the user’s response; it then returns the number in the answer list matching the response.
  3. Guess.cpp This is the file where the main game functions and program are implemented. Your Task 1 below asks you to complete the implementation of the function Play which is the main routine for playing the game. Whenever it is necessary to add a new node in the game, this will happen in the function Play. To understand how Play should work, read the specification of Task 1 below, look at the function main in Guess.cpp which uses it, and play the game for yourself, by executing GuessGame. Then complete the starred tutorial and practical exercises.
  4. animalData is a file containing the data to build a tree. The data stored in this file is a representation of a previously saved game tree in “serialised form”. When you run GuessGame in the same directory as animalData the first thing that happens is that a game tree is constructed from this data, effectively restoring the old game. This is typically how tree data structures can be saved, for example when the database needs to be “backed up”. Your Task 2 asks you to write the part of the code which allows a game tree to be saved in serialised format.

    To understand how the format relates to a game tree, try playing the game –without adding nodes– while you have the file animalData in front of you, and see whether you can “follow” the the program through the file in the file as you respond to the questions the program asks. Answering the starred tutorial exercises will also help you to understand this format.

To play the game using the executable GuessGame, download it and the file animalData and make sure that they are in the same directory. Then type the command ./GuessGame. You can add more data as you play the game, and save it at the end of the game.

Task 1 (10 marks)

Your first task is to complete the function Play in Guess.cpp so that your program plays the game in the same way that the given executable GuessGame does.

To help your understanding of the QuestionNode data structure, complete the starred Tutorial and Practical exercises in Weeks 3 and 4. Then interpret the following pseudocode to complete this task. (UPPER-CASE portions are for you to fill in.)

void Play(QuestionNode *&qN) { // Must be reference to allow a new node possibly to be grafted on in this call.
   if (THE CURRENT NODE qN IS A GUESS NODE) { // OTHERWISE IT'S A QUESTION
      GUESS THE ANSWER;
      if (THE PROGRAM GUESSED CORRECTLY) then PRINT "GREAT! LET'S PLAY AGAIN";
      else { // THE PROGRAM GUESSED INCORRECTLY
         ASK THE USER FOR THE ANSWER, VIA STANDARD OUTPUT/INPUT;
         ASK THE USER FOR A QUESTION THAT WILL DISTINGUISH
            THE ANSWER FROM THE GUESS;
         MAKE A NEW NODE CONTAINING THAT QUESTION, FOR WHICH A "YES"
            LEADS TO THE ACTUAL ANSWER, AND "NO" TO THE GUESS;
         INSERT THAT NEW NODE AT THE CORRECT POINT IN THE TREE;
            // (HINT: DO THE STARRED TUTORIAL EXERCISES FOR HELP WITH THIS)
       } 
   }   
   else { // THE CURRENT NODE IS A QUESTION
     ASK THE QUESTION; GET THE ANSWER; CONTINUE TO PLAY IN THE CORRECT SUB-TREE;
   }
}
You will have correctly implemented this function if it does exactly the same as the game GuessGame. However you will be marked separately for implementing (a) playing the game without additions, and (b) for correctly adding a new node.

Remember to compile your code, have all the files QuestionNode.h, Animal.h and Guess.cpp in the same folder and use g++ Guess.cpp.

Task 2 (5 marks)

The second task is to complete the Save function in QuestionNode.h which takes a game tree and saves it as a serial file, according to the following format:

We use tags T: to denote a text node, and N: to denote NULL. The depth within the game tree is denoted by the indentation so that a node appears at depth d just when its tag is preceded on the line by d-1 spaces. Thus at depth 1 (no spaces) there are three nodes (one for animal, one for vegetable and one for mineral).

All other nodes either have two children, or no children, or are NULL, and they are all at depth greater than 1. After a T: there is text which is either a question (for a question node) or a guess (for a guess node). Its left branch (for "yes") appears on the next line(s) below, indented by one more space, followed by its representation of the subtree.

To check that your code is producing the correct result, copy the game whose log of interactive responses are saved in the file gameRun. Start from scratch (i.e. without a file animalData), and when you've played that game save it to file. Your program's output should be exactly the same as in the file animalData1.

The files

Animal class (fully implemented):
Animal.h

QuestionNode class (partially implemented): QuestionNode.h

The game program (partially implemented): Guess.cpp

An example saved game: animalData

Another example saved game, which saves the game played out in gameRun: animalData1

An example log of an interactive session: gameRun

The executable: GuessGame

Submission instructions

Submit your files using the on-line Blackboard system.

Assessment

This assignment will be marked electronically. To see how the marks will be awarded get the marking scheme here.

The electronic part

You may have your program “trialed” by the electronic marker before the due date. Trials will begin from 31 March onwards. Any files submitted after that time and before the deadline will be presented to the electronic marker every alternate day until the submission date.

The results of the trial runs will be emailed to you at your student account. These trial results will be completely disregarded for marking, so do not worry that mistakes in the trials will count against you: they will not. The feedback is to help you correct errors that may be lurking in your programs, and to make sure that your code is efficient enough.

You will receive the actual results for the electronically marked part of Assignment 1 after the final deadline for submission.

Hints for obtaining full marks in this assignment

There are a number of common mistakes that students make which prevent them receiving full marks, even if they think they have thoroughly tested their programs!

Remember that there will be several opportunities to check that your programs survive the electronic marker before the final submission date.

In spite of all these warnings, sometimes programs are submitted that, astonishingly, don’t even compile with our departmental electronic marker. This means that their authors not only ignored all the warnings above, but they did not even once use the trial-marking system.

Try not to be one of those people.