How to traverse a tree by linklist using recursion in C++??

I want to show out put from the below code.but i don't understand how to do this by using recursion..I just need to know where i need to use recursion and how...


#include<iostream>
#include<stdio.h>
#include<conio.h>

using namespace std;

struct Tnode
      {
      char info;
      Tnode *l;
      Tnode *r;
      };
struct Qnode
      {
      Tnode *data;
      Qnode *neQ;
      };
struct Snode
      {
      Tnode *data;
      Snode *neS;
      };

typedef struct Tnode *T;
T root,R,P,L,pt;
typedef struct Qnode *Q;
Q start,q;
typedef struct Snode *S;
S st,hd;

class tree
     {
      public:
      tree();
      void create();
      void push(T);
      T pop();
      void put(T);
      T get();
      void traverse();
     };

tree :: tree()
     {
      root=NULL;
      start=NULL;
     }
void tree :: put(T leaf)
     {
      Q qt;
      qt=new Qnode;
      qt->data=leaf;
      qt->neQ=NULL;
      if(start==NULL)
       {
        start=qt;
        q=qt;
       }
      else
       {
        q->neQ=qt;
        q=qt;
       }
     }
T tree :: get()
     {
      T leaf;
      leaf=new Tnode;
      leaf=start->data;
      start=start->neQ;
      return leaf;
     }
void tree :: create()
     {
      char ch;

      cout<<"\nEnter root : ";
      cin>>ch;

      P=new Tnode;
      P->l=NULL;
      P->r=NULL;
      P->info=ch;

      root=P;
      put(P);

      while(start!=NULL)
       {
        P=get();
        cout<<"\nEnter left child : ";
        cin>>ch;
        if(ch!='0')
         {
          L=new Tnode;
          L->l=NULL;
          L->r=NULL;
          L->info=ch;
          put(L);
          P->l=L;
         }
        cout<<"\nEnter right child : ";
        cin>>ch;
        if(ch!='0')
         {
          R=new Tnode;
          R->info=ch;
          R->l=NULL;
          R->r=NULL;
          put(R);
          P->r=R;
         }
       }
     }

void tree :: traverse()
{

}
void tree :: push(T leaf)
     {
       st=new Snode;
       st->data=leaf;
       st->neS=hd;
       hd=st;
     }
T tree :: pop()
     {
       T leaf;
       leaf=new Tnode;
       leaf=hd->data;
       hd=hd->neS;
       return leaf;
     }

int main()
     {
       tree tr;

       tr.create();
       tr.traverse();

       return 0;
     }
main.cpp
Ashad OpuStudentAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

jkrCommented:
Basically, you'd create a 'traverse()' function that takes a root node (not only the root node of your tree) that serves as a startuing point for your recursion. In that function, you'd traverse along the paths in your tree by calling the function recursively whenever a valid node is found, e.g.

 void tree::traverse(T* node)
{
  if (node->l != NULL) traverse(node->l);

  if (node->r != NULL) traverse(node->r);
}

 void tree::traverse() // call this one from your 'main()'
{
  traverse(root);
}

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Programming Languages-Other

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.