Sunteți pe pagina 1din 21

Structura de arbore

nivelul 1 A

nivelul 2 B C D

nivelul 3 E F G H I J

nivelul 4 K L M N O

nivelul 5 P
Traversarea arborilor
Preordine: R, A1, A2..Ak
Inordine: A1, R, A2..Ak
Postordine: A1, A2..Ak, R,

A1 A2 AK
Implementarea arborilor
cu ajutorul tipului tablou
typedef struct nod
{ tipcheie Cheie; 1
... info; a
int parinte; 3
int cheie; 2 c
b
};
struct nod A[maxnod]; 10
4 5 9
d e i j

6 f h 8
Reprezentare prin 1
g
indicator spre parinte 7
1 2 3 4 5 6 7 8 9 10

parinte 0 1 1 2 2 5 5 5 3 3

cheie a b c d e f g h i j
Implementarea arborilor
cu ajutorul listelor
struct _nodArb{
int cheie; 4 ...info
...info;
struct _listaPFii *lf;
};
struct _listaPFii{
struct _nodArb *fiu;
struct _listaPFii 7 ...info 8 ...info 9 ...info
*urm;
};

13 ...info 14 ...info
Implementarea arborilor
cu ajutorul listelor
4
info
NULL

7 8 9
info info info
NULL NULL NULL

13 14
info info
NULL NULL
Arbori binari - implementarea
arborilor binari cu ajutorul tablourilor
typedef struct nod *
{
tipcheie cheie; + -
int sting, drept;
};
a / d *
struct nod t[n];

CHEIE STING DREPT


1 * 2 3
b c e f
2 + 6 4
3 - 9 5
4 / 7 8
5 * 10 11 (a+(b/c))*(d-(e*f))
6 a 0 0
7 b 0 0
8 c 0 0
9 d 0 0
10 e 0 0
11 f 0 0
Transformarea unei structuri de arbore
oarecare ntr-o structur de arbore binar
A1 a

a). arborii iniiali


A11 b

A12 c
A1 e

A1k f g d
A11 A12 A1k
a
h
i
b c d
j
e f g h i j b). arborii rezultai
Implementarea arborilor binari
cu ajutorul pointerilor
typedef struct Nod radacina
{
*
tipcheie Cheie;
nod *st,*dr;
};
+ -

a / d *

b c e f
Traversarea arborilor binari
void preordine (Nod *r) void postordine (Nod *r)
{ {
if (r!=NULL) if (r!=NULL)
{ {
prelucrarea_nod(r); postordine(r->st);
preordine(r->st); postordine(r->dr);
preordine(r->dr); prelucrarea_nod(r);
} }
} } radacina
void inordine (Nod *r) *
{
if (r!=NULL) + -
{
inordine(r->st);
a / d *
prelucrarea_nod(r);
inordine(r->dr);
} b c e f
}
Arbori binari ordonai. Arbori
binari de inaltime minima
5 7

3 8 2 8

2 4 7 9 1 6 9

1 6 3
5

4
Cautarea intr-un arbore
binar ordonat
struct Nod *Loc(int x, struct Nod *t)

{
int Gasit=0;
while (t!=NULL && !(Gasit) )
{
if (t->Cheie==x)
Gasit=1;
else
if (x< t->Cheie)
t=t->st;
else
t=t->dr;
}
return t;
}
Cautarea in arbori binari cu fanion
struct nod *f;
struct nod *loc1(int x, struct nod *t) t
{f->cheie=x; *
while (t->cheie!=x)
if (x<t->cheie) t=t->st;
else t=t->dr; + -
return t;
}
a / d *

b e

Nodul fanion
null null
Insertia unui nod intr-un
arbore binar ordonat (1)

3 8

2 4 7 9

1 6 8
Insertia unui nod intr-un
arbore binar ordonat (2)
void inarbior(int x, struct Nod **t) struct nod *radacina;
{ if ((*t)!=NULL) void main()
if (x<(*t)->Cheie) {
Inarbior(x,&((*t)->st)); int c;
else radacina=NULL;
Inarbior(x,&((*t)->dr)); scanf("%d",&c);
else while (c!=0)
{ {
(*t) = (struct Nod *) inarbior(c,&radacina);
malloc(sizeof(struct Nod)); scanf("%d",&c);
(*t)->Cheie=x; }
(*t)->st=NULL; }
(*t)->dr=NULL;
}
}
Tehnica suprimrii unui nod
dintr-un arbore binar ordonat (1)
q = p;
if (q->dr==NULL) p=q->st;
else if (q->st==NULL) p=q->dr;

P Q P P Q

X Q
X X

P=NULL
Tehnica suprimrii unui nod
dintr-un arbore binar ordonat (2)
void Supred(struct Nod **r,
struct Nod *q) nodul de
X suprimat
{ if (((*r)->dr)!=NULL)
Supred(&((*r)->dr),q);
else
{
q->Cheie=(*r)->Cheie;
q=(*r);
(*r)=(*r)->st;
printf("Nodul a fost suprimat\n");
}
}
Y predecesorul
Tehnica suprimrii unui nod
dintr-un arbore binar ordonat (3)
void Suprimare(int x, struct Nod **p)
{
struct Nod *q;
if ((*p)==NULL) printf("Nodul nu se gaseste");
else
if (x<(*p)->Cheie) Suprimare(x,&((*p)->st));
else
if (x>(*p)->Cheie) Suprimare(x,&((*p)->dr));
else //s-a gasit
{
q=(*p);
if (q->dr==NULL) (*p)=q->st;
else
if (q->st==NULL) (*p)=q->dr;
else Supred(&(q->st),q);
}
}
Problema concordantei (1)
void imprarbore(struct cuvint *r)
#include <stdio.h>
{
#include <stdlib.h> if (r!=f)
if (r!=NULL)
struct cuvint {
{ imprarbore(r->sting);
int cheie; printf("%d\t%d\n",r->cheie,
int numar; r->numar);
struct cuvint *sting, *drept; imprarbore(r->drept);
}; }
}
struct cuvint *radacina, *f;
int cuv;
Problema concordantei (2)
void cauta(int x, struct cuvint **p)
{
if ((*p)==NULL) {cuvntul nu se gsete, deci inserie}
{
(*p)=(struct cuvint *) malloc(sizeof(struct cuvint));
(*p)->cheie=x;
(*p)->numar=1;
(*p)->sting=NULL;
(*p)->drept=NULL;
}
else
if (x<(*p)->cheie) cauta(x,&((*p)->sting));
else if (x>(*p)->cheie) cauta(x, &((*p)->drept));
else (*p)->numar++;
}
Problema concordantei (3)
void cauta1(int x,struct cuvint **p)
{
if (x<(*p)->cheie) cauta1(x, &((*p)->sting));
else if (x>(*p)->cheie)
cauta1(x, &((*p)->drept));
else if ((*p)!=f) (*p)->numar++;
else
{
(*p)=(struct cuvint *)
malloc(sizeof(struct cuvint));
(*p)->cheie=x;(*p)->numar=1;
(*p)->sting=f;
(*p)->drept=f;
}
}
Problema concordantei (4)
void main()
{
radacina=NULL;
radacina=(struct cuvint *) malloc(sizeof(struct cuvint));
f=radacina;
f->sting=NULL;
f->drept=NULL;
scanf("%d",&cuv);
while (cuv!=0)
{
f->cheie=cuv;
cauta(cuv,&radacina);
cauta1(cuv,&radacina);
scanf("%d",&cuv);
}
imprarbore(radacina);
}

S-ar putea să vă placă și