Code:
#include <stdio.h>
#include <stdlib.h>
typedef struct node NODE;
typedef NODE* NODEPTR;
struct node
{
char word[10];
NODEPTR Parent;
NODEPTR RightChild;
NODEPTR LeftChild;
};
NODEPTR makeNode();
NODEPTR chooseRoot(NODEPTR, NODEPTR, NODEPTR);
void makeTree(NODEPTR, NODEPTR);
void readFile();
void getWord(NODEPTR);
void destroyTree(NODEPTR, NODEPTR);
void searchTree(NODEPTR, NODEPTR);
int main()
{
readFile();
return;
}
void readFile()
{
NODEPTR rootNode = NULL;
NODEPTR tempNode1 = NULL;
NODEPTR tempNode2 = NULL;
NODEPTR tempNode3 = NULL;
NODEPTR xNode = NULL;
FILE *file;
char word[10];
file = fopen("inData.txt", "r");
if(file == NULL)
{
printf("\nError opening input file ...Program Terminating");
eixt(1); //changed this to return; and it did nothing
}
{
tempNode1 = makeNode(); //call
tempNode2 = makeNode(); //call
tempNode3 = makeNode(); //call
fscanf(file, "%s", tempNode1->word);
fscanf(file, "%s", tempNode2->word);
fscanf(file, "%s", tempNode3->word);
rootNode = chooseRoot(tempNode1, tempNode2, tempNode3); //call
}
xNode = makeNode(); //call
fscanf(file, "%s", xNode->word);
while(!feof(file))
{
makeTree(rootNode, xNode); //call
xNode = makeNode(); //call
fscanf(file, "%s", xNode->word);
}
xNode = makeNode();
getWord(xNode);
searchTree(rootNode, xNode); //call
destroyTree(rootNode, rootNode); //call
return;
}
void getWord(NODEPTR xNode)
{
char word[10];
printf("Enter the word you want to search for.\n");
scanf("%s", xNode->word);
return;
}
NODEPTR makeNode()
{
return malloc(sizeof(NODE));
}
NODEPTR chooseRoot(NODEPTR tempNode1, NODEPTR tempNode2, NODEPTR tempNode3)
{
/*if(tempNode1->word == '\0' || tempNode2->word == '\0' || tempNode3->word == '\0')
{
printf("Error, not enough data...exiting");
exit(1); //this part of the function is commented out
}*/
if((strcmp(tempNode1, tempNode2)>0 && strcmp(tempNode1, tempNode3)<0) || (strcmp(tempNode1, tempNode2)<0 && strcmp(tempNode1, tempNode3)>0))
{
makeTree(tempNode1, tempNode2); //call
makeTree(tempNode1, tempNode3); //call
return tempNode1;
}
if((strcmp(tempNode2, tempNode1)>0 && strcmp(tempNode2, tempNode3)<0) || (strcmp(tempNode2, tempNode1)<0 && strcmp(tempNode2, tempNode3)>0))
{
makeTree(tempNode2, tempNode1); //call
makeTree(tempNode2, tempNode3); //call
return tempNode2;
}
makeTree(tempNode3, tempNode1); //call
makeTree(tempNode3, tempNode2); //call
return tempNode3;
}
void makeTree(NODEPTR rootNode, NODEPTR xNode)
{
if(strcmp(xNode->word, rootNode->word) > 0 && rootNode->RightChild != NULL)
{
makeTree(rootNode->RightChild, xNode); //call
}
if(strcmp(xNode->word, rootNode->word) < 0 && rootNode->LeftChild != NULL)
{
makeTree(rootNode->LeftChild, xNode); //call
}
if(strcmp(xNode->word, rootNode->word) > 0 && rootNode->RightChild == NULL)
{
rootNode->RightChild = xNode;
xNode->Parent = rootNode; //call
}
if(strcmp(xNode->word, rootNode->word) < 0 && rootNode->LeftChild == NULL)
{
rootNode->LeftChild = xNode;
xNode->Parent = rootNode; //call
}
return;
}
void searchTree(NODEPTR rootNode, NODEPTR xNode)
{
if(strcmp(xNode->word, rootNode->word) == 0)
{
printf("Found it!\n");
return;
}
/*if(rootNode->RightChild == NULL && rootNode->LeftChild == NULL)
{
printf("Couldn't find it!\n");
return;
}*/
if(strcmp(xNode->word, rootNode->word) < 0 && rootNode->LeftChild != NULL)
{
searchTree(rootNode->LeftChild, xNode);
return;
}
if(strcmp(xNode->word, rootNode->word) > 0 && rootNode->RightChild != NULL)
{
searchTree(rootNode->RightChild, xNode);
return;
}
printf("Word was not found.");
return;
}
void destroyTree(NODEPTR rootNode, NODEPTR xNode)
{
NODEPTR temp = NULL;
char word[10];
if(rootNode->RightChild == NULL && rootNode->LeftChild == NULL)
{
free(rootNode);
return;
}
if(xNode != NULL)
{
if(xNode->LeftChild != NULL)
{
destroyTree(rootNode, xNode->LeftChild); //call
return;
}
if(xNode->RightChild != NULL)
{
destroyTree(rootNode, xNode->RightChild); //call
return;
}
if(xNode->RightChild == NULL && xNode->LeftChild == NULL)
{
temp = xNode;
xNode = xNode->Parent;
if( strcmp(temp->word, xNode->word) < 0)
{
free(temp);
xNode->LeftChild = NULL;
destroyTree(rootNode, xNode); //call
return;
}
if( strcmp(temp->word, xNode->word) > 0)
{
free(temp);
xNode->RightChild = NULL;
destroyTree(rootNode, xNode); //call
return;
}
}
}
}
It builds a tree that looks like