Question

Creat a C Program that Reads words from a file called words, which contains one word...

Creat a C Program that Reads words from a file called words, which contains one word per line.Each word has maximum

length to M.The number of words in the file is equal to N.

Incert each word to Binary Search Tree with node name dictionary.Each node of the tree must contain one word.The left child must contain a word that it is lexicographically smaller while the right child contains a lexicographically bigger one.

1) When you finish reading the file, ask form the user to give a word as input.

2) Create a function SearchWord that search for a word and return 1 if the word is found or 0 if the word does not exist.

3) Create a function to print the words exactly as they appear in the file.

4) Create a function PrintSorted that prints all the words in a lexicographical order.

## : Choose a value for M and N.N should be greater than 10.

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

E ConsoleTasksProblems Memory ExecutablesSearch <terminated> Test [C/C++ Application] /home/uday.rathkantwar/workspaceNew/Tes

======================================================

Program name BinarySearch.c

======================================================

# include <stdio.h>
# include <stdlib.h>
int N = 50;
int M = 20;
typedef struct BST {
   char data[50];
   struct BST *lchild, *rchild;
} node;

void insert(node *, node *);
void PrintSorted(node *);
int search(node *root, char key[], node **parent);

int main() {

   int choice, res;
   char key[N], fileName[50], oneword[100];
   FILE *fp1;
   char c;

   node *new_node, *root, *parent;
   node *get_node();
   root = NULL;

   printf("Enter file Name: ");
   scanf("%s", fileName);

   fp1 = fopen(fileName, "r");

   do {
       c = fscanf(fp1, "%s", oneword); /* got one word from the file */
       new_node = get_node();

       strcpy(new_node->data, oneword);

       if (root == NULL) /* Tree is not Created */
           root = new_node;
       else
           insert(root, new_node);

   } while (c != EOF);

   printf("Data loaded successfully from file %s", fileName);
   fclose(fp1);

   printf("\n\nProgram For Binary Search Tree ");
   do {
       printf("\n\n1.Search");
       printf("\n2.PrintSorted Tree");
       printf("\n3.Exit");
       printf("\nEnter your choice :");
       scanf("%d", &choice);

       switch (choice) {

case 1:

           printf("\nEnter Element to be searched :");
           scanf("%s", key);

           res = search(root, key, &parent);
           printf("\nSearch result is %d", res);
           break;

       case 2:
           if (root == NULL)
               printf("Tree Is Not Created");
           else {
               PrintSorted(root);
           }
           break;
       default:
           printf("Invalid Choice!");

       }

   } while (choice != 3);

   return 0;
}

/*
Get new Node
*/
node *get_node() {
   node *temp;
   temp = (node *) malloc(sizeof(node));
   temp->lchild = NULL;
   temp->rchild = NULL;
   return temp;
}
/*
This function is for creating a binary search tree
*/
void insert(node *root, node *new_node) {

   if (strcmp(new_node->data, root->data) < 0) {
       if (root->lchild == NULL)
           root->lchild = new_node;
       else
           insert(root->lchild, new_node);
   }

   if (strcmp(new_node->data, root->data) > 0) {
       if (root->rchild == NULL)
           root->rchild = new_node;
       else
           insert(root->rchild, new_node);
   }
}
/*
This function is for searching the node from
binary Search Tree
*/
int search(node *root, char key[], node **parent) {

   node *temp;
   temp = root;
   while (temp != NULL) {
       if (strcmp(temp->data, key) == 0) {
           return 1;
       }
       *parent = temp;

       if (temp->data > key)
           temp = temp->lchild;
       else
           temp = temp->rchild;
   }
   return 0;
}
/*
This function displays the tree in sorted fashion
*/
void PrintSorted(node *temp) {

   if (temp != NULL) {
       PrintSorted(temp->lchild);
       printf("%s ", temp->data);
       PrintSorted(temp->rchild);
   }
}

Add a comment
Know the answer?
Add Answer to:
Creat a C Program that Reads words from a file called words, which contains one word...
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
  • Write a program, called wordcount.c, that reads one word at a time from the standard input....

    Write a program, called wordcount.c, that reads one word at a time from the standard input. It keeps track of the words read and the number of occurrences of each word. When it encounters the end of input, it prints the most frequently occurring word and its count. The following screenshot shows the program in action: adminuser@adminuser-VirtualBox~/Desktop/HW8 $ wordCount This is a sample. Is is most frequent, although punctuation and capitals are treated as part of the word. this is...

  • This is binary search tree problem. The program reads the text file, and creates a binary...

    This is binary search tree problem. The program reads the text file, and creates a binary search tree based on the words in the file. I can create the tree but I also have to store 'the order of insertion' in each node.   For example, if text includes "the" 3 times and it is the 1st, 5th, and 9th word in the file, in binary search tree, one node should have string value "the" and array list{1, 5, 9}. How...

  • Write a program that reads a series of words (one word per line) from a file...

    Write a program that reads a series of words (one word per line) from a file named data.txt. Each word in the file should have each of its characters shifted by 1 character value in the ASCII table (incremented) and then that new word with its characters shifted should be printed to a new file named result.txt. Each word from data.txt should be reprinted with its new encoding in result.txt. Your program should not have any knowledge of how many...

  • Fill a tree called Pine with 25 elements from an input file. Traverse the tree using...

    Fill a tree called Pine with 25 elements from an input file. Traverse the tree using each of the following methods. Print the smallest element in the binary search tree, Pine. Find the number of edges between the root of the tree and the node that contains the smallest value in the tree. Return the count to the calling unit. Count the number of internal nodes in the original tree, Pine. Print the count and return it to the calling...

  • 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...

  • 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...

  • C++ Vectors and Binary Search Trees • Write a program that takes from the user n...

    C++ Vectors and Binary Search Trees • Write a program that takes from the user n integers and stores them a vector of int. Then, create a function insert After that takes first Value and second Value. This function searches for each occurrence of first Value in the vector and insert the second Value after it in the same vector. The first and second values are taken from the user. • Create another function that creates a Binary Search Tree...

  • In C language This program reads in a series of words. All words consist of only...

    In C language This program reads in a series of words. All words consist of only lower-case letters ('a' through 'z'). None of the words entered will be longer than 50 letters. The program reads until ctrl-d (end-of-file), and then prints out all the lower-case letters that were missing in the input or a statement indicating the input has all the letters. Two executions of the program are shown below. Enter your input: the quick brown fox jumps over the...

  • (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...

  • General Requirements . . . Write a program that reads letters from a file called "letters.txt"....

    General Requirements . . . Write a program that reads letters from a file called "letters.txt". Your program will open the letters.txt file read in one character at a time For this assignment the test file will contain at least 30 letters, uppercase OR lowercase In your program you will change each letter (solution) to uppercase Use the function toupper in #include <ctype.h> You create a numerical version of the uppercase letter from the file . o Use int numberSolution...

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