ok i've got my program to work but i need to add a functon that would be able to delete a node from the tree and give an error message when the user is trying to delete a node that isn't already present,i also have to take into account wether the node to be deleted has one,two,or no children
can anyone help?
this is my program:
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define WORD_SIZE 30
/*
program to read a set of words from a file, store them in a binary
search tree and then preform an in-order traversal of the tree
during traversal contents of nodes are displayed - word and count
assumes potential duplicates in original file; only writes one copy
back
*/
typedef struct Treenode
{
char word[WORD_SIZE];
int count;
struct Treenode *left;
struct Treenode *right;
} Treenode;
Treenode* new_node(Treenode*, char*);
void inorder(Treenode*);
void to_file_preorder(Treenode*, FILE*);
void menu();
void check_choice(Treenode* tree, char);
int main()
{
char str[30];
FILE* fptr;
int check =0;
char ch;
Treenode* root =NULL;
if ( (fptr = fopen("test.txt","r")) == NULL)
{
fprintf(stderr, "Error in opening file test.txt");
return(0);
}
while(fscanf(fptr, "%s", str) != EOF)
root = new_node(root, str);
menu();
scanf(" %c", &ch);
while ((ch != 'e') && (ch != 'E'))
{
check_choice(root, ch);
menu();
scanf(" %c", &ch);
}
fclose(fptr);
if ( (fptr = fopen("test.txt","w")) == NULL)
{
fprintf(stderr, "Error in opening file test.txt for writing");
return(0);
}
to_file_preorder(root, fptr);
return(1);
}
Treenode* new_node(Treenode* root, char word[30])
{
if(root == NULL)
{
root = (Treenode *) malloc(sizeof(Treenode));
strcpy(root->word, word);
root->left = NULL;
root->right = NULL;
root->count = 1;
}
else
{
if((strcmp(word,root->word))==0)
{
root->count++;
}
else if((strcmp(word,root->word)) < 0)
{
root->left = new_node(root->left, word);
}
else if((strcmp(word,root->word)) > 0)
{
root->right = new_node(root->right, word);
}
}
return(root);
}
void search(Treenode *root, char *term)
{
int l;
if (root == NULL)
{
printf("Term not found\n");
return;
}
else if ((l = strcasecmp (root->word, term)) == 0)
{
printf("Term found in tree\n");
return;
}
else if (l > 0)
search(root->left, term);
else
search(root->right, term);
}
void inorder(Treenode* root)
{
if(root != NULL)
{
inorder(root->left);
printf("\n %s %d",root->word, root->count);
inorder(root->right);
}
}
void to_file_preorder(Treenode *root, FILE *fptr)
{
if (root != NULL)
{
to_file_preorder(root->left, fptr);
fprintf(fptr, "%s ",root->word);
to_file_preorder(root->right, fptr);
}
}
void menu()
{
printf("\n\tExit program (e)\n");
printf("\tDisplay tree (d)\n");
printf("\tSearch tree for string (s)\n");
printf("\tAdd new string to tree (a)\n");
return;
}
void check_choice(Treenode *tree, char choice)
{
char term[WORD_SIZE];
switch(choice)
{
case 'e' : printf("Exiting ...\n");
break;
case 'd' : inorder(tree);
break;
case 's' : printf("Please enter search term\n");
scanf ("%s", term);
search(tree, term);
break;
case 'a' : printf("Please enter term to be added\n");
scanf ("%s", term);
tree = new_node(tree, term);
break;
}
}