Question: Please note!! This is only one question, I explained the question in the detailed formated with instructions and example, you only have to edit the
Please note!! This is only one question, I explained the question in the detailed formated with instructions and example, you only have to edit the given code to get the similar output in the example. Please answer it in step by step format. Thank you!!
Breadth-first level order traversal of a binary search tree using a queue. Write a C program that constructs a binary search tree from a list of input numbers, and then print out the binary search tree in a level order, left-to-right, traversal of the tree. We are not performing tree balancing its already balanced. For example, an input sequence of 9, 4, 7, 2, 11, 5, 15, 8, 14. Once the binary search tree is constructed, the program should print out the nodes in a level-order, left-to-right, traversal of the tree. Such a traversal of the example tree above would return the numbers in this order: 9, 4, 11, 2, 7, 15 ,5 ,8 , 14
Edit the code given below:
#include
#include
#include "../bstReverseOrder/bst.h"
#include "../queue/queue.h"
// A program to perform a LEVEL ORDER (BREADTH-FIRST) TRAVERSAL of a binary search tree
int main ( int argc, char* argv[] ) {
// READ INPUT FILE TO CREATE BINARY SEARCH TREE
FILE* fp = fopen(argv[1], "r");
if (!fp) {
perror("fopen failed");
return EXIT_FAILURE;
}
BSTNode* root = NULL;
int key;
while ( fscanf(fp, "%d", &key)!=EOF ) {
root = insert (root, key);
}
fclose(fp);
// USE A QUEUE TO PERFORM LEVEL ORDER TRAVERSAL
Queue queue = { .front=NULL, .back=NULL };
/* ... */
delete_bst(root);
return EXIT_SUCCESS;
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
