Question: Having trouble getting the insert and main function for the following program to work correctly: Here's the main code: #include #include struct bst{ int number;

Having trouble getting the insert and main function for the following program to work correctly:

Having trouble getting the insert and main function for the following program

to work correctly: Here's the main code: #include #include struct bst{ int

Here's the main code:

#include #include

struct bst{ int number; struct bst *l; struct bst *r; };

void delete1(); void insert(struct bst ** tree,int val); void delete(); void inorder(struct bst *t); void search(struct bst *t);

void preorder(struct bst *t); void postorder(struct bst *t); void search1(struct bst *t,int data); int smallest(struct bst *t); int largest(struct bst *t);

void main() { char buffer1[26]; char ch[1000]; char buffer2[120]; int n; struct bst *root=NULL; struct bst *temp=NULL; struct bst *t2; struct bst *t1;

printf("s = search for a number in the tree "); printf("d = delete a number "); printf("X = display information about the tree "); printf("Q = quit program "); printf("? = help message ");

while(ch[0]!='Q') { fflush(stdin); printf("Enter your choice: "); fgets(buffer1,26,stdin); sscanf(buffer1,"%c",ch);

if(1==sscanf(ch,"%d",&n)) { //fflush(stdin); //fgets(buffer2,120,stdin); //sscanf(buffer2,"%d",&n); insert(&root,n); }

if(ch[0]=='d') { delete(); }

if(ch[0]=='X') { inorder(root); preorder(root); postorder(root); }

if(ch[0]=='?') { printf("s = search for a number in the tree "); printf("d = delete a number "); printf("X = display information about the tree "); printf("Q = quit program "); printf("? = help message "); }

if(ch[0]=='Q') { return; } else { printf("Please enter a valid input."); } } }

Here's the insert function:

#include #include

struct bst{ int number; struct bst *l; struct bst *r; };

void insert(struct bst ** tree,int val) { struct bst *temp=NULL; if(!(*tree)) { temp=(struct bst *)malloc(sizeof(struct bst)); temp->l=temp->r=NULL; temp->number=val; *tree=temp; return; } if(valnumber) { insert(&(*tree)->l,val); } else if(val>(*tree)->number) { insert(&(*tree)->r,val); } }

I have the other functions as well, but getting the insert function correct seems like a good start. Although, I would appreciate help with the rest of the program as well. Thank you.

1 Introduction This assignment deals with binary search trees (BSTs). In this assignment, you'll create a program that lets a user enter data, and builds a BST from that data. Your program will also let the user interact with that BST, using a series of commands: s number-this will search for number" in the BST. Tell the user whether that number is found or not. d number - search for "number" and, if found, delete it. If it is not found tell the user * X - this command is used to display information about the BST. The following information should be displayed: pre-, in- and post-order traversal of the tree. For each one, output the string "NLR" "LNR:" or "LRN:" followed by a commaseparated list of the nodes. Output one line for each traversal - output data from a breatdh-first traversal of the tree: Output the string "BFS:" followed by a comma-separated list of the nodes - output the height of the tree (an empty tree has height -1; a tree with a single node has height 0; etc.) Your output should say "HEIGHT:" followed by the height. - Indicate whether or not the tree is balanced. Your output should say "BALANCED:" followed by "YES" or "NO' ? - display a help message giving the user a list of valid commands. Q- exit the program If only a number is entered (positive, negative, or 0), insert it into the tree. Only allow a number to be entered once. If the user tries to insert a number that's already in the tree, you should print an error message and not insert the second copy of the number. Typing anything else should print an error message, and tell the user how to get help 2 Details Do not balance the tree! Insert nodes only below leafs, in the unique location that preserves the property of being a BST. When deleting a node N, select the largest node from N's left sub-tree to replace N, unless there is no left Be sure to free all memory that you allocate. Use valgrind on your code to ensure there are no memory leaks. Use your queue from PA3 for implementing breadth-first search (BFS). You should code your insert, search, delete, height, balance-check and depthfirst-search routines recursively, as discussed in class. Your breadth- first search should be implemented using a queue. Create a makefile to build your executable image (which should be called "pa4"). Bundle all your source files and your makefile into a single .tar and upload via Canvas by the due date

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!