0

I need to populate the bynarytree with data from a file, an .txt. How can i do it? i need emergency help. thx a lot

#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <string.h>

struct information {
    int CNP;
    char* nume;
    int varsta;
};

struct bynarytreenode {
    information info;
    bynarytreenode *left, *right;
};

bynarytreenode* createnode(information in, bynarytreenode *l, bynarytreenode *r) 
{
    bynarytreenode *temp;
    temp=(bynarytreenode*)malloc(sizeof(bynarytreenode));

    (*temp).info.CNP=in.CNP;
    temp->info.nume=(char*)malloc((strlen(in.nume)+1)*sizeof(char));
    strcpy(temp->info.nume,in.nume);
    temp->info.varsta=in.varsta;

    temp->left=l;

    temp->right=r;

    return temp;
}

bynarytreenode* insertnode(information in, bynarytreenode *root) 
{
    bynarytreenode *aux; 
    aux = root;

    if(!root) 
    { 
        aux=createnode(in,NULL,NULL);
        return aux;
    } 
    else {
        //structura repetitiva la infinit, iese prin return
        while(1)
        {
            if(in.CNP > aux->info.CNP) 
            { 
                if(aux->right) aux=aux->right;
                else 
                { 
                    aux->right=createnode(in,NULL,NULL);
                    return root;
                }               
            } 
            else 
            { 
                if(in.CNP < aux->info.CNP) 
                {
                    if (aux->left) aux=aux->left;
                    else 
                    { 
                        aux->left=createnode(in,NULL,NULL);
                        return root;
                    }                   
                } 
                else 
                {
                    printf("\n Nod existent");
                    return root;
                }
            }
        }
    }
}// end insertnode

//preordine
void printRLeftRight(bynarytreenode* root) 
{
    if (root != NULL) 
    {
        printf("\n %s are cnp %d", root->info.nume, root->info.CNP);
        printRLeftRight(root->left);
        printRLeftRight(root->right);
    }
}

//inordine
void printLeftRRight(bynarytreenode* root) 
{
    if (root != NULL) 
    {
        printLeftRRight(root->left);
        printf("\n %s are cnp %d", root->info.nume, root->info.CNP);
        printLeftRRight(root->right);
    }
}

//postordine
void printLeftRightR(bynarytreenode* root) 
{
    if (root != NULL) 
    {
        printLeftRightR(root->left);        
        printLeftRightR(root->right);
        printf("\n %s are cnp %d", root->info.nume, root->info.CNP);
    }
}

void main() 
{
    bynarytreenode* r = NULL;
    information elem;

    elem.CNP = 90;
    elem.nume = (char*) malloc((strlen("ION")+1)*sizeof(char));
    strcpy(elem.nume, "ION");
    elem.varsta = 24;
    r = insertnode(elem, r);
    free(elem.nume);

    elem.CNP = 60;
    elem.nume = (char*) malloc((strlen("Gigel")+1)*sizeof(char));
    strcpy(elem.nume, "Gigel");
    elem.varsta = 24;
    r = insertnode(elem, r);
    free(elem.nume);

    elem.CNP = 80;
    elem.nume = (char*) malloc((strlen("Maricica")+1)*sizeof(char));
    strcpy(elem.nume, "Maricica");
    elem.varsta = 24;
    r = insertnode(elem, r);
    free(elem.nume);

    printLeftRRight(r);

    getch();
}

Edited by mike_2000_17: Fixed formatting

2
Contributors
1
Reply
2
Views
7 Years
Discussion Span
Last Post by daviddoria
0

Welcome to DaniWeb! I have a few tips to get you started.

1) While we understand that English is not everyone's first language, you must still use proper English words. For example "plz" and "thx" are not proper words.
2) You must use code tags when posting code. It makes it MUCH easier for us to read.
3) stdio.h, stdlib.h, string.h, etc are deprecated. You should use #include <cstdio> etc instead.
4) You should break the problem down in a very specific question that we can answer. Try to separate the code into pieces and test each piece. When a piece fails, you should provide the input, current output, and expected output and we'll take a look.

Good luck!

Dave

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.