Question

In: Computer Science

In programming C language, write a program that creates a binary tree of words to be...

In programming C language, write a program that creates a binary tree of words to be used as a spell
checking device for various text files. The list of words will come from a file “words.txt”.
Your program is to read through the “words.txt” file and insert the word on that line into the tree
in lexicographic order (also known as Dictionary order). Words in the file will be separated by
spaces. Once this is done, your program should then prompt the user to enter a text file and
then scan through each word in the file to check spelling.
Your program must be able to insert words into and delete words from the tree. It must also be
able to search the tree for a given string (this is how the spell check is done), and print the tree
in pre order, in order, and post order.


The functions that do this should look as follows:


● void insert(root, s): node x char* -> none
○ inserts a node whose data is s into the tree with root node root
● void delete(root, s): node x char* -> none
○ deletes a node whose data is s from the tree with root node root
● void print_preorder(root): node -> none
○ prints the tree with root node root in pre-order format
● void print_inorder(root): node -> none
○ prints the tree with root node root in in-order format
● void print_postorder(root): node -> none
○ prints the tree with root node root in post-order format
● node search(root, s): node x char* -> node
○ searches a tree with root node root for a node whose data is s, if it is found then
that node is returned, otherwise a NULL node is returned


For the spell-checking portion of the program, there should be a function spellcheck which takes
as arguments a root node for your tree, and a string for a filename. Spellcheck should scan the
file at that location word by word and check if that word is in the tree, ignoring capitalization and
punctuation (apostrophes are not considered to be punctuation). If the word is not found then a
message should be printed to the console indicating that word is not found / misspelled. The
message should indicate the word’s position in the file.


● void spellcheck(root, filename): node x char* -> none
○ Scans a text file (filename) word by word, prints a message to the console if a
word is not found in the tree of correct words.


Sample Input/Output
words.txt: “all work and no play makes a dull boy”
myInput.txt: “All work and no play maks Jack a dull boy.”
Output:
“Word “maks” on line 1, word 6 mispelled or not found in dictionary.”
“Word “Jack” on line 1, word 7 mispelled or not found in dictionary.”

Again, in programming C language please.

Solutions

Expert Solution

Code written in C language...

#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>
#pragma warning(disable : 4996)   // for visual studio

#define Max_Len 100 // defines maximum length of the word

typedef struct _node* node;
struct _node    // node structure
{
   char* val;
   node right; // pointer to right child
   node left; // pointer to left child
};

node new_node() // creating new node
{
   node n = (node)malloc(sizeof(struct _node));
   n->val = NULL;
   n->right = NULL;
   n->left = NULL;
   return n;
}
// Function below processes the word ( punctuation removal and conversion to lowercase )
void process_word(char* word)
{
   int len = 0;
   while (word[len] != '\0')
       len++;
   for (int i = 0; (i < len) && (word[i] != '\0'); i++)
{
       if (ispunct(word[i])) // checking and removing punctuations
      {
           for (int j = i; j < len; j++)
               word[j] = word[j + 1];
       }
       if (isupper(word[i])) // conversion to lowercase
           word[i] = 'a' + word[i] - 'A';
   }
}

// Function declarations...
void insert(node root, char* s); // fun to insert a node having s value into the tree with root node root

void delete(node root, char* s); // fun to delete the node with s value from the tree with root node root

void disp_preorder(node root); // prints the tree in pre_order format

void disp_inorder(node root); // prints the tree in in_order format

void disp_postorder(node root); // prints the tree in post_order format

node search(node root, char* s); // searches for the node having value s

void spellcheck(node root, char* filename); // checks every word of the file wether it is present in the tree of correct words or not

int main()
{
   node dictionary = create_node();
  
   FILE* file;
   file = fopen("words.txt", "r"); // open file in read mode
   char word[Max_Len]; // buffer to store word read from file
  
   while (!feof(file)) // reading each word from file
   {
       fscanf(file, "%s", word);
       process_word(word);
       insert(dictionary, word);
   }
   fclose(file); // closing the file after reading

   printf("Enter file name : ");
   char file_name[Max_Len];
   scanf("%s", file_name);
   spellcheck(dictionary, file_name);

   while (dictionary != NULL) // to delete the tree remove every node
   {
       delete(&dictionary, dictionary->val);
   }
   return 0;
}

// Function definitions

void insert(node root, char* s)
{
   if (root->val == NULL)
   {
       // Dynamic memory allocation
       root->val = (char*)malloc(sizeof(char) * strlen(s) + 1);
       strcpy(root->val, s);
   }
   else
   {
    
       if (strcmp(root->val, s) > 0)   // compare curreent node data
       {
           if (root->left == NULL) // add left child if its NULL
               root->left = create_node();
           insert(root->left, s);
       }
       else
      {
           if (root->right == NULL) // add right child if its NULL
               root->right = create_node();
           insert(root->right, s);
       }
   }
}

// *NOTE delete function must pass node by referance
void delete(node* root, char* s)
{
   if ((*root) == NULL)
       return;
  
   if (strcmp((*root)->val, s) == 0)
{
       free((*root)->val);
       node temp = (*root);
       if ((*root)->left == NULL)
       {
           if ((*root)->right == NULL)   
               *root = NULL; //It's a leaf node just remove the node
           else
               *root = (*root)->right; //make right node as current node
       }
       else if ((*root)->right == NULL)
      {
           *root = (*root)->left; // set right node at the current node
       }
       else
      {
           /* current node have both childen , then make right node as current node and attach left node to the left of right node */
           *root = (*root)->right;
           node leftmost = (*root)->left;
           if (leftmost == NULL)
               (*root)->left = temp->left;
           else
           {
               while (leftmost->left != NULL) // left most node of current node
                   leftmost = leftmost->left;
               leftmost->left = temp->left; // add deleted node's left to left most node of current node
           }
       }
       free(temp);
   }
   else
{
       if (strcmp((*root)->val, s) > 0)
           // **note that value to delete function must pass by pointer refrence
           delete(&((*root)->left), s);
       else
           delete(&((*root)->right), s); // go to right child
   }
}
void disp_preorder(node root)
{
   if (root == NULL)
{
       // Null tree
   }
   else
   {
       // print current node data before child nodes ( pre_order )
       printf("%s ", root->val);
       disp_preorder(root->left);
       disp_preorder(root->right);
   }
}

void disp_inorder(node root)
{
   if (root == NULL)
{
       // Pring nothing (NULL)
   }
   else
{
        // print current node data in between child nodes
       disp_inorder(root->left);
       printf("%s ", root->val);
       disp_inorder(root->right);
   }
}

void disp_postorder(node root)
{
   if (root == NULL)
{
       // print nothing (NULL)
   }
   else
   {
         // print current node data after child nodes
       disp_postorder(root->left);
       disp_postorder(root->right);
       printf("%s ", root->val);
   }
}

node search(node root, char* s)
{
   if (root == NULL)
       return NULL;

   if (strcmp(root->val, s) == 0)
       return root;
  
   else
   {
       if (strcmp(root->val, s) > 0)
           return search(root->left, s);
       else
           return search(root->right, s);
   }
}

void spellcheck(node root, char* filename)
{
   FILE* file = NULL;
   file = fopen(filename, "r"); // open file in read mode

   if (file == NULL)
       printf("Can not open file: %s\n", filename);
  
   else
   {
       char word[Max_Len]; // buffer to read words from file
       char copy[Max_Len];
       int l_num = 1; // line number
       int w_num = 1; // word number
       while (!feof(file)) // reading each word of file
       {
           fscanf(file, "%s", word);
           strcpy(copy, word);
           process_word(copy);
           /* search the word in dictionary if seach results NULL then word is not present in the dictionary */
           if (search(root, copy) == NULL)
          {
               printf("Word \"%s\" on line %d, word %d mispelled or not found in dictionary.\n", word, l_num, w_num);
           }
           if (getc(file) == '\n') // checking for new line
          {
               l_num++;
               w_num = 0;
           }
           w_num++;
       }
       fclose(file);
   }
}

/*
words.txt
    all work and no play makes a dull boy

inputfile.txt
    All work and no play maks Jack a dull boy.
*/

Output:

Enter file name : inputfile.txt

Word "marks" on line 1, word 6 mispelled or not found in dictionary.

Word "Jack" on line 1, word 7 mispelled or not found in dictionary.


Related Solutions

In programming C language, write a program that creates a binary tree of words to be...
In programming C language, write a program that creates a binary tree of words to be used as a spell checking device for various text files. The list of words will come from a file “words.txt”. Your program is to read through the “words.txt” file and insert the word on that line into the tree in lexicographic order (also known as Dictionary order). Words in the file will be separated by spaces. Once this is done, your program should then...
Write a program to create a tree randomly. You can use C++ programming language. The input...
Write a program to create a tree randomly. You can use C++ programming language. The input is the number of vertices in the tree, and the output is an adjacent list of the tree. (Managed to complete this assignment with a binary tree. But, was told I needed a general tree instead)
C# Programming Language Write a C# program ( Console or GUI ) that prompts the user...
C# Programming Language Write a C# program ( Console or GUI ) that prompts the user to enter the three examinations ( test 1, test 2, and test 3), homework, and final project grades then calculate and display the overall grade along with a message, using the selection structure (if/else). The message is based on the following criteria: “Excellent” if the overall grade is 90 or more. “Good” if the overall grade is between 80 and 90 ( not including...
In C programming language, write the program "3x3" in size, calculating the matrix "c = a...
In C programming language, write the program "3x3" in size, calculating the matrix "c = a * b" by reading the a and b matrices from the outside and writing on the screen?
(IN C) Write the code to manage a Binary Tree. Each node in the binary tree...
(IN C) Write the code to manage a Binary Tree. Each node in the binary tree includes an integer value and string. The binary tree is sorted by the integer value. The functions include: • Insert into the binary tree. This function will take in as parameters: the root of the tree, the integer value, and the string. Note that this function requires you to create the node. • Find a node by integer value: This function takes in two...
C++ programming language. Write a program that will read in id numbers and place them in...
C++ programming language. Write a program that will read in id numbers and place them in an array.The array is dynamically allocated large enough to hold the number of id numbers given by the user. The program will then input an id and call a function to search for that id in the array. It will print whether the id is in the array or not. Sample Run: Please input the number of id numbers to be read 4 Please...
In C Programming Language Write a program to output to a text log file a new...
In C Programming Language Write a program to output to a text log file a new line starting with day time date followed by the message "SUCCESSFUL". Please screenshot the results.
in C programming Write the code to traverse the binary tree using in-order, post-order and pre-order...
in C programming Write the code to traverse the binary tree using in-order, post-order and pre-order methods. Make sure to validate that your output is correct. Review the terminology for tree structures and list structures. What is the root of the tree, what’s a parent, what’s a child, what’s an ancestor, what is an external node, what is an internal node, what is the head of the list, what is the tail of the list, etc. Traverse the binary tree...
Lab 1 Write a program in the C/C++ programming language to input and add two fractions...
Lab 1 Write a program in the C/C++ programming language to input and add two fractions each represented as a numerator and denominator. Do not use classes or structures. Print your result (which is also represented as a numerator/denominator) to standard out. If you get done early, try to simplify your result with the least common denominator. The following equation can be used to add fractions: a/b + c/d = (a*d + b*c)/(b*d) Example: 1/2 + 1/4 = ( 1(4)...
Using the C Programming language, write a program that sums an array of 50 elements. Next,...
Using the C Programming language, write a program that sums an array of 50 elements. Next, optimize the code using loop unrolling. Loop unrolling is a program transformation that reduces the number of iterations for a loop by increasing the number of elements computed on each iteration. Generate a graph of performance improvement. Tip: Figure 5.17 in the textbook provides an example of a graph depicting performance improvements associated with loop unrolling. Marking:- Optimize the code for an array of...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT