Ello, I'm doing a program and I've run into an error. The program details as follows: I'm making at BST that stores the name of a person, and their favorite game. Functions are add to tree, remove from tree, print tree, etc. The command (ADD, REMOVE, etc.) and the person's name / favorite game is read in from an input file. Everything works fine, except after it stops adding and maybe wants to print or remove something from the tree, if it tries to add another node to the list it doesn't actually show up when printing it. Any help would be nice, if you need any missing functions just ask.
Here is the sample input file:
Code:
ADD KAREN MONOPOLY
ADD MARK SCRABBLE
ADD JOHN MONOPOLY
ADD SASHA WAR
ADD COURTNEY SCRABBLE
PRINT
REMOVE SASHA
ADD DAVID MONOPOLY
PRINTGAME MONOPOLY
PART2
Here is some relevant code:
Code:
struct person
{
char name[20];
char game[20];
};
struct gameTree
{
struct person people;
struct gameTree * left;
struct gameTree * right;
};
Code:
struct gameTree * searchTree = NULL;
struct gameTree * tempNode = NULL;
char command[20], name[20], game[20];
while(1)
{
fscanf(ifp, "%s", command);
if(strcmp(command, "PART2") == 0)
{
printf("Ended Part 1\n");
break;
}
else if(strcmp(command, "ADD") == 0)
{
printf("Entered Add\n");
fscanf(ifp, "%s", name);
fscanf(ifp, "%s", game);
tempNode = createNode(name, game);
searchTree = insertNode(searchTree, tempNode);
}
else if(strcmp(command, "REMOVE") == 0)
{
printf("Entered Remove\n");
fscanf(ifp, "%s", name);
searchTree = deleteNode(searchTree, name);
}
else if(strcmp(command, "PRINTGAME") == 0)
{
printf("Entered Print Game\n");
fscanf(ifp, "%s", game);
fprintf(ofp, "%s:\n", game);
printGame(searchTree, game, ofp);
}
else if(strcmp(command, "PRINT") == 0)
{
printf("Entered Print\n");
fprintf(ofp, "PRINT:\n");
printTree(searchTree, ofp);
fprintf(ofp, "\n");
}
}
return 0;
Code:
struct gameTree * createNode(char name[], char game[])
{
struct gameTree * temp = (struct gameTree *)malloc(sizeof(struct gameTree));
strcpy(temp->people.name, name);
strcpy(temp->people.game, game);
temp->left = NULL;
temp->right = NULL;
return temp;
}
Code:
struct gameTree * insertNode(struct gameTree * root, struct gameTree * element)
{
if(root == NULL)
{
return element;
}
else
{
if(strcmp(element->people.name, root->people.name) > 0)
{
root->right = insertNode(root->right, element);
}
else if(strcmp(element->people.name, root->people.name) < 0)
{
root->left = insertNode(root->left, element);
}
return root;
}
}
Here is the correct output (left) compared to mine (right)
Code:
PRINT: PRINT:
COURTNEY SCRABBLE COURTNEY SCRABBLE
JOHN MONOPOLY JOHN MONOPOLY
KAREN MONOPOLY KAREN MONOPOLY
MARK SCRABBLE MARK SCRABBLE
SASHA WAR SASHA WAR
MONOPOLY: MONOPOLY:
DAVID JOHN
JOHN KAREN
KAREN