Question

In: Computer Science

Use C programming to Implement the breadth-first tree traversal. The tasks are: a) Build the tree....

Use C programming to Implement the breadth-first tree traversal. The tasks are:

a) Build the tree.

b) Perform breadth first traversal on the tree that you have built at step

a).

Note: "Please comment the program"

Solutions

Expert Solution

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

//Tree Structure

struct Tree {

struct Tree* left, *right;

int data;

};

//Queue Structure

struct QNode {

struct QNode* next;

struct Tree* BData;

};

//Queue front and rear pointers

struct Queue {

struct QNode* front, *rear;

};

//Creating a new queue Node

struct QNode* newQNode(struct Tree* data)

{

struct QNode* temp = malloc(sizeof(struct QNode));

temp->BData = data;

temp->next = NULL;

return temp;

}

//Creating a new Tree Node

struct Tree* CreateNode(int data)

{

struct Tree* temp = malloc(sizeof(struct Tree));

temp->data = data;

temp->left = temp->right = NULL;

return temp;

}

//Method to enqueue

void enQueue(struct Queue* q, struct Tree* data)

{

struct QNode* temp = newQNode(data);

if (q->front == NULL && q->rear == NULL)

{

q->front = q->rear = temp;

return;

}

q->rear->next = temp;

q->rear = temp;

}

//Method to pop or dequeue

struct Tree* pop(struct Queue* q)

{

if (q->front == NULL)

return NULL;

struct QNode* temp = q->front;

struct Tree* bdata = temp->BData;

q->front = q->front->next;

if (q->front == NULL)

q->rear = NULL;

return bdata;

}

int IsEmpty(struct Queue* q)

{

return (q->front == NULL && q->rear == NULL);

}

//breadthFirst method

void breadthFirst(struct Tree *root)

{

// Base Case

if (root == NULL)

return;

struct Queue* q = malloc(sizeof(struct Queue));

q->front = q->rear = NULL;

enQueue(q, root);

//Check if Queue is NOT Empty

while (!IsEmpty(q))

{

struct Tree* temp = pop(q);

printf("%d ", temp->data);

/* Enqueue left child */

if (temp->left != NULL)

enQueue(q,temp->left);

/* Enqueue right child */

if (temp->right != NULL)

enQueue(q,temp->right);

}

}

int main()

{

//Create Tree with 1 as root

struct Tree *root = CreateNode(1);

//2 as left child with 1 as root

root->left = CreateNode(2);

//3 as right child with 1 as root

root->right = CreateNode(3);

//4 as left of left child with 2 as parent

root->left->left = CreateNode(4);

//4 as right of right child with 3 as parent

root->left->right = CreateNode(5);

printf("\nBreadth First: ");

//Call BFS with root

breadthFirst(root);


return 0;

}

=====================================

SEE OUTPUT

PLEASE COMMENT if there is any concern.

=========================================


Related Solutions

Traversal tree program in c/c++
Traversal tree program in c/c++
Write a C program that demonstrate tree traversal. A. first is to ask the user to...
Write a C program that demonstrate tree traversal. A. first is to ask the user to enter data B. ask how many nodes THIS IS JUST A SAMPLE. PLEASE ANSWER IT IN COMPLETE CODE. See for the sample output. Sample Output: How many node do you have in your tree : 5 Enter root: 20 if you want to add to the left press 0 otherwise press 1 answer: 0 Enter next node: 10 if you want to add to...
Modifying the tree traversal C program shown below to do reversed tree traversal as defined below....
Modifying the tree traversal C program shown below to do reversed tree traversal as defined below. Reversed PreOrder: Root, Right, Left. Reversed InOrder:     Right, Root, Left. Reversed PostOrder: Right, Left, Root. The tree is: Root = 1, Left(1) = -2, Right(1) = -3; Left(-2) = 4, Right(-2) = 5; Left(-3) = 6, Right(-3)= 7; Left(5) = -8, Right(5)= -9; Left(7) = 10, Right(7) = 11; Left(11) = -12, Right(11) = -13; Left(-13) = 14. Your program should output the printed...
Tree Reconstruction - Pre+In => Post (C++) Give you pre-order traversal and in-order traversal, please output...
Tree Reconstruction - Pre+In => Post (C++) Give you pre-order traversal and in-order traversal, please output post-order traversal. Input Two lines. Pre-order traversal and in-order traversal. Output Post-order traversal. Sample Input GDAFEMHZ ADEFGHMZ Sample Output AEFDHZMG
C++ tree program (do NOT use STRUCT, use classes)    Program 1 Implement a Binary tree...
C++ tree program (do NOT use STRUCT, use classes)    Program 1 Implement a Binary tree using an array    Program 2 Implement a tree using linked list - pointer Binary Tree    Program 3 - Convert program 1 to a template
do the following SQL programming tasks: Use the CREATE TABLE statement to build the sample table...
do the following SQL programming tasks: Use the CREATE TABLE statement to build the sample table (MODULE) Use the INSERT INTO statement to populate it - use either the data in the image or your own Write an SQL query to display the whole populated table Write an SQL query to display certain combinations of columns (use your imagination) Write an SQL query to extract certain combinations of columns and rows (imagination again!)
In this assignment, you will implement Breadth First Search (BFS). The input to your program is...
In this assignment, you will implement Breadth First Search (BFS). The input to your program is a graph in the adjacency list format. The input graph has at most 100 vertices. Your program must initiate a BFS from vertex 2 of the graph. If the graph is connected, your program must output “Graph is connected”. If the graph is disconnected, your program must output “Graph is not connected”. Your program should read from an input file: data2.txt and write to...
Show that a breadth-first spanning tree contains the shortest paths be- tween the root to every...
Show that a breadth-first spanning tree contains the shortest paths be- tween the root to every other vertex.
This is C++ programming. Use separate compilation to implement a polynomial ADT that manipulates polynomials in...
This is C++ programming. Use separate compilation to implement a polynomial ADT that manipulates polynomials in a single variable x (e.g., p = 4 x^5 + 7 x^3 – x^2 + 9 ). For this problem, consider only polynomials whose exponents are non-negative integers. You are required to identify a proper data representation schema to store such polynomials and hide such data from external users of this ADT. Additionally, your ADT will at least include the following member functions: One...
Design a nonrecursive post order traversal algorithm. 1.Use the linked representation for the binary tree to...
Design a nonrecursive post order traversal algorithm. 1.Use the linked representation for the binary tree to be traversed. 2.Except left, right, and data fields, there are no other fields in a node. 3.After the traversal the tree should remain intact. 4.Use only one stack. 5.You can’t push anything other than nodes into the stack.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT