Question

I need to make it so this program outputs to an output.txt, the program works fine,...

I need to make it so this program outputs to an output.txt, the program works fine, just need it to fprintf to output.txt

#include <stdio.h>
#include <string.h>
#include <malloc.h>
#define MAX 30

struct treeNode
{
char names[MAX];
   struct treeNode *right;
struct treeNode *left;
}*node;

void searchName(char names[], struct treeNode ** parent, struct treeNode ** location)
{
struct treeNode * ptr, * tempPtr;

if(node == NULL)
   {
*location = NULL;
*parent = NULL;
return;
}

if(strcmp(names, node->names) == 0)
   {
*location = node;
*parent = NULL;
return;
}

if(strcmp(names, node->names) < 0)
ptr = node->left;
else
ptr = node->right;

tempPtr = node;

while(ptr != NULL)
   {
if(strcmp(names, ptr->names) == 0)
           {
* location = ptr;
* parent = tempPtr;
return;
}
tempPtr = ptr;

if(strcmp(names, ptr->names) <= 0)
ptr = ptr->left;
else
ptr = ptr->right;
}
*location = NULL;
*parent = tempPtr;
   }

void insName(char names[])
{
struct treeNode * parent, * location, * temp;
searchName(names, & parent, & location);
if (location != NULL)
   {
printf("This name already exists\n");
return;
}

temp = (struct treeNode*) malloc(sizeof(struct treeNode));
strcpy(temp->names, names);
temp->left = NULL;
temp->right = NULL;
if (parent == NULL)
node = temp;
else
if (strcmp(names, parent->names) <= 0)
parent->left = temp;
else
parent->right = temp;
}

void twoEmpty(struct treeNode *parent, struct treeNode *location)
{
if (parent == NULL)
node = NULL;
else
if (location == parent->left)
parent->left = NULL;
else
parent->right = NULL;
}

void oneEmpty(struct treeNode *parent, struct treeNode *location)
{
struct treeNode *child;
if (parent == NULL)
node = child;
else if (location == parent->left)
parent->left = child;
else
          parent->right = child;
   if (location->left != NULL)
   child = location->left;
   else
   child = location->right;
}

void Empty(struct treeNode *parent, struct treeNode *location)
{
struct treeNode *ptr, *prev, *next, *tempPtr;
tempPtr = location;
ptr = location->right;
while (ptr->left != NULL)
   {
tempPtr = ptr;
ptr = ptr->left;
}
next = ptr;
prev = tempPtr;
if (next->left == NULL && next->right == NULL)
twoEmpty(prev, next);
else
oneEmpty(prev, next);
if (parent == NULL)
node = next;
else
if (location == parent->left)
parent->left = next;
else
parent->right = next;
next->left = location->left;
next->right = location->right;
}

int delName(char names[])
{
struct treeNode *parent, *location;
if (node == NULL)
   {
printf("Empty tree");
return 0;
}

searchName(names, &parent, &location);
if (location == NULL)
   {
printf("\nItem does not exist");
return 0;
}
if (location->left == NULL && location->right == NULL)
twoEmpty(parent, location);
if (location->left == NULL && location->right != NULL)
oneEmpty(parent, location);
   if (location->left != NULL && location->right == NULL)
   oneEmpty(parent, location);
if (location->left != NULL && location->right != NULL)
Empty(parent, location);
printf("\n%s: deleted", location->names);
free(location);
}

void inOrder(struct treeNode *ptr)
{

   if (ptr != NULL)
{
        inOrder(ptr->left);
        printf("%s ", ptr->names);
        inOrder(ptr->right);
}
   if (node == NULL)
   {
printf("Empty Tree");
return;
}
}

int preOrder(struct treeNode * ptr)
{
if (node == NULL)
   {
printf("Empty tree");
return 0;
}
if (ptr != NULL)
   {
printf("%s ", ptr->names);
preOrder(ptr->left);
preOrder(ptr->right);
}
}


void postOrder(struct treeNode * ptr)
{

if (ptr != NULL)
   {
postOrder(ptr->left);
postOrder(ptr->right);
printf("%s ", ptr->names);
}

   if (node == NULL)
   {
printf("Empty tree");
return;
}
}
void alphabetic(struct treeNode * ptr)
{
if (ptr != NULL)
   {
alphabetic(ptr->left);
alphabetic(ptr->right);
}
}

int countBefore(struct treeNode * root, char searchKey[])
{
int count = 0;

if (root != NULL)
   {
if (strcmp(root->names, searchKey) < 0)
           {
count++;
}
count += countBefore(root->left, searchKey);
count += countBefore(root->right, searchKey);
}
return count;
}

int main()
{
FILE *fout;
fout = fopen("output.txt", "w");
char names[MAX];
int numRW;
   int numSW;
   int numDW;
   int i;
   struct treeNode *location;
node = NULL;
FILE *fin = fopen("in.txt", "r");
if (fin == NULL)
   {
fprintf(fout, "Error: Unable to open input file");
return 0;
}
fscanf(fin, "%d %d %d", &numRW, &numSW, &numDW);

for (i = 1; i <= numRW; i++)
   {
fscanf(fin, "%s", names);
insName(names);
}

printf("Pre Order: ");
preOrder(node);
printf("\nIn Order: ");
inOrder(node);
printf("\nPost Order: ");
postOrder(node);
printf("\n\nSearch Phase:");

for (i = 1; i <= numSW; i++)
   {
struct treeNode *temp = node;

fscanf(fin, "%s", names);
int flag = 0;
while (temp != NULL)
           {
if (strcmp(names, temp->names) == 0)
               {
printf("\n%s: Found, ", names);
printf("Items before: %d", countBefore(node, names));
flag = 1;
break;
}
if (strcmp(names, temp->names) <= 0)
temp = temp->left;
else
temp = temp->right;
}
if (flag == 0)
           {
printf("\n%s: Not Found, ", names);
printf("Items before: 0");
}
}

printf("\n\nDelete Phase:");
for (i = 1; i <= numDW; i++) {
fscanf(fin, "%s", names);
delName(names);
}


fprintf(fout, "\n\nPre Order: %c ");
preOrder(node);
fprintf(fout, "\nIn Order: %c");
inOrder(node);
fprintf(fout, "\nPost Order: %c");
printf("\n");
fclose(fout);
return 0;
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. Thanks

#include <stdio.h>

#include <string.h>

#include <malloc.h>

#define MAX 30

//declaring a global FILE object, inside the main method, if this file

//is initialized with stdout, then the system will print all output to the

//console, else the program will print the output to the file pointed by

//this object

FILE *fout=NULL;

struct treeNode {

    char names[MAX];

    struct treeNode* right;

    struct treeNode* left;

} * node;

void searchName(char names[], struct treeNode** parent, struct treeNode** location)

{

    struct treeNode *ptr, *tempPtr;

    if (node == NULL) {

        *location = NULL;

        *parent = NULL;

        return;

    }

    if (strcmp(names, node->names) == 0) {

        *location = node;

        *parent = NULL;

        return;

    }

    if (strcmp(names, node->names) < 0)

        ptr = node->left;

    else

        ptr = node->right;

    tempPtr = node;

    while (ptr != NULL) {

        if (strcmp(names, ptr->names) == 0) {

            *location = ptr;

            *parent = tempPtr;

            return;

        }

        tempPtr = ptr;

        if (strcmp(names, ptr->names) <= 0)

            ptr = ptr->left;

        else

            ptr = ptr->right;

    }

    *location = NULL;

    *parent = tempPtr;

}

void insName(char names[])

{

    struct treeNode *parent, *location, *temp;

    searchName(names, &parent, &location);

    if (location != NULL) {

                fprintf(fout,"This name already exists\n");    

        return;

    }

    temp = (struct treeNode*)malloc(sizeof(struct treeNode));

    strcpy(temp->names, names);

    temp->left = NULL;

    temp->right = NULL;

    if (parent == NULL)

        node = temp;

    else if (strcmp(names, parent->names) <= 0)

        parent->left = temp;

    else

        parent->right = temp;

}

void twoEmpty(struct treeNode* parent, struct treeNode* location)

{

    if (parent == NULL)

        node = NULL;

    else if (location == parent->left)

        parent->left = NULL;

    else

        parent->right = NULL;

}

void oneEmpty(struct treeNode* parent, struct treeNode* location)

{

    struct treeNode* child;

    if (parent == NULL)

        node = child;

    else if (location == parent->left)

        parent->left = child;

    else

        parent->right = child;

    if (location->left != NULL)

        child = location->left;

    else

        child = location->right;

}

void Empty(struct treeNode* parent, struct treeNode* location)

{

    struct treeNode *ptr, *prev, *next, *tempPtr;

    tempPtr = location;

    ptr = location->right;

    while (ptr->left != NULL) {

        tempPtr = ptr;

        ptr = ptr->left;

    }

    next = ptr;

    prev = tempPtr;

    if (next->left == NULL && next->right == NULL)

        twoEmpty(prev, next);

    else

        oneEmpty(prev, next);

    if (parent == NULL)

        node = next;

    else if (location == parent->left)

        parent->left = next;

    else

        parent->right = next;

    next->left = location->left;

    next->right = location->right;

}

int delName(char names[])

{

    struct treeNode *parent, *location;

    if (node == NULL) {

                fprintf(fout,"Empty tree\n");

        return 0;

    }

    searchName(names, &parent, &location);

    if (location == NULL) {

                fprintf(fout,"Item does not exist\n");

        return 0;

    }

    if (location->left == NULL && location->right == NULL)

        twoEmpty(parent, location);

    if (location->left == NULL && location->right != NULL)

        oneEmpty(parent, location);

    if (location->left != NULL && location->right == NULL)

       oneEmpty(parent, location);

    if (location->left != NULL && location->right != NULL)

        Empty(parent, location);

    fprintf(fout,"%s: deleted\n", location->names);

    free(location);

}

void inOrder(struct treeNode* ptr)

{

    if (ptr != NULL) {

        inOrder(ptr->left);

        fprintf(fout,"%s ", ptr->names);

       

        inOrder(ptr->right);

    }

    if (node == NULL) {

                fprintf(fout,"Empty Tree");

        return;

    }

}

int preOrder(struct treeNode* ptr)

{

    if (node == NULL) {

                fprintf(fout,"Empty tree");

      

        return 0;

    }

    if (ptr != NULL) {

                fprintf(fout,"%s ", ptr->names);

       

        preOrder(ptr->left);

        preOrder(ptr->right);

    }

}

void postOrder(struct treeNode* ptr)

{

    if (ptr != NULL) {

        postOrder(ptr->left);

        postOrder(ptr->right);

        fprintf(fout,"%s ", ptr->names);

    }

    if (node == NULL) {

                fprintf(fout,"Empty tree");

       

        return;

    }

}

void alphabetic(struct treeNode* ptr)

{

    if (ptr != NULL) {

        alphabetic(ptr->left);

        alphabetic(ptr->right);

    }

}

int countBefore(struct treeNode* root, char searchKey[])

{

    int count = 0;

    if (root != NULL) {

        if (strcmp(root->names, searchKey) < 0) {

            count++;

        }

        count += countBefore(root->left, searchKey);

        count += countBefore(root->right, searchKey);

    }

    return count;

}

int main()

{

    //initializing fout, so that the program will print output to that file

    fout= fopen("output.txt", "w");

    //if you want to send output to console instead of file, using below line

    //fout = stdout;

    char names[MAX];

    int numRW;

    int numSW;

    int numDW;

    int i;

    struct treeNode* location;

    node = NULL;

    FILE* fin = fopen("in.txt", "r");

    if (fin == NULL) {

        fprintf(fout, "Error: Unable to open input file");

        return 0;

    }

    fscanf(fin, "%d %d %d", &numRW, &numSW, &numDW);

    for (i = 1; i <= numRW; i++) {

        fscanf(fin, "%s", names);

        insName(names);

    }

    fprintf(fout,"Pre Order: ");

    preOrder(node);

    fprintf(fout,"\nIn Order: ");

    inOrder(node);

    fprintf(fout,"\nPost Order: ");

    postOrder(node);

    fprintf(fout,"\n\nSearch Phase:");

    for (i = 1; i <= numSW; i++) {

        struct treeNode* temp = node;

        fscanf(fin, "%s", names);

        int flag = 0;

        while (temp != NULL) {

            if (strcmp(names, temp->names) == 0) {

                fprintf(fout,"\n%s: Found, ", names);

                fprintf(fout,"Items before: %d", countBefore(node, names));

                flag = 1;

                break;

            }

            if (strcmp(names, temp->names) <= 0)

                temp = temp->left;

            else

                temp = temp->right;

        }

        if (flag == 0) {

            fprintf(fout,"\n%s: Not Found, ", names);

            fprintf(fout,"Items before: 0");

        }

    }

    fprintf(fout,"\n\nDelete Phase:");

    for (i = 1; i <= numDW; i++) {

        fscanf(fin, "%s", names);

        delName(names);

    }

    fprintf(fout,"\n");

    fclose(fout);

    return 0;

}

Add a comment
Know the answer?
Add Answer to:
I need to make it so this program outputs to an output.txt, the program works fine,...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Please I need help ASAP Java Programing: Binary Search Tree Fully implement the BST class in Listing 25.4 (on page 961 of the 11th Edition of the text). Design and write a (main) driver program to com...

    Please I need help ASAP Java Programing: Binary Search Tree Fully implement the BST class in Listing 25.4 (on page 961 of the 11th Edition of the text). Design and write a (main) driver program to completely test every method in the BST class to ensure the class meets all its requirements. You should read the Listing 25.5: TestBST.java for an idea of what your program should look like. Listing 25.4 BST.java public class BST> extends AbstractTree { protected TreeNode...

  • write a new test program called RemoveDuplicates.java. The program reads text input from keyboard or a text file and adds the words to a BST. The program then traverses the BST and prints out the word...

    write a new test program called RemoveDuplicates.java. The program reads text input from keyboard or a text file and adds the words to a BST. The program then traverses the BST and prints out the words in order (based on ASCII/UNICODE order) on the screen (or to output text file). Note that you may need to make some changes to BST.java. Sample test: ----jGRASP exec: java -ea removeDuplicates Original Text: a B 2 n w C q K l 0...

  • I am stuck on a data structure problem, I am just going off of Geeks for...

    I am stuck on a data structure problem, I am just going off of Geeks for Geeks for help but I need to print 100 random numbers in the range of [1-200]. I can currently only print 5 numbers. Here is my code: #include <stdio.h> #include <stdlib.h> #include <limits.h> using namespace std; //binary tree has data & left and right child struct node{ int data; struct node *left; struct node *right; }; //create a new node struct node* newNode (int...

  • Using C, I need help debugging this program. I have a few error messages that I'm...

    Using C, I need help debugging this program. I have a few error messages that I'm not sure how to fix. Here is the code I have: /* * C Program to Print a Linked List in Reverse Order */ #include <stdio.h> #include <stdlib.h> struct node { int num; struct node *next; }; int main() { struct node *p = NULL; struct node_occur *head = NULL; int n; printf("Enter data into the list\n"); create(&p); printf("Displaying the nodes in the list:\n");...

  • need this updated so it will delete the list and then recreate it again /***********************************************************/ /*...

    need this updated so it will delete the list and then recreate it again /***********************************************************/ /* Header files. */ /***********************************************************/ #include <stdio.h> #include <stdlib.h> #include <string.h> /***********************************************************/ /* Structure definitions. */ /***********************************************************/ struct node { int data; struct node *right; struct node *left; }; struct trash { struct node *node; struct trash *next; }; /****************************************/ /* BUILD_LIST. */ /****************************************/ void BUILD_LIST(int number2Add, struct node *(*head), struct node *(*tail)) { int i; struct node *previous, *current; *head = NULL; *tail =...

  • Linkedlist implementation in C++ The below code I have written is almost done, I only need...

    Linkedlist implementation in C++ The below code I have written is almost done, I only need help to write the definition for delete_last() functio​n. ​ Language C++ // LinkedList.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include <string> #include <iostream> using namespace std; struct Node { int dataItem;//Our link list stores integers Node *next;//this is a Node pointer that will be areference to next node in the list }; class LinkedList { private: Node *first;...

  • Question - modify the code below so that for a node, the value of every node...

    Question - modify the code below so that for a node, the value of every node of its right subtree is less the node, and the value of each node of its left subtree is greater than the node. - create such a binary tree in the Main method, and call the following method:  InOrder(Node theRoot),  PreOrder(Node theRoot),  PostOrder(Node theRoot),  FindMin(),  FindMax(),  Find(int key) using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks;...

  • ^^^ Q3. I am trying to implement double linked list but I was failed to write...

    ^^^ Q3. I am trying to implement double linked list but I was failed to write the code so anyone gives the main Code in the main function   THANK YOU FOR ADVANCE #include<stdio.h> #include<stdlib.h> #include<alloc.h> struct node {      int info;      struct node *lptr,*rptr; }; typedef struct node DL; DL *delete( ) , *insert ( ); void display(); DL *delete(DL *start,int x) {      DL *left,*right,*curr;      curr = start;      if( start == NULL)       {                 printf("\nDoubly...

  • Convert the TreeArray C++ source code(posted below) into a BinaryTree, using this TreeNode definition: class TreeNode<T>...

    Convert the TreeArray C++ source code(posted below) into a BinaryTree, using this TreeNode definition: class TreeNode<T>       T data       TreeNode<T> left       TreeNode<T> right Since this TreeNode is a generic Template, use any data file we've used this quarter to store the data in the BinaryTree. To do this will likely require writing a compare function or operator. Hint: Think LEFT if the index is calculate (2n+1) and RIGHT if index is (2n+2). Source code: #include<iostream> using namespace std;...

  • Need help for C program. Thx #include <stdio.h> #include <string.h> #include <ctype.h> // READ BEFORE YOU...

    Need help for C program. Thx #include <stdio.h> #include <string.h> #include <ctype.h> // READ BEFORE YOU START: // This homework is built on homework 06. The given program is an updated version of hw06 solution. It begins by displaying a menu to the user // with the add() function from the last homework, as well as some new options: add an actor/actress to a movie, display a list of movies for // an actor/actress, delete all movies, display all movies,...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT