?
Solved

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

Posted on 2014-08-26
1
Medium Priority
?
242 Views
Last Modified: 2014-08-31
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
0
Comment
Question by:Ashad Opu
1 Comment
 
LVL 86

Accepted Solution

by:
jkr earned 1500 total points
ID: 40286001
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

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you haven’t already, I encourage you to read the first article (http://www.experts-exchange.com/articles/18680/An-Introduction-to-R-Programming-and-R-Studio.html) in my series to gain a basic foundation of R and R Studio.  You will also find the …
This article will show, step by step, how to integrate R code into a R Sweave document
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.
Suggested Courses
Course of the Month15 days, 18 hours left to enroll

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question