Question: In C language. #include #include #include int main( int argc, char *argv[]){ FILE *fin, *fout; int i; int a, b, p, n, root, lca; fin

In C language.

In C language. #include #include #include int main(int argc, char *argv[]){ FILE

#include

#include

#include

int main(int argc, char *argv[]){

FILE *fin, *fout;

int i;

int a, b, p, n, root, lca;

fin = fopen(argv[1], "r");

fout = fopen(argv[2], "w");

/* initialization */

// You code here

/* read input, and construct tree */

fscanf(fin, "%d", &N);

for (i = 0; i

fscanf(fin, "%d%d", &p, &n);

// You code here

}

fscanf(fin, "%d%d", &a, &b);

// You code here

/* get lowest common ancestor and output */

lca = 0;

// You code here

fprintf(fout, "%d ", lca);

fclose(fin);

fclose(fout);

return 0;

}

Question 3: Lowest Common Ancestor (30\%) You are given a general tree. You are asked to identify the lowest common ancestor of two nodes, i.e. the deepest ancestor of the two given nodes. All nodes only contain a unique number ranging from 0 to 200 . Your program should read the input from the file, and output the answer to another file. The first argument is the input file name, while the second argument is the output file name. Name your program as "lab5-q3.c". Input file: First line, an integer N, indicating the number of nodes in the tree. Following n - 1 lines, pn, two integers, node p is the parent of node n. The p in line 2 is the root of the tree. Last line, a b, two integers, find the lowest common ancestor of node a and node b. Output file: An integer, I, indicates the lowest common ancestor of node a and node b

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!