Re: Tree traversal without recursion!
"Milind Mehendale" <[email protected]>
| Newsgroups | gmane.comp.programming.cppug |
|---|---|
| Message-ID | <[email protected]> |
Hi todor
here is the code for recursive tree traversal,
//////////////////////////////////////////
void preorder(struct tree *p)
{
if(p!=NULL)
{
printf(" %d ", p->data);
preorder(p->left);
preorder(p->right);
}
}
//////////////////////////////////////////
void postorder(struct tree *p)
{
if(p!=NULL)
{
postorder(p->left);
postorder(p->right);
printf(" %d ", p->data);
}
}
//////////////////////////////////////////
void inorder(struct tree *p)
{
if(p!=NULL)
{
inorder(p->left);
printf(" %d ", p->data);
inorder(p->right);
}
}
//////////////////////////////////////////
structure would be,
struct tree
{
int data;
struct tree *left;
struct tree *right;
};
//////////////////////////////////////////
you can create binary tree as follows
void create(struct tree *p, int i)
{
if (i > p->data)
{
if (p->right == NULL)
{
p->right= (struct tree*) malloc (sizeof(struct tree));
p=p->right;
p->data=i;
p->left=NULL;
p->right=NULL;
}
else
{
create(p->right, i);
}
}
else if (i<p->data)
{
if (p->left == NULL)
{
p->left= (struct tree*) malloc (sizeof(struct tree));
p=p->left;
p->data=i;
p->left=NULL;
p->right=NULL;
}
else
{
create(p->left, i);
}
}
}
Regards
Milind
--- In [email protected], Todor Kostadinov
<tkostadinov_1999@y...> wrote:
> I would like to know this with recursion please.
>
> Thanks
> T. J. K.
>
> --- gupta_nkl <gupta_nkl@y...> schrieb: > Please
> let me know, how to implement inorder,
> > postorder, breath first
> > tree traversal without recursion.
> >
> > Thanks,
> > Nikhil
> >
> >
> >
>
> __________________________________________________________________
>
> Gesendet von Yahoo! Mail - http://mail.yahoo.de
> Logos und Klingeltî¥ fÈ¡ndy bei http://sms.yahoo.de
------------------------ Yahoo! Groups Sponsor ---------------------~-->
Buy Ink Cartridges or Refill Kits for your HP, Epson, Canon or Lexmark
Printer at MyInks.com. Free s/h on orders $50 or more to the US & Canada.
http://www.c1tracking.com/l.asp?cid=5511
http://us.click.yahoo.com/mOAaAA/3exGAA/qnsNAA/EbFolB/TM
---------------------------------------------------------------------~->
To unsubscribe from this group, send an email to:
[email protected]
Your use of Yahoo! Groups is subject to http://docs.yahoo.com/info/terms/