this works fine but i was wondering if there a way to recreate my insert function without returning any thing. for ex right now in my insert function iam return a function. but i want to recreate it so that its
void insert(char *li)
here is my code
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#define COMMAND_ARG 3
#define IN_FILE_ARG 1
#define OUT_FILE_ARG 2
#define SSIZE 16
struct tree_node
{
char line[SSIZE];
struct tree_node *left_child;
struct tree_node *right_child;
};
struct tree_node *head;
/* Prototypes for the functions */
struct tree_node* insert(struct tree_node* ,char *);
int max_string(struct tree_node *);
int max_height(struct tree_node *);
int max_leaves(struct tree_node *);
/* argc(how many command entered-output(3)) */
/* argv(point to 1st char-output(point to p)) */
int main(int argc, char *argv[])
{
int count0 = 0;
int count1 = 0;
int count2 = 0;
int count3 = 0;
int count4 = 0;
int count5 = 0;
int count6 = 0;
char line[SSIZE];
FILE *finp;
FILE *foutp;
/*** Check for 3 commands ***/
if(argc != COMMAND_ARG)
{
fprintf(stderr, "Error - Enter three commands.\n");
exit(1);
}
/*** Open and read input file ***/
if((finp = fopen(argv[IN_FILE_ARG], "r")) == NULL)
{
fprintf(stderr, "Could not open file: %s.\n", argv[IN_FILE_ARG]);
exit(1);
}
/*** Open and write output file ***/
if((foutp = fopen(argv[OUT_FILE_ARG], "w")) == NULL)
{
fprintf(stderr, "Could not open file: %s.\n", argv[OUT_FILE_ARG]);
exit(1);
}
/* Read lines from input file */
while(fscanf(finp, "%s", line) != EOF)
{
head = insert(head, line);
}
/*** Call funct and store in counts ***/
count0 = max_string(head); //String
count1 = max_height(head); //Height
count2 = max_leaves(head); //Leaves
count3 = max_height(head->left_child); //Left leave
count4 = max_string(head->left_child); //Left string
count5 = max_height(head->right_child); //Righ leave
count6 = max_string(head->right_child); //Right string
/* If height is negitve number than change to zero */
if(count1 <= 0) { count1 = 0; }
if(count3 <= 0) { count3 = 0; }
if(count5 <= 0) { count5 = 0; }
/*** Write in ouput file ***/
fprintf(foutp,"Total number of strings in the input file: %d\n", count0);
fprintf(foutp,"The height of the binary search tree: %d\n", count1);
fprintf(foutp,"The number of leaves in the binary search tree: %d\n", count2);
fprintf(foutp,"The height of the left subtree of the root: %d\n", count3);
fprintf(foutp,"The number of strings in the left subtree of the root: %d\n",count4);
fprintf(foutp,"The height of the right subtree of the root: %d\n", count5);
fprintf(foutp,"The number of string in the right subtree of the root: %d\n",count6);
/* Close input file */
if(fclose(finp) == EOF)
{
fprintf(stderr, "Error in colsing file %s.\n", argv[IN_FILE_ARG]);
}
/* Close output file */
if(fclose(foutp) == EOF)
{
fprintf(stderr, "Error in closing file %s.\n", argv[OUT_FILE_ARG]);
}
return 0;
}
struct tree_node * insert(struct tree_node *head, char *li)
{
if(head == NULL)
{
head = (struct tree *)malloc(sizeof(struct tree_node));
if(head == NULL)
{
printf("Node allocation failed.\n"); fflush(stdout);
exit(1);
}
strcpy(head->line, li);
head->left_child = NULL;
head->right_child = NULL;
return head;
}
else
{
if(strcmp(head->line, li) < 0)
{
head->right_child = insert(head->right_child, li);
}
else
{
head->left_child = insert(head->left_child, li);
}
}
}
/* total number of string in the input file */
int max_string(struct tree_node *head)
{
if(head == NULL)
{
return 0;
}
else if(head->left_child = NULL && head->right_child == NULL)
{
return 1;
}
else
{
//add 1 for head/root
return (max_string(head->left_child)) +
(max_string(head->right_child)) + 1;
}
}
/* height of the binary search tree */
int maz_string(struct tree_node *head)
{
int left = 0;
int right = 0;
if(head == NULL)
{
return -1;
}
else
{
left = max_height(head->left_child);
right = max_height(head->right_child);
}
//find biggest numner and return it to main
if(left > right)
{
return left + 1;
}
else
{
return right + 1;
}
}
/* number of leaves in the binary search tree */
int max_leaves(struct tree_node *head)
{
if(head == NULL)
{
return 0;
}
else if(head->left_child = NULL && head->right_child == NULL)
{
return 1;
}
else
{
return (max_string(head->left_child)) +
(max_string(head->right_child));
}
}