Question: Please help me with this Write a function that takes the adjacency matrix of a tree as input and prints the tree's level-order traversal. The
Write a function that takes the adjacency matrix of a tree as input and prints the tree's level-order traversal. The elements should be printed in ascending order given by the level-order traversal of the tree going from left to right in a level(i.e. Start from node 0 and print the nodes in ascending order of this level. Let's call it level 1. After printing all the nodes of level 1, start level 2 by printing the children of the first node in level 1 in ascending order, then the children of the second node in level 1 in ascending order, and so on). The root node of the tree is always node O An example of the operation is shown below. INPUT 01 02 13 18 256 242 OUTPUT 012384256 All necessary starter codes (along with a sample main function) have been provided to you in the 1 2 4 10 11 12 14 15 16 17 18 19 20 21 22 24 25 26 #include #include #define NODES looe // Function to add an edge to the adjacency list void addEdge(int NODES], int u, int v) // Function to perform Level Order traversal void levelordertraversal(int adj[t'1AX NODES] [MAX NODES], int nunnodes, int startNode) { //adj: The adjacency matrix of the tree //numNodes: The maximum value Node + 1 present in the tree // startNode: Root Node i.e e // TODO: Complete the function to print the level order traversal of the tree int main() { int numEdges; scanf("Xd", &numEdges); int int nuntlodes = e; Activate W Go to PC settir for (int i e; i < numEdges; i++) { int u, v; addEdge(adj, u, v); if (u > numNodes) numNodes u; if (v > numNodes) numNodes - v; numNodes++; // Nodes are assumed to be zero-indexed, so increment by one for the count levelordertraversal (adj, numNodes, 0); printf(" "); return e;
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
