Thanks so much for your help. I understood your comments and corrected my code accordingly. I also noticed i needed to change the:
Code:
struct treenode {
char *word;
int occurence;
struct treenode *left;
struct treenode *right;
};
to:
Code:
struct treenode {
char word[30];
int occurence;
struct treenode *left;
struct treenode *right;
};
and I was able to get rid of a segmentation fault error.
and in my rush I accidently added code in the wrong spot of my while loop, it should look like this:
Code:
while(x < entries){
fscanf(fp, "%s", fname);
fscanf(fp, "%s", lname);
fscanf(fp, "%s", title);
while(strncmp(over, word, 13) != 0){
fscanf(fp, "%s", word);
if(strncmp(over, word, 13) != 0){
i=0;
str = strlen(word);
while(i < str){
word[i] = tolower(word[i]);
i++;
}
temp = create_node(word);
root = insert(root, temp);
}
}
}
x++;
inorder(root);
lladd(root, fname, lname, title);
}
and not:
Code:
while(x < entries){
fscanf(fp, "%s", fname);
fscanf(fp, "%s", lname);
fscanf(fp, "%s", title);
while(strncmp(over, word, 13) != 0){
fscanf(fp, "%s", word);
if(strncmp(over, word, 13) != 0){
i=0;
str = strlen(word);
while(i < str){
word[i] = tolower(word[i]);
i++;
}
temp = create_node(word);
root = insert(root, temp);
}
}
inorder(root);
lladd(root, fname, lname, title);
x++;
}
}
Oops...
But now it seems to get lost in the while loops, and goes on forever... Please help
My new edited code looks like this:
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <malloc.h>
struct treenode {
char word[30];
int occurence;
struct treenode *left;
struct treenode *right;
};
struct summary{
char *lname;
char *fname;
char *title;
struct treenode node;
}info;
struct ll{
struct summary info;
struct ll *next;
}*List;
int lladd(struct treenode *myroot, char fname[30], char lname[30], char title[30]);
struct treenode* insert(struct treenode *root, struct treenode *temp);
struct treenode* create_node(char val[30]);
void inorder(struct treenode * current_ptr);
int main(void){
int action=0, entries=0, words=0, i=0, j=0, str = 0, x=0, m=0;
char over[30] = {'D','O','C','U','M','E','N','T','_','O','V','E','R'};
char **query, filename[30], word[30], fname[30], lname[30], title[30];
FILE *fp;
struct treenode *temp= NULL, *root = NULL;
printf("Which action would you like to perfrom?\n"
" 1) Loading a list of books into the database.\n"
" 2) Handling queries to the database.\n"
" 3) Quit the program.\n");
scanf("%d", &action);
while(action < 3){
if(action == 1){
printf("\nEnter the name of your file: ");
scanf("%s", filename);
fp = fopen(filename, "r");
if(fp == NULL){
printf("\n\nSorry, that is an invalid file\n");
return 0;
}
fscanf(fp, "%d", &entries);
printf("%d\n", entries);
while(x < entries){
fscanf(fp, "%s", fname);
fscanf(fp, "%s", lname);
fscanf(fp, "%s", title);
while(strncmp(over, word, 13) != 0){
fscanf(fp, "%s", word);
if(strncmp(over, word, 13) != 0){
i=0;
str = strlen(word);
while(i < str){
word[i] = tolower(word[i]);
i++;
}
temp = create_node(word);
root = insert(root, temp);
}
}
}
x++;
inorder(root);
lladd(root, fname, lname, title);
}
if(action ==2){
printf("\nHow many words are in your query?\n");
scanf("%d", &words);
printf("\nWhich words would you like to search for?\n");
query = (char **)malloc(words * sizeof(char *)+1);
for(i=0; i < words; i++){
j=0;
query[i] = (char *)malloc(sizeof(char));
printf("%d). ", i+1);
scanf("%s", query[i]);
str = strlen(query[i]);
while(j< str){
query[i][j] = tolower(query[i][j]);
j++;
}
printf("%s\n", query[i]); //just to test if they were read in...
if (find(root, query[i]) != 0)
printf("\n\nWORD FOUND");
}
}
//if (action ==3) program terminates
printf("Which action would you like to perfrom?\n"
" 1) Loading a list of books into the database.\n"
" 2) Handling queries to the database.\n"
" 3) Quit the program.\n");
scanf("%d", &action);
}
return 0;
}
struct treenode* insert(struct treenode *root, struct treenode *temp){
// element should be inserted to the right.
// Inserting into an empty tree.
if (root == NULL){
root = (struct treenode*)malloc(sizeof(struct treenode));
strcpy(root->word, temp->word);
root->left = NULL;
root->right = NULL;
root->occurence = 1;
return root;
}
else{
if(strcmp(root->word, temp->word) == 0){
root->occurence++;
return root;
}
// element should be inserted to the left.
// There is a right subtree to insert the node.
else if(strcmp(root->word, temp->word) > 0){
if (root->right != NULL){
root->right = insert(root->right, temp);
}
// Place the node directly to the right of root.
else{
root->right = (struct treenode*)malloc(sizeof(struct treenode));
strcpy(root->right->word, temp->word);
root->right->left = NULL;
root->right->right = NULL;
root->right->occurence = 1;
return root;
}
}
else if(strcmp(root->word, temp->word) < 0){
// There is a left subtree to insert the node.
if (root->left != NULL){
root->left = insert(root->left, temp);
}
// Place the node directly to the left of root.
else{
root->left = (struct treenode*)malloc(sizeof(struct treenode));
strcpy(root->left->word, temp->word);
root->left->left = NULL;
root->left->right = NULL;
root->left->occurence = 1;
return root;
}
}
}
}
int find(struct treenode *current_ptr, char val[]) {
// Check if there are nodes in the tree.
if (current_ptr != NULL) {
// Found the value at the root.
if (strcmp(current_ptr->word, val) == 0){
printf("\n\n%s", current_ptr->word);
return current_ptr->occurence;
}
// Search to the left.
if (strcmp(current_ptr->word, val) > 0) {
return find(current_ptr->left, val);
}
// Or...search to the right.
if(strcmp(current_ptr->word, val) < 0) {
return find(current_ptr->right, val);
}
}
else
return 0;
}
int lladd(struct treenode *myroot, char fname[30], char lname[30], char title[30])
{
int i=0;
struct ll * pNew;
pNew = (struct ll *) (malloc(sizeof(struct ll)));
pNew->info.fname = fname;
pNew->info.lname = lname;
pNew->info.title = title;
pNew->info.node = *myroot;
pNew->next = NULL;
if(List == NULL)
List = pNew;
else
{
pNew->next = List;
List = pNew ;
}
return 1;
}
struct treenode * create_node(char val[30]){
// Allocate space for the node, set the fields.
struct treenode * temp;
temp = (struct treenode*)malloc(sizeof(struct treenode));
strcpy(temp->word,val);
temp->left = NULL;
temp->right = NULL;
return temp; // Return a pointer to the created node.
}
void inorder(struct treenode * current_ptr) {
// Only traverse the node if it's not null.
if (current_ptr != NULL) {
inorder(current_ptr->left); // Go Left.
printf("%s \n", current_ptr->word); // Print the root.
inorder(current_ptr->right); // Go Right.
}
}
Thanks again for ANY and all help!