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: