Welcome to W3Courses
 Like Us on Facebook

C++ Source Code for Creating a Tree

Source code for creating a Tree in C++

typedef struct node
{
 struct node *left;
 struct node *right;
 data item;
 node(data x)
 {
  item=x;
  left=0;
  right=0;
 }
}tree_node;
typedef tree_node *link;

void visit(link t)
{
 cout<<t->item;
}

link maketree(QUEUE<data> out)
{
 STACK<link> st(0);
 while(!out.empty())
 {
   char x=out.get();
   tree_node *t;
   t=new node(x);
   if(isalnum(x))
   {
     st.push(t);
   }
  else
  {
     t->right=st.pop();
     t->left=st.pop();
     st.push(t);
  }
 }
 return st.pop();
}

Share