Vous êtes sur la page 1sur 6

r

Arbori binari

info

left right cod

nume

varsta

punem din structura (student elem) in informatia din nod

elem
90

Ion

23

root

aux

info

left right

inainte de a insera, se face o creare


root aux de nod

creare
temp

info ("Ion", 90,24)

l (NULL)
r (NULL)
left left right
left
right
right
NULL NULL

temp

info("Gigel",60,24)

left right

60<90 =>trebuie sa-l plasam in stanga


aux

info("Ion",90,24)
left

info("Gigel",60,2
4)

left right

(NULL) (NULL)

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

struct student
{
int cod;
char* nume;
int varsta;
};

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

bynarytreenode* createnode(student stud, bynarytreenode *l, bynarytreenode *r)


{
bynarytreenode *temp;
temp=(bynarytreenode*)malloc(sizeof(bynarytreenode));
temp->info.cod=stud.cod;
temp->info.nume=(char*)malloc((strlen(stud.nume)+1)*sizeof(char));
strcpy(temp->info.nume,stud.nume);
temp->info.varsta=stud.varsta;
temp->left=l;
temp->right=r;
return temp;
}

bynarytreenode* insertnode(student stud, bynarytreenode *root)


{
bynarytreenode *aux;
aux = root;

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

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

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

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

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

elem.cod = 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.cod = 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.cod = 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();
}
inserare

stergere

cautare

afisare(preordine,postorine)

Vous aimerez peut-être aussi