Practical 6. Program based on triesi. Create a text file containing keys{“and”, “bat”, “ ball”,“book”,“cot”, “cotton”, “internet”, “interview”, “joy”,“job”, “King”, “Lion”, “man”, “mango”, “manage”}separated by tab and then read those keys and insert themin trie and search the key which is accepted as input fromuserii. Read the keys from char array which is already given andsearch the keys which are accepted as input from useriii. Read the keys from char array and search those keys which are given in text file
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#define ALPHABET_SIZE 26
struct TrieNode {
struct TrieNode* children[ALPHABET_SIZE];
int isEndOfWord;
};
struct TrieNode* createNode() {
struct TrieNode* node = (struct TrieNode*)malloc(sizeof(struct TrieNode));
node->isEndOfWord = 0;
for (int i = 0; i < ALPHABET_SIZE; i++) {
node->children[i] = NULL;
}
return node;
}
void insert(struct TrieNode* root, const char* key) {
struct TrieNode* current = root;
while (*key) {
int index = tolower(*key) - 'a';
if (current->children[index] == NULL) {
current->children[index] = createNode();
}
current = current->children[index];
key++;
}
current->isEndOfWord = 1;
}
int search(struct TrieNode* root, const char* key) {
struct TrieNode* current = root;
while (*key) {
int index = tolower(*key) - 'a';
if (current->children[index] == NULL) {
return 0;
}
current = current->children[index];
key++;
}
return current->isEndOfWord;
}
void loadFromFile(struct TrieNode* root, const char* filename) {
FILE* file = fopen(filename, "r");
if (!file) {
printf("Error opening file.\n");
return;
}
char buffer[100];
while (fscanf(file, "%s", buffer) != EOF) {
insert(root, buffer);
}
fclose(file);
}
void searchFromArray(struct TrieNode* root, const char* keys[], int size) {
for (int i = 0; i < size; i++) {
if (search(root, keys[i])) {
printf("'%s' found in trie.\n", keys[i]);
} else {
printf("'%s' not found in trie.\n", keys[i]);
}
}
}
void searchFromTrie(struct TrieNode* root, const char* filename) {
FILE* file = fopen(filename, "r");
if (!file) {
printf("Error opening file.\n");
return;
}
char buffer[100];
while (fscanf(file, "%s", buffer) != EOF) {
if (search(root, buffer)) {
printf("'%s' found in trie.\n", buffer);
} else {
printf("'%s' not found in trie.\n", buffer);
}
}
fclose(file);
}
int main() {
struct TrieNode* root = createNode();
const char* keys[] = {"and", "bat", "ball", "book", "cot", "cotton", "internet", "interview", "joy", "job", "king", "lion", "man", "mango", "manage"};
int keySize = sizeof(keys) / sizeof(keys[0]);
for (int i = 0; i < keySize; i++) {
insert(root, keys[i]);
}
char input[100];
printf("Enter a word to search: ");
scanf("%s", input);
if (search(root, input)) {
printf("'%s' found in trie.\n", input);
} else {
printf("'%s' not found in trie.\n", input);
}
printf("\nLoading keys from file and inserting into trie.\n");
loadFromFile(root, "keys.txt");
printf("\nSearching keys from array in trie.\n");
searchFromArray(root, keys, keySize);
printf("\nSearching keys from file in trie.\n");
searchFromTrie(root, "keys.txt");
return 0;
}
Practical 6. Program based on triesi. Create a text file containing keys{“and”, “bat”, “ ball”,“book”,“cot”, “cotton”, “internet”, “interview”, “joy”,“job”, “King”, “Lion”, “man”, “mango”, “manage”}separated by tab and then read those keys and insert themin trie and search the key which is accepted as input fromuserii. Read the keys from char array which is already given andsearch the keys which are accepted as input from useriii. Read the keys from char array and search those keys which are given in text file
Reviewed by RISHI and ARYAN
on
23:35
Rating:

No comments:
May I help you ?