Question

In: Computer Science

Write a program to create a tree randomly. You can use C++ programming language. The input...

Write a program to create a tree randomly. You can use C++ programming language. The input is the number of vertices in the tree, and the output is an adjacent list of the tree. (Managed to complete this assignment with a binary tree. But, was told I needed a general tree instead)

Solutions

Expert Solution

The basic idea behind printing the adjacency list of a random tree is to apply Depth FIrst Search (DFS) over it.

Code:

#include <bits/stdc++.h> 
using namespace std; 

// DFS on tree 
void dfs(vector<int> list[], int node, int arrival) 
{ 
        // Printing traversed node 
        cout << node << '\n'; 

        // Traversing adjacent edges 
        for (int i = 0; i < list[node].size(); i++) { 

                // Not traversing the parent node 
                if (list[node][i] != arrival) 
                        dfs(list, list[node][i], node); 
        } 
} 

// driver program
int main() 
{ 
        // Number of vertices 
        int nodes; 
  cout<<"Enter no. of vertices: ";
  cin>>nodes;

        // Adjacency list 
        vector<int> list[10000]; 

        // Creating the tree 
        list[1].push_back(2); 
        list[2].push_back(1); 

        list[1].push_back(3); 
        list[3].push_back(1); 

        list[2].push_back(4); 
        list[4].push_back(2); 

        list[3].push_back(5); 
        list[5].push_back(3); 

        // Function call 
  cout<<"The required adjacency list: \n";
        dfs(list, 1, 0); 

        return 0; 
} 


Related Solutions

In programming C language, write a program that creates a binary tree of words to be...
In programming C language, write a program that creates a binary tree of words to be used as a spell checking device for various text files. The list of words will come from a file “words.txt”. Your program is to read through the “words.txt” file and insert the word on that line into the tree in lexicographic order (also known as Dictionary order). Words in the file will be separated by spaces. Once this is done, your program should then...
In programming C language, write a program that creates a binary tree of words to be...
In programming C language, write a program that creates a binary tree of words to be used as a spell checking device for various text files. The list of words will come from a file “words.txt”. Your program is to read through the “words.txt” file and insert the word on that line into the tree in lexicographic order (also known as Dictionary order). Words in the file will be separated by spaces. Once this is done, your program should then...
Lab 1 Write a program in the C/C++ programming language to input and add two fractions...
Lab 1 Write a program in the C/C++ programming language to input and add two fractions each represented as a numerator and denominator. Do not use classes or structures. Print your result (which is also represented as a numerator/denominator) to standard out. If you get done early, try to simplify your result with the least common denominator. The following equation can be used to add fractions: a/b + c/d = (a*d + b*c)/(b*d) Example: 1/2 + 1/4 = ( 1(4)...
Use C language Write a program that reads in a series of lines of input character...
Use C language Write a program that reads in a series of lines of input character by character (using getchar()). The first line of the input contains an integer which specifies the number of remaining lines of input, each of which contains a floating point number. The integer value on the first line can be read with scanf(), but all of the following lines can only be read with getchar(). Each line after the first contains a single floating point...
in the c programming language input is given in the form The input will be of...
in the c programming language input is given in the form The input will be of the form [number of terms] [coefficient k] [exponent k] … [coefficient 1] [exponent 1] eg. 5 ─3 7 824 5 ─7 3 1 2 9 0 in this there are 5 terms with -3x^7 being the highest /* Initialize all coefficients and exponents of the polynomial to zero. */ void init_polynom( int coeff[ ], int exp[ ] ) { /* ADD YOUR CODE HERE...
1a) Write a program in C programming language to determine *pass* or *fail*. Use the GP...
1a) Write a program in C programming language to determine *pass* or *fail*. Use the GP ( 0.00 - 1.49 -> fail 1.50 - 4.00 -> pass ) 1b) Write a program in C to display month in Islamic Calendar.
C Programming Language: For this lab, you are going to create two programs. The first program...
C Programming Language: For this lab, you are going to create two programs. The first program (named AsciiToBinary) will read data from an ASCII file and save the data to a new file in a binary format. The second program (named BinaryToAscii) will read data from a binary file and save the data to a new file in ASCII format. Specifications: Both programs will obtain the filenames to be read and written from command line parameters. For example: - bash$...
C# Programming Language Write a C# program ( Console or GUI ) that prompts the user...
C# Programming Language Write a C# program ( Console or GUI ) that prompts the user to enter the three examinations ( test 1, test 2, and test 3), homework, and final project grades then calculate and display the overall grade along with a message, using the selection structure (if/else). The message is based on the following criteria: “Excellent” if the overall grade is 90 or more. “Good” if the overall grade is between 80 and 90 ( not including...
In C programming language, write the program "3x3" in size, calculating the matrix "c = a...
In C programming language, write the program "3x3" in size, calculating the matrix "c = a * b" by reading the a and b matrices from the outside and writing on the screen?
Programming Language Required: C Write a multithreaded program in C (not c++) using the pthread library...
Programming Language Required: C Write a multithreaded program in C (not c++) using the pthread library and dynamic memory(malloc) that multiplies two matrices together. The numbers in the matrices must be read in from a text file. The program should also check if the two matrices are capable of being multiplied together. The amount of threads used has to be dynamic. The user should be able to choose how many threads they wish to use using the command line. Finally,...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT