You are on page 1of 3

//#include <iostream>

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

int parinte[20] = { 0, 2, 7, 5, 2, 7, 7, -1, 5, 2 };


int nr = 9;
int copii[20];

struct arb_multicai
{
int cheie;
int nr_copii;
arb_multicai **copii;
} *vect_mult[100], *rad;

struct arb_binar
{
int cheie;
arb_binar *fiu;
arb_binar *frate;
} *vect_bin[100];

void arbore_multicai()
{
//Parintele
int i, x;//x=c
for (i = 1; i <= nr; i++)
{
if (parinte[i] == -1)
{
rad = vect_mult[i];
}
else
{
vect_mult[parinte[i]]->nr_copii = vect_mult[parinte[i]]->nr_copii
+ 1;
x = vect_mult[parinte[i]]->nr_copii;
vect_mult[parinte[i]]->copii[x] = vect_mult[i];
}
}
}

void print(arb_multicai *p)


{

int i;
if (p != NULL)
{
if (p->nr_copii == 0)

printf("Nodul %d nu are copii.\n", p->cheie);


else
{
if (p->nr_copii == 1)
printf("Nodul %d are un copil : ", p->cheie);
else
printf("Nodul %d are copiii urmatori: ", p->cheie);
for (i = 1; i <= p->nr_copii; i++)

printf("%d ", p->copii[i]->cheie);

printf("\n");

for (i = 1; i <= p->nr_copii; i++)


print(p->copii[i]);
}
}
}

//copilul il pune in stanga fratele in dreapta


void arbore(arb_multicai *p)
{ //multicai - binar
int i;
if (p != NULL)
{
for (i = 1; i <= p->nr_copii; i++)
{
if (i == 1)
vect_bin[p->cheie]->fiu = vect_bin[p->copii[1]->cheie];
else
vect_bin[p->copii[i - 1]->cheie]->frate = vect_bin[p-
>copii[i]->cheie];

arbore(p->copii[i]);
}
}
}

void pretty_print(int ch, int depth)


{

printf("\n");
for (int j = 1; j <= depth; j++)
printf(" ");
printf("%d", ch);

if (vect_bin[ch]->fiu != NULL)
pretty_print(vect_bin[ch]->fiu->cheie, depth + 1);

if (vect_bin[ch]->frate != NULL)
pretty_print(vect_bin[ch]->frate->cheie, depth);

void main()
{
int i;

for (i = 1; i <= nr; i++)


{
vect_mult[i] = (arb_multicai*)malloc(sizeof(arb_multicai));
vect_mult[i]->cheie = i;
vect_mult[i]->nr_copii = 0; //nu contine
niciun fiu
vect_mult[i]->copii = (arb_multicai**)malloc(copii[i] *
sizeof(arb_multicai*));

vect_bin[i] = (arb_binar*)malloc(sizeof(arb_binar));
vect_bin[i]->cheie = i;
vect_bin[i]->fiu = vect_bin[i]->frate = NULL;
}

printf("Primul caz\n ");


printf("\n");
printf("Vector parinti : \n");

for (i = 1; i <= nr; i++)


printf("%d ", parinte[i]);
printf("\n");
arbore_multicai();
printf("\n");
printf("Al doilea caz\n");
printf("\n");
print(rad);
arbore(rad);
printf("\nAl treilea caz\n");
printf("\n");
pretty_print(rad->cheie, 0);

getchar();