CS360 Midterm -- October 18, 1999. Question 1: Answer
and Grading
10 points
The answer
Print_tree() should traverse the input tree. It should
cast the val field of each node to a PTstruct,
and then test the type field. If the type is
zero, then it should print out the node's key, and
fn.s. If the type is one, then it should cast fn.v
to a JRB, traverse it and then print the original node's
key with each sub-tree node's key:
void print_tree(JRB t)
{
JRB tmp1, tmp2, subtree;
PTstruct *p;
jrb_traverse(tmp1, t) {
p = (PTstruct *) tmp1->val.v;
if (p->type == 0) {
printf("%s %s\n", p->fn.s, tmp1->key.s);
} else {
subtree = (JRB) p->fn.v;
jrb_traverse(tmp2, subtree) {
printf("%s %s\n", tmp2->key.s, tmp1->key.s);
}
}
}
}
Grading
Points were allocated as follows:
- Overall structure: 1 point
- Traversing the first tree: 1 point
- Casting the val.v to a PTstruct *: 1 point
- Printing the name when p->type = 0: 1 point (note, it
did not matter if you printed the first name first, or the
last name first, as long as you were consistent.
- Printing the first name with fn.s: 1 point
- Printing the last name with key.s: 1 point
- Casting fn.v to a JRB if p->type = 1: 1 point
- Traversing this second tree: 1 point
- Printing the correct name for each subtree node: 1 point
- Getting other details right: 1 point