CS360 Midterm -- March 14, 2002. Question 5

The following procedure takes a two-level red-black tree and a STDIO file pointer as input. It then writes the tree to the file. It knows the following things about the red-black tree:
  • The keys in the first-level tree are integers (inserted with jrb_insert_int()).
  • The keys in the second-level tree are doubles. (inserted with jrb_insert_dbl()).
  • The vals in the second-level tree are doubles.

writer(JRB t, FILE *f)
{
  JRB tmp1, tmp2, t2;
  int i;
 
  jrb_traverse(tmp1, t) {

    t2 = (JRB) tmp->val.v;
    fwrite(&tmp1->key.i, sizeof(int), 1, f);

    i = 0;
    jrb_traverse(tmp2, t2) i++;
    fwrite(&i, sizeof(int), 1, f);

    jrb_traverse(tmp2, t2) {
      fwrite(&tmp2->key.d, sizeof(double), 1, f);
      fwrite(&tmp2->val.d, sizeof(double), 1, f);
    }

  }
}

Your job is to write the procedure reader, which takes as input the pointer to a file created by writer, and returns the red-black tree stored in that file.

JRB reader(FILE *f)
{
   ...
}

Some prototypes:
JRB jrb_insert_int(JRB tree, int key, Jval val);
JRB jrb_insert_dbl(JRB tree, double key, Jval val);
int fread(void *ptr, int element_size, int num_elements, FILE *fp);
int fwrite(void *ptr, int element_size, int num_elements, FILE *fp);