Code:
#include <iostream.h>
#include <math.h>
#include <stdlib.h>
#include <fstream.h>
#include <string.h>
You should be using the newer headers as follows:
Code:
#include <iostream>
#include <cmath>
#include <cstdlib>
#include <fstream>
#include <cstring>
using namespace std;
Code:
struct node
{
char wordFromFile [20];
node *left;
node *right;
treeNodeNew(char newWord[]);
};
What is the purpose of treeNodeNew? Is it meant to be a constructor of some sort? Where is the implementation?
Code:
struct node* allNode (char data[20])
{
struct node* node = new(struct node);
node->wordFromFile[20] = data[20];
node->left = NULL;
node->right = NULL;
return (node);
};
The line above in red is problematic. You need to use strcpy here. I.e. strcpy(node->wordFromFile,data) or better yet make use of a string container instead of a character array.
Code:
struct node* insert(struct node* node, char data[20])
{
if (node == NULL)
{
return(allNode(data));
}
else
{
if (data <= node->wordFromFile)
node->left = insert(node->left, data);
else
node->right = insert(node->right, data);
return(node);
}
}
You can't compare character arrays this way, you can with string containers but not character arrays. If using character arrays, you must use the strcmp function.
Code:
int lookup(struct node* node, int target);
int main()
{
node newNode;
char word [20];
ifstream inFile1 ("text1.txt");
if (!inFile1)
{
cout<<"Error opening file!"<<endl;
exit (1);
}
while (!inFile1.eof())
{
inFile1>>word;
strcpy (newNode.wordFromFile, word);
}
inFile1.close();
return 0;
}
Instead of declaring a node instance newNode, you should declare a pointer and initialize it to NULL. Also, you don't want to use eof as a test condition in your while loop. A better way would likely be while( inFile1 >> word ) followed by your call to the insert function within the body of the loop.
Code:
int lookup(struct node* allNode, char target[20])
{
if (allNode == NULL)
return (false);
else
{
if (target == allNode->wordFromFile)
return (true);
else
{
if (target < allNode->wordFromFile)
return (lookup(allNode->left, target));
else
return (lookup(allNode->right, target));
}
}
}
Again, using character arrays you cannot test for equality/less-than using the == and < operators like you could if you were using string objects. You must use strcmp here instead.