540 Programming and Data Structures
prepr->ls;
else
pr*pr->rs;
}
* 1- 0;
* p - p s j
return 0;
I
insert (int ele)
{ struct nodes *t,*father,*m _loc;
search (ele, &fathar,&m_loc);
i f (m_locl«0)
{
printf ("Item already present”) ;
return 0;
}
t* (struct nodes * )malloc (sizeof (struct nodes));
t->data*ele;
t->ls«0;
tj->rs»0;
if(father»=0)
tree-t;
if(ele<father-xiata)
father->lt;
else
father->rs*=t;
return 0;
del (int ele)
{
int a (struct nodes*, struct nodes*);
int b(struct nodes*, struct nodes*);
int c (struct nodes*,struct nodes*);
struct nodes * father, *mloc ;
i f (tree»*0)
{
printf ("Tree is empty");
return 0;
}
search (ele, fcfather, &m loc) ;
Non-Linear Data Structure 541
if(mloc==0)
{
printf ("Item not exists in tree") ;
return 0;
}
i f (m_loc->ls»»0 && m_loc->rs®»0)
a (father, m loc) ;
i f (m_loc->ls l«0 && m_loc->rs**G)
b (father,m loc);
i f (m_loc->ls»*0 m loc - >rs 1=0)
b(father,m loc );
i f (m_loc->ls!*0 && m_loc->rs!=0)
c (father,m lo c);
free (m lo c);
return 0;
a (struct nodes *p, struct nodes *1 )
{
if(p=*0) treesO;
else
i f (l«=*p->ls) p->ls*0;
else p->rs»0;
return 0;
}
b (struct nodes *p, struct nodes *1)
{
struct nodes *san;
if(l-> ls !»0 ) son*l->ls;
else san**l->rs?
if(p «*0 ) tree*son;
else
i f ( l«*p -> ls) p->ls*son;
else p ->rs-so n;
return 0;
c (struct nodes *p, struct nodes *1)
{
struct nodes *pr,*p_s, *suc,*p_r;
p_s«l;
pr=l->rs;
while (pr->lsl=0)
542 Programming and Data Structures
{
p_s*pr;
pr»pr->l8;
}
suc«pr;
pjr-p_s;
i f (euc->l8««0 && suc->rs«*0)
a(p_r,suc);
else
b(p_r,suc);
i f (p*«0) treeasuc;
else
i f (lM p->ls) p->ls*suc;
else p->rsosuc;
suc->ls*l->ls;
suc->r8»l->rs;
return 0;
}
preorder (struct nodes *pr)
{
if(treesaO)
{
printf ( "Tree is empty");
return 0;
>
if(prl>0)
{
printf ("%d ",pr->data);
preorder (pr->l s);
preorder (pr->rs);
>
return 0;
}
inorder (struct nodes *pr)
{
if(tree==0)
{
printf ("Tree is enopty");
return 0;
}
if(pr!=0)
{
inorder(pr->ls);
printf ( "%d " ,pr->data) ;
inorder (pr->rs) ;
}
Non-Linear Data Structure 543
return 0;
}
postorder (struct nodes *pr)
{
if(tree==0)
{
printf ("Tree is enqpty");
return 0;
}
if(prl=0)
{
postorder(pr->ls);
postorder(pr->rs);
p rin tf( "%d " ,pr->data);
>
return 0;
1
show(struct nodes *pr,int lev)
{
int i;
i f ( pr 1 =0 )
{
show(pr->rs, lev+1);
printf ("n ");
for (i = 0; i < lev; i++)
p rin t f(" " );
printf ("%d", pr->data);
shov/(pr->ls, lev++);
}
return 0;
OUTPUT:
l.Insert
2.Delete
3.Inorder T raversal
4.Preorder Traversal
5.Postorder Traversal
6.Display
7.Exit
Enter your option: 1
Enter the number (0 to exit): 12 3 4 5 7 0
Enter your option: 3
1 2 3 4 5 7
..................Content has been hidden....................

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