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