Yes, I forgot to add
The complier didn't say anything about it. =/
With that been added, the preorder is working now. So this means the items are inserted into the tree. I've been using, for example:
Code:
void DoCommand(char command, List *list]
for most of the programs. Can you explain why changing DoCommand() to return something or to:
Code:
TreeNode *DoCommand(char command, TreeNode *root);
?
However, the search is still not working. When I search for the item, it still gives me null.
The current code of DoCommand with search in red:
Code:
TreeNode *DoCommand(char command, TreeNode *root)
{
TreeEntry target; // search item
TreeEntry item; //input
int flag; // records EOF condition from scanf
FILE *fp; // pointer to file to open
char file[MAXCHAR]; // file name to be opened
char c;
int i;
TreeNode *newnode, *targetnode;
switch(command)
{
/* Reading File */
case 'r':
printf("\nEnter the filename: ");
scanf("%s", file);
if((fp = fopen(file,"r")) == NULL)
Error("File failed to open.\n");
else
{
/* Read file */
newnode = (TreeNode*)malloc(sizeof(TreeNode));
while(fscanf(fp, "%s", &newnode->entry.key) != EOF)
{
if(newnode)
{
root = InsertTree(root, newnode);
printf("newnode = %s\n", newnode);
newnode = (TreeNode *)malloc(sizeof(TreeNode));
}
else
printf("memory exhausted.\n");
}
}
fclose(fp);
break;
/* Search */
case 's':
printf("Enter the name that you would like to search: ");
scanf("%s", &target.key);
targetnode = TreeSearch(root, target.key);
printf("target = %s\n", targetnode);
break;
case 'p': //preorder
printf("Preorder of list is:\n");
Preorder(root, Print);
break;
case 'i': //inorder
printf("Inorder of list is:\n");
Inorder(root, Print);
break;
case 'o': //postorder
printf("Postorder of list is:\n");
Postorder(root, Print);
break;
case 'h':
Help();
break;
case 'q':
printf("\nBinary Search Tree ended\n\n");
exit(0);
}
return root;
}
Thank you for helping!