Monday, March 18, 2013

Recursive Procedure for Tree Traversals


Type Declaration of Tree Node

struct node
{
   struct treenode *lchild;
   char data;
   struct treenode *rchild;
};

Procedure for Recursive Preorder

void rpreorder(struct node *p)
{
   if (p!=NULL)
   {
      printf("%c ",p->data);
      rpreorder(p->lchild);
      rpreorder(p->rchild);
   }
}

Procedure for Recursive Inorder

void rinorder(struct treenode *p)
{
   if (p!=NULL)
   {
      rinorder(p->lchild);
      printf("%c ",p->data);
      rinorder(p->rchild);
   }
}

Procedure for Recursive Postorder

void rpostorder(struct node *p)
{
   if (p!=NULL)
   {
      rpostorder(p->lchild);
      rpostorder(p->rchild);
      printf("%c ",p->data);
   }
}

No comments:

Post a Comment