Question

Please Only use C language In this assignment, you have to read a text file (in.txt)...

Please Only use C language

In this assignment, you have to read a text file (in.txt) that contains a set of words. The first line of the file contains 3 numbers (N, S, D). These numbers represent the sequence of input words in your file.

N: represents the number of words to read to build a binary search tree. You have to write a recursive insert code to create and insert these words into the binary search tree. After inserting all the items, you should show the words in Pre-order In order, and Post order. So, you need to create three functions for this purpose.

S: represents the number of words to search from the tree. These S-words are placed after the first N words in the input file. You need to implement a search function that will be able to search these words in your BST.

Additionally, the search words will also be used to look in the binary search tree and count number of words in the tree that come before the search word, alphabetically. You will need to create a recursive function with the following prototype CountBefore(treeNode* root, char searchKey[]), where treeNode is the node structure of your tree.

D: represents the number of words to delete from the BST. This list of words are placed after N+words in the input file. Write a recursive delete function for your task. After deleting all the items, also show the tree in three different orders of traversals.

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

Program screenshot:

Input file: in.txt

Sample output:

Code to copy:

#include <stdio.h>
#include <string.h>
#include <malloc.h>
#define MAX 20
//define a node structure of the tree
struct treeNode
{
   //declare the variable and pointers.
   char word[MAX];
   struct treeNode *leftChild;  
   struct treeNode *rightChild;
}*node;

//definition of the function searchWord()
void searchWord(char word[], struct treeNode **parent, struct treeNode **position)
{
   struct treeNode *ptr, *tempPtr;
   // Checks if root node is null
   if (node == NULL)
   {
       *position = NULL;
       *parent = NULL;
       return;
   }
   //if the word is equal to root node word
   //the word is present at root node
   if (strcmp(word, node->word) == 0)
   {
       *position = node;
       *parent = NULL;
       return;
   }
   //if word is less than the root node word
   // then make ptr to left child
   if (strcmp(word, node->word) < 0)
       ptr = node->leftChild;
   // Otherwise make ptr to left child
   else
       ptr = node->rightChild;

   tempPtr = node;
   //iterate the tree
   while (ptr != NULL)
   {
       //compare the word with tree word
       //if equal make the parent as tempPtr
       if (strcmp(word, ptr->word) == 0)
       {
           *position = ptr;
           *parent = tempPtr;
           return;
       }
       tempPtr = ptr;
       //if word is less than the root node word
       // then make ptr to left child
       if (strcmp(word, ptr->word) <= 0)
           ptr = ptr->leftChild;
       else
           // Otherwise make ptr to left child
           ptr = ptr->rightChild;
   }
   *position = NULL;
   *parent = tempPtr;
}
//definition of the function insertWord()
void insertWord(char word[])
{
   struct treeNode *parent, *position, *temp;
   //call the function searchWord to check is word is present in tree
   searchWord(word, &parent, &position);
   //if the position is not null then the word is present.
   if (position != NULL)
   {
       printf("The word already present in BST.");
       return;
   }
   //create memory for the treeNode pointer
   temp = (struct treeNode*) malloc(sizeof(struct treeNode));
   //copy the word
   strcpy(temp->word, word);
   //make left and right child of temp as null
   temp->leftChild = NULL;
   temp->rightChild = NULL;
   //if the parent is null make the root node as temp
   if (parent == NULL)
       node = temp;
   else
       //if word is less than the parent word
       // then make parent's left child as temp
       if (strcmp(word, parent->word) <= 0)
           parent->leftChild = temp;
       else
           // Otherwise make parent's right child as temp
           parent->rightChild = temp;
}
//definition of the function bothChildEmpty()
void bothChildEmpty(struct treeNode *parent, struct treeNode *position)
{
   //if the parent is null then the root node is null
   if (parent == NULL)
       node = NULL;
   else
       //if the position is at the left child
       //then make position of the left child as null
       if (position == parent->leftChild)
           parent->leftChild = NULL;
       else
           //otherwise make position of the right child as null
           parent->rightChild = NULL;
}
//definition of the function oneChildEmpty()
void oneChildEmpty(struct treeNode *parent, struct treeNode *position)
{
   struct treeNode *child;
   //if the position of the leftchild is not null
   //make the position of the leftchild as the child to be deleted.
   if (position->leftChild != NULL)
       child = position->leftChild;
   else
       //else, make the position of the rightchild as the child to be deleted.
       child = position->rightChild;
   //if parent is null, root is deleted
   if (parent == NULL)
       node = child;
   else
       //if position is left child's position
       //make parent of left child as child
       if (position == parent->leftChild)
           parent->leftChild = child;
       else
           //else, make parent of right child as child
           parent->rightChild = child;
}
//definition of the function noChildEmpty()
void noChildEmpty(struct treeNode *parent, struct treeNode *position)
{
   struct treeNode *ptr, *tempPtr, *next, *prev;
   tempPtr = position;
   ptr = position->rightChild;
   while (ptr->leftChild != NULL)
   {
       tempPtr = ptr;
       ptr = ptr->leftChild;
   }
   next = ptr;
   prev = tempPtr;
   if (next->leftChild == NULL && next->rightChild == NULL)
       bothChildEmpty(prev, next);
   else
       oneChildEmpty(prev, next);
   if (parent == NULL)
       node = next;
   else
       if (position == parent->leftChild)
           parent->leftChild = next;
       else
           parent->rightChild = next;
   next->leftChild = position->leftChild;
   next->rightChild = position->rightChild;
}
//definition of the function deleteWord()
int deleteWord(char word[])
{
   struct treeNode *parent, *position;
   if (node == NULL)
   {
       printf("Tree empty");
       return 0;
   }
   //call the function searchWord to check is word is present in tree
   searchWord(word, &parent, &position);
   if (position == NULL)
   {
       printf("Item not present in tree");
       return 0;
   }
   if (position->leftChild == NULL && position->rightChild == NULL)
       bothChildEmpty(parent, position);
   if (position->leftChild != NULL && position->rightChild == NULL)
       oneChildEmpty(parent, position);
   if (position->leftChild == NULL && position->rightChild != NULL)
       oneChildEmpty(parent, position);
   if (position->leftChild != NULL && position->rightChild != NULL)
       noChildEmpty(parent, position);
   printf("\n%s: deleted", position->word);
   free(position);
}

//definition of the function preOrder()
//it is the recursive function and prints the preorder of the tree
int preOrder(struct treeNode *ptr)
{
   if (node == NULL)
   {
       printf("Tree is empty");
       return 0;
   }// End of if condition
   if (ptr != NULL)
   {
       printf("%s ", ptr->word);
       preOrder(ptr->leftChild);
       preOrder(ptr->rightChild);
   }
}
//definition of the function inOrder()
//it is the recursive function and prints the inOrder of the tree
void inOrder(struct treeNode *ptr)
{
   if (node == NULL)
   {
       printf("Tree is empty");
       return;
   }
   if (ptr != NULL)
   {
       inOrder(ptr->leftChild);
       printf("%s ", ptr->word);
       inOrder(ptr->rightChild);
   }
}
//definition of the function postOrder()
//it is the recursive function and prints the postOrder of the tree
void postOrder(struct treeNode *ptr)
{
   if (node == NULL)
   {
       printf("Tree is empty");
       return;
   }
   if (ptr != NULL)
   {
       postOrder(ptr->leftChild);
       postOrder(ptr->rightChild);
       printf("%s ", ptr->word);
   }
}
void alphabetic(struct treeNode *ptr)
{
   if (ptr != NULL)
   {
       alphabetic(ptr->leftChild);
       alphabetic(ptr->rightChild);
   }
}
//definition of the the main
int main()
{
   //declare the variable
   char word[MAX];
   int numOfReadWords, numOfSearchWords, numOfDeleteWords, i;
   node = NULL;
   //create a struct variabl
   struct treeNode *position;
   // open the input file in read mode
   FILE *fin = fopen("in.txt", "r");
   //if the file not opened, then print an error message.
   if (fin == NULL)
   {
       // Display error message
       puts("Error: Could not open files");
       return 0;
   }
   //read the first line of the input file
   fscanf(fin, "%d %d %d", &numOfReadWords, &numOfSearchWords, &numOfDeleteWords);
   //read the number of words and build a binary search tree
   //using function insertWord()
   for (i = 1; i <= numOfReadWords; i++)
   {
       fscanf(fin, "%s", word);
       insertWord(word);
   }
   //print the pre-order, in-order, postorder
   printf("Pre Order: ");
   preOrder(node);
   printf("\nIn Order: ");
   inOrder(node);
   printf("\nPost Order: ");
   postOrder(node);
   printf("\n\nSearch Phase:");
   //search the words in the binary search tree
   for (i = 1; i <= numOfSearchWords; i++)
   {
       struct treeNode *temp = node;
       //read the word from the file
       fscanf(fin, "%s", word);
       int flag = 0;
       while (temp != NULL)
       {
           if (strcmp(word, temp->word) == 0)
           {
               printf("\n%s: Found.", word);
               flag = 1;
               break;
           }
           if (strcmp(word, temp->word) <= 0)
               temp = temp->leftChild;
           else
               temp = temp->rightChild;
       }
       if (flag == 0)
           printf("\n%s: Not Found.", word);
   }
   //delete the words from the tree
   printf("\n\nDelete Phase:");
   for (i = 1; i <= numOfDeleteWords; i++)
   {
       fscanf(fin, "%s", word);
       deleteWord(word);
   }
   //print the pre-order, in-order, postorder of the tree
   printf("\n\nPre Order: ");
   preOrder(node);
   printf("\nIn Order: ");
   inOrder(node);
   printf("\nPost Order: ");
   postOrder(node);
   printf("\n");
   return 0;
}

Add a comment
Know the answer?
Add Answer to:
Please Only use C language In this assignment, you have to read a text file (in.txt)...
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
  • Using C Please comment Part 1: BST Create a link based Binary Search tree composed of a Node and a Tree struct. You should have a header file, BST.h, with the following: o Node struct containing...

    Using C Please comment Part 1: BST Create a link based Binary Search tree composed of a Node and a Tree struct. You should have a header file, BST.h, with the following: o Node struct containing left, right, and parent pointers, in addition to holding an Data struct value Tree struct containing a pointer to the root of the tree A function declaration for a function that allocates a tree, and initializes the root to NULL o o o A...

  • (in C) Binry Srch tree problem: 1. Need to read words from a “in” text file...

    (in C) Binry Srch tree problem: 1. Need to read words from a “in” text file and build a binary search tree. (1st line will state number of elements) 2. strcmp() can be used to compare data and decide were to insert a new node. 3. The output should include: -print the tree as in-order traversal. -print the character count in the tree. -Ask user input for a word to search, print an output if either found or not. (typing...

  • Basic C program Problem: In this assignment, you have to read a text file (in.txt) that...

    Basic C program Problem: In this assignment, you have to read a text file (in.txt) that contains a set of point coordinates (x,y). The first line of the file contains the number of points (N) and then each line of the file contains x and y values that are separated by space. The value of x and y are an integer. They can be both negative and positive numbers. You have to sort those points in x-axis major order and...

  • Name the program for this assignment "bst_driver.cpp." In this assignment you will make several modifications to...

    Name the program for this assignment "bst_driver.cpp." In this assignment you will make several modifications to the BST class in the file “bst.cpp” that I provided. Consider the following: 1. Change the declaration of treenode to class treenode //node in a BST { public: string county_name; double population_size; treenode *lchild, *rchild; //left and right children pointers }; 2. Make the following changes to the BST class: a) change the name from “BST” to “bst”; b) change default constructor from “BST”...

  • Binary Search Trees (a) 5 pointsl Insert 5, 12, 7, 1, 6, 3, 13, 2, 10,...

    Binary Search Trees (a) 5 pointsl Insert 5, 12, 7, 1, 6, 3, 13, 2, 10, 11 into an empty binary search tree in the given order. Show the resulting BST after every insertion. (b) 5 points) What are the preorder, inorder, and postorder traversals of the BST you have after (a)? (c) 5 points Delete 2, 7, 5, 6, 11 from the BST you have after (a) in the given order Show the resulting BST after every deletion.

  • C++ ONLY Threaded Binary Search Tree Since a binary search tree with N nodes has N...

    C++ ONLY Threaded Binary Search Tree Since a binary search tree with N nodes has N + 1 NULL pointers, half the space allocated in a binary search tree for pointer information is wasted. Suppose that if a node has a NULL left child, we make its left child pointer link to its inorder predecessor, and if a node has a NULL right child, we make its right child pointer link to its inorder successor. This is known as a...

  • Overview: file you have to complete is WordTree.h, WordTree.cpp, main.cpp Write a program in C++ that...

    Overview: file you have to complete is WordTree.h, WordTree.cpp, main.cpp Write a program in C++ that reads an input text file and counts the occurrence of individual words in the file. You will see a binary tree to keep track of words and their counts. Project description: The program should open and read an input file (named input.txt) in turn, and build a binary search tree of the words and their counts. The words will be stored in alphabetical order...

  • Please show that the code is working at the end. Your program should read from the standard input...

    Please show that the code is working at the end. Your program should read from the standard input a sequence of integer values, with each value separated by a space. Your task is to: Build a binary search tree using these values in the order they are entered. Print 3 traversals: pre-, in-, and post-order. Allow the user to insert/delete a value. Once a new tree is generated, print it in-order. Find predecessor of a given value. The predecessor is...

  • Overview The purpose of this assignment is to practice functional programming in the Racket Programming Language...

    Overview The purpose of this assignment is to practice functional programming in the Racket Programming Language and to also reinforce the notion of the list as a universal data structure, by implementing various operations on binary search trees. Specification A binary search tree is a binary tree which satisfies the following invariant property: for any node X, every node in X's left subtree has a value smaller than that of X, and every node in X's right subtree has a...

  • please use java application to solve methods: 5,6,10,12,13 (no need for the rest) Problem1: Create a...

    please use java application to solve methods: 5,6,10,12,13 (no need for the rest) Problem1: Create a new Java Application that has the following methods: 6 A method that generate a binary search tree (BST) where the number of nodes and the range of the values are 1. from a file. 2. A recursive method to print the BST in preorder traversal 3. A recursive method to print the BST in post-order traversal 4. A recursive method to print the BST...

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