S-Q 1997 - Assignment 3 Due 07/17/97 at 6:00pm Last Date Accepted: 07/21/97 at 6:00pm
All work should be done in directory /Q/asst3. The Makefile provided includes rules to build all the programs in this assignment; you shouldn't need to change it at all. If you add additional files for your animals program, then you may need to add these files to the Makefile; see the Makefile for directions.
Hand-drawn figures are preferable; do not spend a lot of time trying to make the figures beautiful.
Once again, hand-drawn figures are preferable.
You can write out your proof longhand or use whatever tools you wish to edit it. If you use plaintext, place your answer in file asst3.txt.
A level-order traversal processes the root node, then the keys of the nodes at depth 1 (from left to right), then the keys of the nodes at depth 2 (again from left to right), and so on. The traversal must run in time O(n) and space O(n), where n is the number of nodes in the tree (regardless of the shape of the tree).
You can write out your algorithm longhand or use whatever tools you wish to edit it. If you use plaintext, place your answer in file asst3.txt.
You do not need to implement your algorithm, but your algorithm should be detailed enough that it would be easy to implement.
An animal tree is a binary tree where the internal nodes are called question nodes and represent questions, while the leaves are called animal nodes and represent the names of animals. The questions are always yes/no questions. All the left descendants of a question nodes correspond to a ``yes'' answer and all the right descendants a ``no'' answer. The game of animals is an interactive program that constructs an animal tree, based on input from the user, and uses this information to try to guess the identity of whatever animal the user thinks of.
Figure shows an example dialog with the game, produced by sol_animals. (You may find it very useful to run sol_animals to get a feel for how your program should work. Do not feel compelled to provide a completely identical user interface, but do make sure that your program is easy to use and has a sensible user interface.) Figure shows the corresponding animal tree that would exist at the end of the game illustrated in figure , and figure shows how this tree looks as each new animal and question is added.
Many functions that you may find useful are already implemented in animals.c (with corresponding declarations and definitions in animals.h. You may use, modify, or ignore this code in any way you wish.
As mentioned in lecture, is possible to traverse a binary tree iteratively, without any use of recursion. Complete function treePrintIterative in tree_print.c so that it is functionally identical to treePrintRecursive, but does not use recursion at all. For full credit, your algorithm must run in time O(n) and space O(n), where is n is the number of nodes in the tree. Be sure to document your algorithm; a reasonable algorithm is worth many points, even if the code is not perfect.
Hint- you may find a stack very useful. You may also notice some similarity between this algorithm and your algorithm for performing a level-order traversal- in particular, the code to perform a preorder traversal using a stack may remind you of your algorithm for performing a level-order traversal using a queue. Implementing inorder and postorder traversals requires an additional trick, however.
Figure: The text of a short game of animals. User
input is indicated by a bold font.
Figure: Construction of the animal tree.
This document was generated using the LaTeX2HTML translator Version 96.1-h (September 30, 1996) Copyright © 1993, 1994, 1995, 1996, Nikos Drakos, Computer Based Learning Unit, University of Leeds.
The command line arguments were:
latex2html -split 0 asst3.tex.
The translation was initiated by Dan Ellard on Thu Jul 10 12:03:09 EDT 1997