Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Create a C program (3 points): oAt the top of the file include the following com

ID: 3747062 • Letter: C

Question

Create a C program (3 points): oAt the top of the file include the following comments: Your name - The purpose (core concept found below) of the program The date created o Create binary search tree functions: The tree will store integers. The list of integers will be supplied by the user. . Add a node so that it is sorted as shown in lesson 9.1. - Search the tree, specifying if the value is found or not found. - Perform a preorder traversal of the tree (see lesson 9.2 for a pseudocode implementation) o Call all functions from the main function -Create a menu system with the following options: - Add an integer to the tree Display the entrire tree using a preorder traversal Search for a number Quit - Repeat the program until the user chooses to quit. Compile and run your code. ° submit your source code as a plain text file with a .c extension. Make sure it compiles without error before submitting it.

Explanation / Answer

//Anonymous

//To learn binary search tree

//14/09/18

#include <stdio.h>

#include <stdlib.h>

typedef struct tree{

int val;

struct tree* left;

struct tree* right;

struct tree* parent;

}tr;

tr* root=NULL;

void inorder(tr* pr)

{

if(pr!=NULL)

{

inorder(pr->left);

printf("%d ",pr->val);

inorder(pr->right);

}

}

void postorder(tr* pr)

{

if(pr!=NULL)

{

postorder(pr->left);

postorder(pr->right);

printf("%d ",pr->val);

}

}

void preorder(tr* pr)

{

if(pr!=NULL)

{

printf("%d ",pr->val);

preorder(pr->left);

preorder(pr->right);

}

}

int search(int n)

{

tr *temp = root;

while(temp!=NULL)

{

if(temp->val==n)

{

return 1;

}

else

{

if(n>temp->val)

{

temp=temp->right;

}

else

{

temp=temp->left;

}

}

}

return 0;

}

void insert(int n)

{

int pos;

if(root==NULL)

{

root = (tr*)malloc(sizeof(tr));

root->val=n;

root->left=NULL;

root->right=NULL;

root->parent=NULL;

return;

}

else

{

tr *temp = root;

while(1)

{

tr *temp2 = temp;

if(n>=temp->val)

{

//temp->right=temp;

//printf("insert right ");

temp=temp->right;

pos=1;

}

else if(n<temp->val)

{

//temp->left=temp;

//printf("insert left ");

temp=temp->left;

pos=2;

}

if(temp==NULL)

{

temp = (tr*)malloc(sizeof(tr));

temp->val=n;

temp->left=NULL;

temp->right=NULL;

if(pos==1)

{

temp->parent=temp2;

temp2->right=temp;

}

else

{

temp->parent=temp2;

temp2->left=temp;

}

break;

}

}

return;

}

}

int main()

{

int n=0,i,choice=1;

tr * pr;

while(1)

{

printf(" ");

printf("Enter 1 to insert a number ");

printf("Enter 2 for Preordered traversal ");

printf("Enter 3 for Number search ");

printf("Enter 0 to exit ");

printf("Enter your choice: ");

scanf(" %d",&choice);

if(choice==1)

{

int a;

scanf("%d",&a);

insert(a);

if(n==0)

{

pr = root;

}

n++;

}

else if(choice==2)

{

printf("PREORDER ");

preorder(pr);

printf(" ");

}

else if(choice==3)

{

printf("No. to be searched: ");

int no;

scanf("%d",&no);

if(search(no))

{

printf("Found ");

}

else

{

printf("Not found ");

}

}

else if(choice==0)

{

break;

}

else

{

printf("Please enter valid choice ");

}

}

return 0;

}