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();}
        }
}

i think i know what went wrong and i revised it and kept getting this warning:
assignment makes pointer from integer without a cast in line 64: root = *line_array[j]; can anyone help me out??thankx.

this is my new code:

#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <unistd.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;
    ElementType a;
    root = NULL;
    static const char filename[] = "text.txt";
    const char *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)
        {
            // Trim the newline character
            line[strcspn(line, "\n")] = '\0';
            // Stop processing if line_array is full
            if (i < sizeof line_array / sizeof *line_array)
            {
                line_array[i++] = strdup(line);
            }
            else
            {
                break;
            }
        }
        fclose(file);

    for (j=0; j<i; j++){
        root = *line_array[j];
    }

    while(1){
        printf("\n1. Insert\n2. Print In Order\n3.Exit\nEnter Your Choice : ");
        scanf("%d",&ch);
        switch(ch){
            case 1:
            printf("Enter an element : ");
            scanf("%s",a);
            root = insert(a, root);
            break;
            case 2:
            printinorder(root);
            break;
            case 3:
            exit(0);
            default:
            printf("Invalid Choice");}
            getch();}
        }
    }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.