hi i'm having trouble in sorting the text file in inorder traversal can anyone help me out?
code:
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
struct TreeNode;
typedef struct TreeNode *node;
typedef char ElementType;
struct TreeNode{
char element;
node left, right;
}
*root;
node insert(ElementType x,node t){
if(t==NULL){
t = malloc(sizeof(node));
t->element = x;
t->left = t->right = NULL;
}
else{
if(x < t->element)t->left = insert(x, t->left);
else if(x > t->element)t->right = insert(x, t->right);
}
return t;
}
void printinorder(node T){
if(T != NULL){
printinorder(T->left);
printf("%s ", T->element);
printinorder(T->right);
}
}
int main(){
int ch;
root = NULL;
static const char filename[] = "text.txt";
ElementType *line_array[1000];
char line[1024];
int i = 0;
int j = 0;
FILE *file = fopen(filename, "r");
if (file != NULL)
{
while (fgets(line, sizeof line, file) != NULL)
{
line[strcspn(line, "\n")] = '\0';
if (i < sizeof line_array / sizeof *line_array)
{
line_array[i++] = _strdup(line);
}
else
{
break;
}
}
fclose(file);
while(1){
printf("\n1. Insert\n2. Print In Order\n3.Exit\nEnter Your Choice : ");
scanf("%d",&ch);
switch(ch){
case 1:
root = insert(line_array, root);
break;
case 2:
printinorder(root);
break;
case 3:
exit(0);
default:
printf("Invalid Choice");}
getch();}
}
}