SlideShare a Scribd company logo
1 of 6
Download to read offline
#include
#include
struct node
{
int id;
struct node *leftchild;
struct node *next; // next sibling
};
struct stack
{ // Stack of pointers to nodes
struct node **node;
int top;
};
struct node *create_node(int id);
struct stack *create_stack(int size);
void destroy_stack(struct stack *s);
int stack_size(struct stack *s);
void stack_push(struct stack *s, struct node *p);
struct node *stack_pop(struct stack *s);
struct node *find_node(struct node *root, int target_id);
void add_node(struct node *root, int parent, int child);
struct node *create_tree();
void destroy_tree(struct node *root);
void display_tree(struct node *root);
void dfs(struct node *root)
{
struct stack *s = create_stack(100);
stack_push(s, root);
struct node *curr;
while (stack_size(s) >> 0)
{
curr = stack_pop(s);
printf("%2d ", curr->id);
for (struct node *p = curr->leftchild; p != NULL; p = p->next)
{
stack_push(s, p);
}
}
destroy_stack(s);
}
/** Modify */
struct queue
{
};
/** Modify */
void bfs(struct node *root)
{
struct stack *s = create_stack(100);
stack_push(s, root);
struct node *curr;
while (stack_size(s) >> 0)
{
curr = stack_pop(s);
printf("%2d ", curr->id);
for (struct node *p = curr->leftchild; p != NULL; p = p->next)
{
stack_push(s, p);
}
}
destroy_stack(s);
}
void main()
{
struct node *root = create_tree();
printf("Display Tree [node, child]n");
display_tree(root);
printf("n");
printf("DFSn");
dfs(root);
printf("n");
printf("BFSn");
bfs(root);
printf("n");
destroy_tree(root);
}
struct node *create_node(int id)
{
struct node *p = (struct node *)malloc(sizeof(struct node));
p->id = id;
p->next = NULL;
p->leftchild = NULL;
}
struct stack *create_stack(int size)
{
struct stack *s = (struct stack *)malloc(sizeof(struct stack));
s->top = -1;
s->node = (struct node **)malloc(sizeof(struct node *) * size);
}
void destroy_stack(struct stack *s)
{
free(s->node);
free(s);
}
int stack_size(struct stack *s)
{
return s->top + 1;
}
void stack_push(struct stack *s, struct node *p)
{
s->top++;
s->node[s->top] = p;
}
struct node *stack_pop(struct stack *s)
{
if (s->top == -1)
return NULL;
struct node *p = s->node[s->top];
s->top--;
return p;
}
struct node *find_node(struct node *root, int target_id)
{
if (root == NULL)
{
return NULL;
}
if (root->id == target_id)
{
return root;
}
for (struct node *p = root->leftchild; p != NULL; p = p->next)
{
struct node *found_node = find_node(p, target_id);
if (found_node != NULL)
{
return found_node;
}
}
return NULL;
}
void add_node(struct node *root, int parent, int child)
{
struct node *parent_ptr = find_node(root, parent);
struct node *p = create_node(child);
p->next = parent_ptr->leftchild;
parent_ptr->leftchild = p;
}
struct node *create_tree()
{
struct node *root = create_node(0);
add_node(root, 0, 1);
add_node(root, 0, 2);
add_node(root, 0, 3);
add_node(root, 2, 4);
add_node(root, 0, 5);
add_node(root, 1, 6);
add_node(root, 1, 7);
add_node(root, 2, 8);
add_node(root, 5, 9);
add_node(root, 5, 10);
return root;
}
void destroy_tree(struct node *root)
{
if (root == NULL)
return;
for (struct node *p = root->leftchild; p != NULL; p = p->next)
{
destroy_tree(p);
}
free(root);
}
void display_tree(struct node *root)
{
if (root != NULL)
{
for (struct node *p = root->leftchild; p != NULL; p = p->next)
{
printf("[%3d, %3d]n", root->id, p->id);
display_tree(p);
}
}
}
Please implement the bfs(struct node *root) function and the necessary queue data structure
using the C programming language to match the intended output and provide the additional
functions, methods and imports used. Problem 1 [2 pt]. Attached is a C language program
search.c. It has functions: - void dfs(struct node * root, int source): prints all the nodes in a tree in
depth first search order. - void bfs (struct node * root, int source): prints all the nodes in a tree in
breadth first search order. The bfs function doesn't work, and its present form implements dfs.
Correct the bfs function, by replacing the stack with a queue. You need to define - the data
structure for the queue (struct queue) - write additional functions such as create_queue
destroy_queue queue_add (add a node to the queue) queue_get (get a node from the queue)
queue_size and perhaps others. The output should look something like this: Display Tree [node,
child] [055][[1010][[5,9][[0,3][[,8][[2,][1,7] DFS 016724835910 BFS 053211098476

More Related Content

Similar to #include stdlib.h#include stdio.hstruct node{ int .pdf

Help to implement delete_node get_succ get_pred walk and.pdf
Help to implement delete_node get_succ get_pred walk and.pdfHelp to implement delete_node get_succ get_pred walk and.pdf
Help to implement delete_node get_succ get_pred walk and.pdf
contact32
 
using set identitiesSolutionimport java.util.Scanner; c.pdf
using set identitiesSolutionimport java.util.Scanner;  c.pdfusing set identitiesSolutionimport java.util.Scanner;  c.pdf
using set identitiesSolutionimport java.util.Scanner; c.pdf
excellentmobilesabc
 
Write a C program that reads the words the user types at the command.pdf
Write a C program that reads the words the user types at the command.pdfWrite a C program that reads the words the user types at the command.pdf
Write a C program that reads the words the user types at the command.pdf
SANDEEPARIHANT
 
Implementing a basic directory-tree structure that is derived from a.pdf
Implementing a basic directory-tree structure that is derived from a.pdfImplementing a basic directory-tree structure that is derived from a.pdf
Implementing a basic directory-tree structure that is derived from a.pdf
funkybabyindia
 
in this assignment you are asked to write a simple driver program an.pdf
in this assignment you are asked to write a simple driver program an.pdfin this assignment you are asked to write a simple driver program an.pdf
in this assignment you are asked to write a simple driver program an.pdf
michardsonkhaicarr37
 
Rewrite this code so it can use a generic type instead of integers. .pdf
Rewrite this code so it can use a generic type instead of integers. .pdfRewrite this code so it can use a generic type instead of integers. .pdf
Rewrite this code so it can use a generic type instead of integers. .pdf
alphaagenciesindia
 
include ltiostreamgt include ltfstreamgt in.pdf
include ltiostreamgt include ltfstreamgt in.pdfinclude ltiostreamgt include ltfstreamgt in.pdf
include ltiostreamgt include ltfstreamgt in.pdf
adisainternational
 
pleaase I want manual solution forData Structures and Algorithm An.pdf
pleaase I want manual solution forData Structures and Algorithm An.pdfpleaase I want manual solution forData Structures and Algorithm An.pdf
pleaase I want manual solution forData Structures and Algorithm An.pdf
wasemanivytreenrco51
 
main.cpp#include TreeNode.h GIVEN void inorderTraversal(.pdf
main.cpp#include TreeNode.h GIVEN void inorderTraversal(.pdfmain.cpp#include TreeNode.h GIVEN void inorderTraversal(.pdf
main.cpp#include TreeNode.h GIVEN void inorderTraversal(.pdf
pratikradia365
 
-- Task 2- Debugging a program with stacks- queues- and doubly-linked.docx
-- Task 2- Debugging a program with stacks- queues- and doubly-linked.docx-- Task 2- Debugging a program with stacks- queues- and doubly-linked.docx
-- Task 2- Debugging a program with stacks- queues- and doubly-linked.docx
Adamq0DJonese
 
In c++ format, for each function in the code, please using the comme.pdf
In c++ format, for each function in the code, please using the comme.pdfIn c++ format, for each function in the code, please using the comme.pdf
In c++ format, for each function in the code, please using the comme.pdf
rajkumarm401
 
Please write the C++ code that would display the exact same output a.pdf
Please write the C++ code that would display the exact same output a.pdfPlease write the C++ code that would display the exact same output a.pdf
Please write the C++ code that would display the exact same output a.pdf
amarndsons
 
mainpublic class AssignmentThree {    public static void ma.pdf
mainpublic class AssignmentThree {    public static void ma.pdfmainpublic class AssignmentThree {    public static void ma.pdf
mainpublic class AssignmentThree {    public static void ma.pdf
fathimafancyjeweller
 
A)B) C++ program to create a Complete Binary tree from its Lin.pdf
A)B) C++ program to create a Complete Binary tree from its Lin.pdfA)B) C++ program to create a Complete Binary tree from its Lin.pdf
A)B) C++ program to create a Complete Binary tree from its Lin.pdf
anton291
 

Similar to #include stdlib.h#include stdio.hstruct node{ int .pdf (20)

Help to implement delete_node get_succ get_pred walk and.pdf
Help to implement delete_node get_succ get_pred walk and.pdfHelp to implement delete_node get_succ get_pred walk and.pdf
Help to implement delete_node get_succ get_pred walk and.pdf
 
using set identitiesSolutionimport java.util.Scanner; c.pdf
using set identitiesSolutionimport java.util.Scanner;  c.pdfusing set identitiesSolutionimport java.util.Scanner;  c.pdf
using set identitiesSolutionimport java.util.Scanner; c.pdf
 
Write a C program that reads the words the user types at the command.pdf
Write a C program that reads the words the user types at the command.pdfWrite a C program that reads the words the user types at the command.pdf
Write a C program that reads the words the user types at the command.pdf
 
C code on linked list #include stdio.h #include stdlib.h.pdf
 C code on linked list #include stdio.h #include stdlib.h.pdf C code on linked list #include stdio.h #include stdlib.h.pdf
C code on linked list #include stdio.h #include stdlib.h.pdf
 
Implementing a basic directory-tree structure that is derived from a.pdf
Implementing a basic directory-tree structure that is derived from a.pdfImplementing a basic directory-tree structure that is derived from a.pdf
Implementing a basic directory-tree structure that is derived from a.pdf
 
Linked lists
Linked listsLinked lists
Linked lists
 
in this assignment you are asked to write a simple driver program an.pdf
in this assignment you are asked to write a simple driver program an.pdfin this assignment you are asked to write a simple driver program an.pdf
in this assignment you are asked to write a simple driver program an.pdf
 
Rewrite this code so it can use a generic type instead of integers. .pdf
Rewrite this code so it can use a generic type instead of integers. .pdfRewrite this code so it can use a generic type instead of integers. .pdf
Rewrite this code so it can use a generic type instead of integers. .pdf
 
include ltiostreamgt include ltfstreamgt in.pdf
include ltiostreamgt include ltfstreamgt in.pdfinclude ltiostreamgt include ltfstreamgt in.pdf
include ltiostreamgt include ltfstreamgt in.pdf
 
pleaase I want manual solution forData Structures and Algorithm An.pdf
pleaase I want manual solution forData Structures and Algorithm An.pdfpleaase I want manual solution forData Structures and Algorithm An.pdf
pleaase I want manual solution forData Structures and Algorithm An.pdf
 
main.cpp#include TreeNode.h GIVEN void inorderTraversal(.pdf
main.cpp#include TreeNode.h GIVEN void inorderTraversal(.pdfmain.cpp#include TreeNode.h GIVEN void inorderTraversal(.pdf
main.cpp#include TreeNode.h GIVEN void inorderTraversal(.pdf
 
C program to insert a node in doubly linked list
C program to insert a node in doubly linked listC program to insert a node in doubly linked list
C program to insert a node in doubly linked list
 
-- Task 2- Debugging a program with stacks- queues- and doubly-linked.docx
-- Task 2- Debugging a program with stacks- queues- and doubly-linked.docx-- Task 2- Debugging a program with stacks- queues- and doubly-linked.docx
-- Task 2- Debugging a program with stacks- queues- and doubly-linked.docx
 
C program
C programC program
C program
 
In c++ format, for each function in the code, please using the comme.pdf
In c++ format, for each function in the code, please using the comme.pdfIn c++ format, for each function in the code, please using the comme.pdf
In c++ format, for each function in the code, please using the comme.pdf
 
write recursive function that calculates and returns the length of a.pdf
write recursive function that calculates and returns the length of a.pdfwrite recursive function that calculates and returns the length of a.pdf
write recursive function that calculates and returns the length of a.pdf
 
Please write the C++ code that would display the exact same output a.pdf
Please write the C++ code that would display the exact same output a.pdfPlease write the C++ code that would display the exact same output a.pdf
Please write the C++ code that would display the exact same output a.pdf
 
week-13x
week-13xweek-13x
week-13x
 
mainpublic class AssignmentThree {    public static void ma.pdf
mainpublic class AssignmentThree {    public static void ma.pdfmainpublic class AssignmentThree {    public static void ma.pdf
mainpublic class AssignmentThree {    public static void ma.pdf
 
A)B) C++ program to create a Complete Binary tree from its Lin.pdf
A)B) C++ program to create a Complete Binary tree from its Lin.pdfA)B) C++ program to create a Complete Binary tree from its Lin.pdf
A)B) C++ program to create a Complete Binary tree from its Lin.pdf
 

More from alstradecentreerode

(Rational Class) - use the original files to create the new progra.pdf
(Rational Class) - use the original files to create the new progra.pdf(Rational Class) - use the original files to create the new progra.pdf
(Rational Class) - use the original files to create the new progra.pdf
alstradecentreerode
 
(1)CT13.1) Your parents are considering investing in Apple Inc. co.pdf
(1)CT13.1) Your parents are considering investing in Apple Inc. co.pdf(1)CT13.1) Your parents are considering investing in Apple Inc. co.pdf
(1)CT13.1) Your parents are considering investing in Apple Inc. co.pdf
alstradecentreerode
 
#define LOGFILE signal_log.txt Signal handlers void sigusr1.pdf
#define LOGFILE signal_log.txt  Signal handlers void sigusr1.pdf#define LOGFILE signal_log.txt  Signal handlers void sigusr1.pdf
#define LOGFILE signal_log.txt Signal handlers void sigusr1.pdf
alstradecentreerode
 
�Cu�les de los siguientes son ejemplos de intervenciones de educa.pdf
 �Cu�les de los siguientes son ejemplos de intervenciones de educa.pdf �Cu�les de los siguientes son ejemplos de intervenciones de educa.pdf
�Cu�les de los siguientes son ejemplos de intervenciones de educa.pdf
alstradecentreerode
 

More from alstradecentreerode (20)

(Rational Class) - use the original files to create the new progra.pdf
(Rational Class) - use the original files to create the new progra.pdf(Rational Class) - use the original files to create the new progra.pdf
(Rational Class) - use the original files to create the new progra.pdf
 
(Q001) Expand the Geotour10 folder in Google Earth by clicking the t.pdf
(Q001) Expand the Geotour10 folder in Google Earth by clicking the t.pdf(Q001) Expand the Geotour10 folder in Google Earth by clicking the t.pdf
(Q001) Expand the Geotour10 folder in Google Earth by clicking the t.pdf
 
(if any) is your opportunity cost.pdf
(if any) is your opportunity cost.pdf(if any) is your opportunity cost.pdf
(if any) is your opportunity cost.pdf
 
(Forecasting and forecast error metrics calculation) Suzy Homemaker .pdf
(Forecasting and forecast error metrics calculation) Suzy Homemaker .pdf(Forecasting and forecast error metrics calculation) Suzy Homemaker .pdf
(Forecasting and forecast error metrics calculation) Suzy Homemaker .pdf
 
(Elementary Statistics) Computer=1100(102r+1).pdf
(Elementary Statistics) Computer=1100(102r+1).pdf(Elementary Statistics) Computer=1100(102r+1).pdf
(Elementary Statistics) Computer=1100(102r+1).pdf
 
(Derecho empresarial) X le otorga a Y un patrimonio vitalicio. Pos.pdf
(Derecho empresarial) X le otorga a Y un patrimonio vitalicio. Pos.pdf(Derecho empresarial) X le otorga a Y un patrimonio vitalicio. Pos.pdf
(Derecho empresarial) X le otorga a Y un patrimonio vitalicio. Pos.pdf
 
(d) Give a single line of R code that will produce a plot exactly li.pdf
(d) Give a single line of R code that will produce a plot exactly li.pdf(d) Give a single line of R code that will produce a plot exactly li.pdf
(d) Give a single line of R code that will produce a plot exactly li.pdf
 
(5 points) There are seven boys and six girls. Coach wants to select.pdf
(5 points) There are seven boys and six girls. Coach wants to select.pdf(5 points) There are seven boys and six girls. Coach wants to select.pdf
(5 points) There are seven boys and six girls. Coach wants to select.pdf
 
(2ab)5.pdf
(2ab)5.pdf(2ab)5.pdf
(2ab)5.pdf
 
(1)1�(1+.10000000000000000000000(100))�2(126127) The single precision .pdf
(1)1�(1+.10000000000000000000000(100))�2(126127) The single precision .pdf(1)1�(1+.10000000000000000000000(100))�2(126127) The single precision .pdf
(1)1�(1+.10000000000000000000000(100))�2(126127) The single precision .pdf
 
(1)CT13.1) Your parents are considering investing in Apple Inc. co.pdf
(1)CT13.1) Your parents are considering investing in Apple Inc. co.pdf(1)CT13.1) Your parents are considering investing in Apple Inc. co.pdf
(1)CT13.1) Your parents are considering investing in Apple Inc. co.pdf
 
(10 points) Construct a BST by inserting the following keys into an .pdf
(10 points) Construct a BST by inserting the following keys into an .pdf(10 points) Construct a BST by inserting the following keys into an .pdf
(10 points) Construct a BST by inserting the following keys into an .pdf
 
#2 Listed below are the numbers of triplets born in a country each y.pdf
#2 Listed below are the numbers of triplets born in a country each y.pdf#2 Listed below are the numbers of triplets born in a country each y.pdf
#2 Listed below are the numbers of triplets born in a country each y.pdf
 
#52 In a large American university in 1969, the male and female pro.pdf
#52 In a large American university in 1969, the male and female pro.pdf#52 In a large American university in 1969, the male and female pro.pdf
#52 In a large American university in 1969, the male and female pro.pdf
 
#define LOGFILE signal_log.txt Signal handlers void sigusr1.pdf
#define LOGFILE signal_log.txt  Signal handlers void sigusr1.pdf#define LOGFILE signal_log.txt  Signal handlers void sigusr1.pdf
#define LOGFILE signal_log.txt Signal handlers void sigusr1.pdf
 
�Los Premios Darwin honran adecuadamente sus ideas sobre la selecci.pdf
 �Los Premios Darwin honran adecuadamente sus ideas sobre la selecci.pdf �Los Premios Darwin honran adecuadamente sus ideas sobre la selecci.pdf
�Los Premios Darwin honran adecuadamente sus ideas sobre la selecci.pdf
 
�derine NTHikEaDs 10 itatic thresd-t thread void nolint all cosit os .pdf
 �derine NTHikEaDs 10 itatic thresd-t thread void nolint all cosit os .pdf �derine NTHikEaDs 10 itatic thresd-t thread void nolint all cosit os .pdf
�derine NTHikEaDs 10 itatic thresd-t thread void nolint all cosit os .pdf
 
�Es el az�car bueno... malo... ninguno... ambos Dime por qu�. �Cu�.pdf
 �Es el az�car bueno... malo... ninguno... ambos Dime por qu�. �Cu�.pdf �Es el az�car bueno... malo... ninguno... ambos Dime por qu�. �Cu�.pdf
�Es el az�car bueno... malo... ninguno... ambos Dime por qu�. �Cu�.pdf
 
�Cu�les de los siguientes son ejemplos de intervenciones de educa.pdf
 �Cu�les de los siguientes son ejemplos de intervenciones de educa.pdf �Cu�les de los siguientes son ejemplos de intervenciones de educa.pdf
�Cu�les de los siguientes son ejemplos de intervenciones de educa.pdf
 
�Cu�l de los siguientes se considera un indicador principal de la e.pdf
 �Cu�l de los siguientes se considera un indicador principal de la e.pdf �Cu�l de los siguientes se considera un indicador principal de la e.pdf
�Cu�l de los siguientes se considera un indicador principal de la e.pdf
 

Recently uploaded

會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽
會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽
會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽
中 央社
 
會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文
會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文
會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文
中 央社
 

Recently uploaded (20)

philosophy and it's principles based on the life
philosophy and it's principles based on the lifephilosophy and it's principles based on the life
philosophy and it's principles based on the life
 
Envelope of Discrepancy in Orthodontics: Enhancing Precision in Treatment
 Envelope of Discrepancy in Orthodontics: Enhancing Precision in Treatment Envelope of Discrepancy in Orthodontics: Enhancing Precision in Treatment
Envelope of Discrepancy in Orthodontics: Enhancing Precision in Treatment
 
An overview of the various scriptures in Hinduism
An overview of the various scriptures in HinduismAn overview of the various scriptures in Hinduism
An overview of the various scriptures in Hinduism
 
Software testing for project report .pdf
Software testing for project report .pdfSoftware testing for project report .pdf
Software testing for project report .pdf
 
Basic Civil Engineering notes on Transportation Engineering, Modes of Transpo...
Basic Civil Engineering notes on Transportation Engineering, Modes of Transpo...Basic Civil Engineering notes on Transportation Engineering, Modes of Transpo...
Basic Civil Engineering notes on Transportation Engineering, Modes of Transpo...
 
BỘ LUYỆN NGHE TIẾNG ANH 8 GLOBAL SUCCESS CẢ NĂM (GỒM 12 UNITS, MỖI UNIT GỒM 3...
BỘ LUYỆN NGHE TIẾNG ANH 8 GLOBAL SUCCESS CẢ NĂM (GỒM 12 UNITS, MỖI UNIT GỒM 3...BỘ LUYỆN NGHE TIẾNG ANH 8 GLOBAL SUCCESS CẢ NĂM (GỒM 12 UNITS, MỖI UNIT GỒM 3...
BỘ LUYỆN NGHE TIẾNG ANH 8 GLOBAL SUCCESS CẢ NĂM (GỒM 12 UNITS, MỖI UNIT GỒM 3...
 
An Overview of the Odoo 17 Discuss App.pptx
An Overview of the Odoo 17 Discuss App.pptxAn Overview of the Odoo 17 Discuss App.pptx
An Overview of the Odoo 17 Discuss App.pptx
 
size separation d pharm 1st year pharmaceutics
size separation d pharm 1st year pharmaceuticssize separation d pharm 1st year pharmaceutics
size separation d pharm 1st year pharmaceutics
 
Championnat de France de Tennis de table/
Championnat de France de Tennis de table/Championnat de France de Tennis de table/
Championnat de France de Tennis de table/
 
ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH FORM 50 CÂU TRẮC NGHI...
ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH FORM 50 CÂU TRẮC NGHI...ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH FORM 50 CÂU TRẮC NGHI...
ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH FORM 50 CÂU TRẮC NGHI...
 
Operations Management - Book1.p - Dr. Abdulfatah A. Salem
Operations Management - Book1.p  - Dr. Abdulfatah A. SalemOperations Management - Book1.p  - Dr. Abdulfatah A. Salem
Operations Management - Book1.p - Dr. Abdulfatah A. Salem
 
How to Analyse Profit of a Sales Order in Odoo 17
How to Analyse Profit of a Sales Order in Odoo 17How to Analyse Profit of a Sales Order in Odoo 17
How to Analyse Profit of a Sales Order in Odoo 17
 
PSYPACT- Practicing Over State Lines May 2024.pptx
PSYPACT- Practicing Over State Lines May 2024.pptxPSYPACT- Practicing Over State Lines May 2024.pptx
PSYPACT- Practicing Over State Lines May 2024.pptx
 
demyelinated disorder: multiple sclerosis.pptx
demyelinated disorder: multiple sclerosis.pptxdemyelinated disorder: multiple sclerosis.pptx
demyelinated disorder: multiple sclerosis.pptx
 
Navigating the Misinformation Minefield: The Role of Higher Education in the ...
Navigating the Misinformation Minefield: The Role of Higher Education in the ...Navigating the Misinformation Minefield: The Role of Higher Education in the ...
Navigating the Misinformation Minefield: The Role of Higher Education in the ...
 
Stl Algorithms in C++ jjjjjjjjjjjjjjjjjj
Stl Algorithms in C++ jjjjjjjjjjjjjjjjjjStl Algorithms in C++ jjjjjjjjjjjjjjjjjj
Stl Algorithms in C++ jjjjjjjjjjjjjjjjjj
 
An Overview of the Odoo 17 Knowledge App
An Overview of the Odoo 17 Knowledge AppAn Overview of the Odoo 17 Knowledge App
An Overview of the Odoo 17 Knowledge App
 
REPRODUCTIVE TOXICITY STUDIE OF MALE AND FEMALEpptx
REPRODUCTIVE TOXICITY  STUDIE OF MALE AND FEMALEpptxREPRODUCTIVE TOXICITY  STUDIE OF MALE AND FEMALEpptx
REPRODUCTIVE TOXICITY STUDIE OF MALE AND FEMALEpptx
 
會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽
會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽
會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽
 
會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文
會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文
會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文
 

#include stdlib.h#include stdio.hstruct node{ int .pdf

  • 1. #include #include struct node { int id; struct node *leftchild; struct node *next; // next sibling }; struct stack { // Stack of pointers to nodes struct node **node; int top; }; struct node *create_node(int id); struct stack *create_stack(int size); void destroy_stack(struct stack *s); int stack_size(struct stack *s); void stack_push(struct stack *s, struct node *p); struct node *stack_pop(struct stack *s); struct node *find_node(struct node *root, int target_id); void add_node(struct node *root, int parent, int child); struct node *create_tree(); void destroy_tree(struct node *root); void display_tree(struct node *root); void dfs(struct node *root) { struct stack *s = create_stack(100); stack_push(s, root); struct node *curr;
  • 2. while (stack_size(s) >> 0) { curr = stack_pop(s); printf("%2d ", curr->id); for (struct node *p = curr->leftchild; p != NULL; p = p->next) { stack_push(s, p); } } destroy_stack(s); } /** Modify */ struct queue { }; /** Modify */ void bfs(struct node *root) { struct stack *s = create_stack(100); stack_push(s, root); struct node *curr; while (stack_size(s) >> 0) { curr = stack_pop(s); printf("%2d ", curr->id); for (struct node *p = curr->leftchild; p != NULL; p = p->next) { stack_push(s, p); } } destroy_stack(s); } void main()
  • 3. { struct node *root = create_tree(); printf("Display Tree [node, child]n"); display_tree(root); printf("n"); printf("DFSn"); dfs(root); printf("n"); printf("BFSn"); bfs(root); printf("n"); destroy_tree(root); } struct node *create_node(int id) { struct node *p = (struct node *)malloc(sizeof(struct node)); p->id = id; p->next = NULL; p->leftchild = NULL; } struct stack *create_stack(int size) { struct stack *s = (struct stack *)malloc(sizeof(struct stack)); s->top = -1; s->node = (struct node **)malloc(sizeof(struct node *) * size); } void destroy_stack(struct stack *s) { free(s->node); free(s); }
  • 4. int stack_size(struct stack *s) { return s->top + 1; } void stack_push(struct stack *s, struct node *p) { s->top++; s->node[s->top] = p; } struct node *stack_pop(struct stack *s) { if (s->top == -1) return NULL; struct node *p = s->node[s->top]; s->top--; return p; } struct node *find_node(struct node *root, int target_id) { if (root == NULL) { return NULL; } if (root->id == target_id) { return root; } for (struct node *p = root->leftchild; p != NULL; p = p->next) { struct node *found_node = find_node(p, target_id); if (found_node != NULL)
  • 5. { return found_node; } } return NULL; } void add_node(struct node *root, int parent, int child) { struct node *parent_ptr = find_node(root, parent); struct node *p = create_node(child); p->next = parent_ptr->leftchild; parent_ptr->leftchild = p; } struct node *create_tree() { struct node *root = create_node(0); add_node(root, 0, 1); add_node(root, 0, 2); add_node(root, 0, 3); add_node(root, 2, 4); add_node(root, 0, 5); add_node(root, 1, 6); add_node(root, 1, 7); add_node(root, 2, 8); add_node(root, 5, 9); add_node(root, 5, 10); return root; } void destroy_tree(struct node *root) { if (root == NULL) return; for (struct node *p = root->leftchild; p != NULL; p = p->next)
  • 6. { destroy_tree(p); } free(root); } void display_tree(struct node *root) { if (root != NULL) { for (struct node *p = root->leftchild; p != NULL; p = p->next) { printf("[%3d, %3d]n", root->id, p->id); display_tree(p); } } } Please implement the bfs(struct node *root) function and the necessary queue data structure using the C programming language to match the intended output and provide the additional functions, methods and imports used. Problem 1 [2 pt]. Attached is a C language program search.c. It has functions: - void dfs(struct node * root, int source): prints all the nodes in a tree in depth first search order. - void bfs (struct node * root, int source): prints all the nodes in a tree in breadth first search order. The bfs function doesn't work, and its present form implements dfs. Correct the bfs function, by replacing the stack with a queue. You need to define - the data structure for the queue (struct queue) - write additional functions such as create_queue destroy_queue queue_add (add a node to the queue) queue_get (get a node from the queue) queue_size and perhaps others. The output should look something like this: Display Tree [node, child] [055][[1010][[5,9][[0,3][[,8][[2,][1,7] DFS 016724835910 BFS 053211098476