Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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.

image text in transcribed

#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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Systems Design Implementation And Management

Authors: Peter Rob, Carlos Coronel

6th International Edition

061921323X, 978-0619213237

More Books

Students also viewed these Databases questions