Home > Software design >  Inserting nodes in a tree and display this tree
Inserting nodes in a tree and display this tree

Time:11-11

For a project, I have to insert nodes in a tree. This tree is a big one as every node can have at maximum 26 children. In order to do so I've created a structure which is the following :

typedef struct s_node
{
    char letter;
    struct s_node* f_letters[26];
    word* flechies;
}t_node;

typedef struct s_tree
{
    t_node* root;
}t_tree;

I'm also having this structure to affect the word from the .txt file :

typedef struct
{
    char content[100];
    char base_word[100];
    int nature;
    int genre;
    int nombre;
    int temps;
    int personne;
}word;

The letter store one letter and the f_letters store pointers to the next character. The letter are coming from a .txt file that looks like this :

stabilimetre    stabilimetre   Nom:Mas SG

I'm trying to put the letter from the first word (stabilimetre here) in nodes (one letter per node). I've created two functions that I use to create node and display the tree :

t_node* new_value(char letter,t_tree *Tree, t_node* leaf)
{
    t_node* n_node;
    t_node n_node1;
    n_node = &n_node1;
    leaf->f_letters[0] = n_node;
    n_node->letter = letter;
    for (int i=0; i<26; i  )
    {
        n_node->f_letters[i] = NULL;
    }
    return n_node;
}

void display_tree(t_tree t)
{
    printf("The letter in this node is %c.\n",t.root->letter);
    t_node* temp;
    temp = t.root->f_letters[0];
    printf("The letter in this node is %c.\n",temp->letter);
    while(temp->f_letters[0] != NULL)
    {
        temp = temp->f_letters[0];
        printf("The letter in this node is %c.\n",temp->letter);
    }
}

This is my main :

int main () {
    FILE *fp;
    char str[5000];
    char type[100];

    t_tree Letter;
    t_node* temp;
    t_node temp1, first;

    temp = &temp1;
    Letter.root=NULL;

    fp = fopen("dico_reducted.txt" , "r"); // Open the file in read mode "r"

    if(fp == NULL) {
        perror("Error opening file");
        return(-1); // test if the file is not empty or if there is any issue
    }

    while (fgets (str, sizeof(str), fp)!=NULL) {
        word A;
        char word;
        sscanf(str,"%s\t%s\t%s",&A.content, &A.base_word, &type); //Put every information from the text in the right spot
        Letter.root = &first;
        first.letter = 'N'; //This letter represent the type of the word (here noun)
        first.f_letters[0] = temp;
        temp->letter = A.content[0]; //The first 2 letters are putted manually and this is working

        for (int i=1; i<12; i  )
        {
            temp = new_value(A.content[i], &Letter, temp); // Here I put the following letters inside the tree
        }
    }

    display_tree(Letter); //Here I'm trying to display the whole tree (It is working for only the first 2 letters
    fclose(fp); // Close the file

    return 0;
}

The main issue is that I don't knwo which function is not working. The output I have for now is :

The letter in this node is N. //This is good
The letter in this node is s. //This is good
The letter in this node is ▲. //Why is it displaying kind of an adress and why the program is stopping ?

I've tried many things like changing part of the functions or do the display manually by some printf without using the function but the same error occured. I think that the main issue is the new_value function but I don't get were it is coming from. The final output should be :

The letter in this node is N. 
The letter in this node is s. 
The letter in this node is t.
The letter in this node is a.
The letter in this node is b.
The letter in this node is i.
The letter in this node is l.
The letter in this node is i.
The letter in this node is m.
The letter in this node is e.
The letter in this node is t.
The letter in this node is r.
The letter in this node is e.

CodePudding user response:

Replacing n_node = &n_node1; with n_node = malloc(sizeof(t_node));
solve my issue.

  • Related