Non-Linear Data Structure 549
if(ele<pstr->number)
pstr=pstr->ls;
else pstr=*pstr->rs;
}
*lc= 0 , *p_r=p_save;
return 0;
ins_item(int ele)
{ struct b t r e e temp, * father, ^position;
search (ele, &father,&position);
if(positionlsO)
{
puts ("Item already e x ist ");
return 0;
}
tecopa (struct b tree *) malloc (sizeof (struct b tree));
temp- >nuDDber»ele;
tenp->l0;
tenp->rs=0;
if(fa th e rs s O )
r_node*tenp;
else
i f (ele< father->nunber)
father->ls» temp;
else
father->rs» temp;
return 0;
}
dal_itern (int ele)
{
struct b t r e e * father, ^position;
int a (struct b_tree*, struct b tree * );
int b(struct b tree*,struct b tree * );
int c (struct b_tree*, struct b tree * );
int d (struct b tree*, struct b tree *) ;
i f (r_node=*=0)
{
printf ("Tree is empty") ;
return 0;
>
550 Programming and Data Structures
search (ole,&father,&position);
if(positian=*0)
{
prin tf ("Item dose not exist in tree") ;
return 0;
}
i f (position->ls=*0 && position- >rs=*0)
a (father, position);
i f (position- >lsI«0 && position->rs=«0)
b (father, position);
i f (position->ls*«0 && position->rs! »0)
b(father,position);
i f (position->ls 1*0 && position->rsl-0)
c(father,position) ;
free (position);
return 0;
}
a (struct b tree *p_r, struct b tree *l_c )
{
if(p _r»«0 ) r_node-0;
else if(l_c*ep_r->ls)
p _r->ls»0;
else p_r->rs«0;
return 0;
}
b (struct b tree *p_r, struct b tree *1 c)
struct b_tree *child;
i f (l_c->ls!»0) child»l_c->ls;
else child=l_c->rs;
i f (p_r«»0 ) r_node»child;
else
i f ( l_c««p_r-> ls)
Pjr->ls«child;
else p_r->rs»child;
return 0;
c (struct b tree ♦p r, struct b tree *1 c)
{
struct b tree *pstr,*p save,*sue,*parsue;
Non-Linear Data Structure 551
p_save=l_c;
pstr=l_c->rs;
while(pstr->lsI=0)
{
psave=ps tr ;
petr«pstr->ls;
}
sue=*pstr;
par sucapsave ;
if (suc->ls«*0 && suc->r0BoO)
a (parsue, sue);
else b (pareuc, sue);
i f (p _r«0) r_node=suc;
else i f (l_c=x*p_r->ls)
p_r->ls=suc;
else p_r->rs=sue;
sue- >ls*l_c- >ls;
sue - >rs»l_c - >rs;
return 0;
}
pre order (struct b tree *pstr)
{
if(r_node==0)
{
printf ("Tree is empty") j
return 0;
}
if(pstrl=0)
{
printf ( "%d ", pstr- >nunober);
preorder(pstr->ls);
pre order (pstr->rs);
}
return 0;
)
in order (struct b tree *pstr)
{
if(rnode==0)
{
printf ("Tree is empty") ;
return 0;
552 Programming and Data Structures
>
if(pstr1=0)
{
in order(pstr->ls);
printf("%d ",pstr->number);
in order (pstr- > rs);
}
return 0;
post order (struct b tree pstr)
{
if(r_node=»0)
{
printf ("Tree is empty");
return 0;
}
if(p str 1=0)
{
postorde r(pstr->ls);
postorder(pstr->rs);
printf ("%d " fpstr->nunber);
}
return 0;
}
show (struct b tr e e *pstr,int l v e l )
{
int j=0;
i f ( pstr 1=0 )
{
show(pstr->rs, l+ l_ v e l);
printf ("n") ;
while (j < l v e l )
{
p r in t f(" " );
++j;
}
p rin tf("%d", pstr->number);
show(pstr->ls, l+ l_v e l);
}
return 0;
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.137.212.124