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

If someone could get this C-program to run and read this input that would be gre

ID: 653686 • Letter: I

Question

If someone could get this C-program to run and read this input that would be great! The functions below have comments that explain what needs to be performed. Basically it needs to read the following input text as a binary search tree and present the data's mean, min, man, pre-order, post-order, etc.

Input text:

50 21 33 65 8 4 78 23 12 43 87 97 1 9 34 56 79 100 27 38 49 76 63 91 73

#include
#include
#include


//Define a binary search tree node to include
struct tree_node {
   int data;
   struct tree_node *left_child;
   struct tree_node *right_child;
};

void readDataFile();
struct tree *insert(struct tree *,int);
void inorder(struct tree *);
void postorder(struct tree *);
void preorder(struct tree *);
struct tree *delet(struct tree *,int);
struct tree *search(struct tree *);


int main(void)
{

readDataFile();
printf("Processed input and wrote output()");
return 0;

struct tree *root;
int choice, item,item_no;
root = NULL;
clrscr();
/* rear = NULL;*/
do
{
do
{
printf(" 1. Generate Binary Search Tree ");
printf(" 2. Print the BST in pre-order format ");
printf(" 3. Print the BST in in-order format ");
printf(" 4. Print the BST in post-order format ");
printf(" 5. Print the BST in breadth-first format ");
printf(" 6. Find a value in the BST ");
printf(" 7. Find the minimum value in the BST nodes ");
printf(" 8. Find the maximum value in the BST nodes");
printf(" 9. Calculate the sum of the BST nodes");
printf(" 10. Find the median value of the BST nodes");
printf(" 11. Calculate the sum of the BST nodes");
printf(" 12. Count the number of BST nodes");
printf(" 13. Delete a value in the BST");
printf(" 14. Exit Program");


scanf(" %d",&choice);
if(choice<1 || choice>7)
printf(" Invalid choice - try again");
}while (choice<1 || choice>14);
switch(choice)
{
case 1:
   printf(" Preorder traversal of binary tree is : ");
   scanf("%d", &item);
   root= insert(root,item);
   printf(" root is %d",root->info);
   printf(" Inorder traversal of binary tree is : ");
   inorder(root);
   break;
case 2:
   printf(" Inorder traversal of binary tree is : ");
   scanf(" %d",&item_no);
   root=delet(root,item_no);
   inorder(root);
   break;
case 3:
   printf(" Postorder traversal of binary tree is : ");
   inorder(root);
   break;
case 4:
   printf(" Display the elements of the BST in breadth-first format ");
   postorder(root);
   break;
case 5:
   printf(" Search the BST for a value that exists ");
   preorder(root);
   break;
case 6:
   printf(" Search the BST for a value that does not exist ");
   root=search(root);
   break;
case 7:
   printf(" Display the minimum value of the BST ");
   root=search(root);
   break;
case 8:
   printf(" Display the maximum value of the BST ");
   root=search(root);
   break;
case 9:
   printf(" Average value of BST ");
   root=search(root);
   break;
case 10:
   printf(" Medium Value of BST ");
   root=search(root);
   break;
case 11:
   printf(" Sum of the BST nodes ");
   root=search(root);
   break;
case 12:
   printf(" Number of BST nodes ");
   root=search(root);
   break;
case 13:
   printf(" Enter the element to be deleted : ");
   root=search(root);
   break;
case 14:
   printf(" Exiting Program ");
   root=search(root);
   break;  
default:
   printf(" End of program ");
} /* end of switch */
}while(choice !=7);
return(0);
}

//Function to read the data file as nodes of a binary search tree
//Read the contents of data file into the node data structure and generate
//binary search tree in the order the data is in the file
void readDataFile()
{
   const LENGTH = 50;
   char data[LENGTH];
   char *fileName = "AssignmentFourInput.txt";
   FILE *filePointer;
  
   printf("Trying to open file %s ", fileName);
   filePointer = fopen(fileName, "r"); // read mode
   //printf("Successfully opened file %s ", fileName);
  
   if(filePointer == NULL)
   {
       perror("Error while opening the file ");
       exit(0);
   }

   while( fgets(data, LENGTH, filePointer) != NULL)
   {
       //printf("Permutating %s ", data);   
       RecursivePermute(data,0);
   }

   fclose(filePointer);
}

//Function to display the BST in pre-order format
void preorder (struct tree_node *p) {
   if (p != NULL) {
       printf("%d ", p->data);
       preorder(p->left_child);
       preoredr(p->right_child);
   }
}

//Function to display the BST in in-order format
void inorder (struct tree_node *p) {
   if (p != NULL) {
       inorder(p->left_child);
       printf("%d ", p->data);
       inorder(p->right_child);
   }
}

//Function to display the BST in post-order format
void postorder (struct tree_node *p) {
   if (p != NULL) {
       postorder(p->left_child);
       postorder(p->right_child);
       printf("%d ", p->data);
   }
}

//Function to find a value in the BST
int find (struct tree_node *current_ptr, int val) {
   //Check if there are nodes in the tree:
   if (current_ptr != NULL) {
       //Found the value at the root
       if (current_ptr->data == val)
           return 1;
       //Search to the left
       if (val < current_ptr->data)
           return find(current_ptr->left, val);
       //Or...search to the right
       else
           return find(current_ptr->right, val);
   }
   else
       return 0;
}

//Function to find the minmum value in the BST
//Traverse the BST and search for the minimum value in the BST
nt minValue(struct node* node) {
struct node* current = node;

/* loop down to find the leftmost leaf */
while (current->left != NULL) {
current = current->left;
}
return(current->data);
}


//Function to find the maximum value in the BST
//Traverse the BST and search for the maximum value in the BST
int minValue(struct node* node) {
struct node* current = node;

/* loop down to find the leftmost leaf */
while (current->right != NULL) {
current = current->right;
}
return(current->data);
}

//Function to find the average value of the nodes in the BST
//Traverse the BST and calculate the average value of the BST nodes

//Function to find the median value of the nodes in the BST
//Traverse the BST and determine the median value of the BST nodes


//Function to find the sum of the values of the BST nodes
//Summing the Nodes of a Binary Search Tree
int add(struct tree_node *current_ptr) {
   if (current_ptr != NULL)
       return current_ptr->data +
       add(current_ptr->left) + add(current_ptr->right);
   else
       return 0;
}

//Function to find the count of the BST nodes

//Function to delete a node in the BST
struct tree *delet(struct tree *ptr,int x)
{
struct tree *p1,*p2;
if(!ptr)
{
printf(" Node not found ");
return(ptr);
}
else
{
if(ptr->info < x)
{
ptr->right = delet(ptr->right,x);
/*return(ptr);*/
}
else if (ptr->info >x)
{
ptr->left=delet(ptr->left,x);
return ptr;
}
else /* no. 2 else */
{
   if(ptr->info == x) /* no. 2 if */
   {
   if(ptr->left == ptr->right) /*i.e., a leaf node*/
   {
   free(ptr);
   return(NULL);
   }
   else if(ptr->left==NULL) /* a right subtree */
   {
   p1=ptr->right;
   free(ptr);
   return p1;
   }
   else if(ptr->right==NULL) /* a left subtree */
   {
   p1=ptr->left;
   free(ptr);
   return p1;
   }
   else
   {
   p1=ptr->right;
   p2=ptr->right;
   while(p1->left != NULL)
   p1=p1->left;
   p1->left=ptr->left;
   free(ptr);
   return p2;
   }
}/*end of no. 2 if */
}/* end of no. 2 else */
/* check which path to search for a given no. */
}
return(ptr);
}

Explanation / Answer

#include #include #include #include struct tree { int info; struct tree *left; struct tree *right; }; struct tree *insert(struct tree *,int); void inorder(struct tree *); void postorder(struct tree *); void preorder(struct tree *); struct tree *delet(struct tree *,int); struct tree *search(struct tree *); int main(void) { struct tree *root; int choice, item,item_no; root = NULL; clrscr(); do { do { printf(" 1. Insert in Binary Tree "); printf(" 2. Delete from Binary Tree "); printf(" 3. Inorder traversal of Binary tree"); printf(" 4. Postorder traversal of Binary tree"); printf(" 5. Preorder traversal of Binary tree"); printf(" 6. Search and replace "); printf(" 7. Exit "); printf(" Enter choice : "); scanf(" %d",&choice); if(choice7) printf(" Invalid choice - try again"); }while (choice7); switch(choice) { case 1: printf(" Enter new element: "); scanf("%d", &item); root= insert(root,item); printf(" root is %d",root->info); printf(" Inorder traversal of binary tree is : "); inorder(root); break; case 2: printf(" Enter the element to be deleted : "); scanf(" %d",&item_no); root=delet(root,item_no); inorder(root); break; case 3: printf(" Inorder traversal of binary tree is : "); inorder(root); break; case 4: printf(" Postorder traversal of binary tree is : "); postorder(root); break; case 5: printf(" Preorder traversal of binary tree is : "); preorder(root); break; case 6: printf(" Search and replace operation in binary tree "); root=search(root); break; default: printf(" End of program "); } /* end of switch */ }while(choice !=7); return(0); } struct tree *insert(struct tree *root, int x) { if(!root) { root=(struct tree*)malloc(sizeof(struct tree)); root->info = x; root->left = NULL; root->right = NULL; return(root); } if(root->info > x) root->left = insert(root->left,x); else { if(root->info right = insert(root->right,x); } return(root); } void inorder(struct tree *root) { if(root != NULL) { inorder(root->left); printf(" %d",root->info); inorder(root->right); } return; } void postorder(struct tree *root) { if(root != NULL) { postorder(root->left); postorder(root->right); printf(" %d",root->info); } return; } void preorder(struct tree *root) { if(root != NULL) { printf(" %d",root->info); preorder(root->left); preorder(root->right); } return; } /* FUNCTION TO DELETE A NODE FROM A BINARY TREE */ struct tree *delet(struct tree *ptr,int x) { struct tree *p1,*p2; if(!ptr) { printf(" Node not found "); return(ptr); } else { if(ptr->info right = delet(ptr->right,x); /*return(ptr);*/ } else if (ptr->info >x) { ptr->left=delet(ptr->left,x); return ptr; } else /* no. 2 else */ { if(ptr->info == x) /* no. 2 if */ { if(ptr->left == ptr->right) /*i.e., a leaf node*/ { free(ptr); return(NULL); } else if(ptr->left==NULL) /* a right subtree */ { p1=ptr->right; free(ptr); return p1; } else if(ptr->right==NULL) /* a left subtree */ { p1=ptr->left; free(ptr); return p1; } else { p1=ptr->right; p2=ptr->right; while(p1->left != NULL) p1=p1->left; p1->left=ptr->left; free(ptr); return p2; } }/*end of no. 2 if */ }/* end of no. 2 else */ /* check which path to search for a given no. */ } return(ptr); } /* function to search and replace an element in the binary tree */ struct tree *search(struct tree *root) { int no,i,ino; struct tree *ptr; ptr=root; printf(" Enter the element to be searched :"); scanf(" %d",&no); fflush(stdin); while(ptr) { if(no>ptr->info) ptr=ptr->right; else if(noinfo) ptr=ptr->left; else break; } if(ptr) { printf(" Element %d which was searched is found and is = %d",no,ptr->info); printf(" Do you want replace it, press 1 for yes : "); scanf(" %d",&i); if(i==1) { printf(" Enter new element :"); scanf(" %d",&ino); ptr->info=ino; } else printf(" It's okay"); } else printf(" Element %d does not exist in the binary tree",no); return(root); }
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote