You are on page 1of 3

20

27

188
22

12

25

15
14

5
10
8
7

28

16

24

26

struct Nod
{
int val;
Nod*st, *dr;
};
Nod*rad=NULL;
void StergNod(Nod*&rad, int valsters)
{
if (rad!=NULL)
//daca mai sunt noduri de parcurs se cauta valoarea
if (valsters<rad->val)
StergNod(red->st, vasters);
//stergere din subarbore stang
else
if (valsters>rad->val)
StergNod(rade->dr, valsters);
//stergere din subarbore drept
else
{
if (rad->st==NULL && rad->dr==NULL)
//s-a gasit nodul de sters
//I situatie: stergere nod terminal
delete rad; // daca nu reanulam, pe parcurgere, vor fi probleme
else
if (rad->dr==NULL)
{
//nodul are doar subarbore stang
//inlocuim radacina cu subarborele stang
Nod*sters=rad;
rad=rad->st;
delete sters;
}
else
if (rad->st==NULL)
{
//nodul are doar subarbore drept
//inlocuim radacina cu subarborele sau drept
New *sters=rad;
rad=rad->dr;
delete *sters;
}
else
//nodul are ambii subarbori
//identificarea celui mai mare nod al subarborelui stang (nodul
dezalocat)
//nodul dezalocat are doar subarbore stang
{
Nod*sters=rad->st, *tatasters=rad;
while (sters->dr!=NULL)
{
if (s->dr->dr=NULL)
{tatasters=s
sters=s->dr;}
s=s->dr;
}
rad->val=sters->val;
if(rad->st==sters)

//nodul de sters logic(rad) este parintele nodului de sters


fizic (sters)
{
rad->st=sters->st;
detele sters;
}
else
//nodul de sters logic (rad) nu este tatal celui de sters fizic
(sters)
{
tatasters->dr=sters->st;
delete sters;
}
}
}
}
//echilibrarea arborilor

You might also like