Why?
>>it does give output as expected.
What do you expect and what do you get?
#include<iostream.h>
#include<conio.h>
#include<alloc.h>
// node structure follwos. each node has 3 parts : Value or info, No. of children, an array of pointers (dynamic) to point to n child nodes
struct GTreeNode
{
int val;
int NChild;
GTreeNode **Child;
}**Root=NULL;
void CreateGeneralTree(GTreeNode**,int);
void ShowGeneralTree(GTreeNode*); //will show details of all nodes, one by one, not working fine.
void ShowGTNode(GTreeNode *R); //will dhow details of one node. ok.
void main()
{
int i, val, n;
GTreeNode *NewNode ;
clrscr();
//Create Root Node.
cout<<"\nEnter Root Val " ;
cin>>val ;
cout<<"Enter No. of Children " ;
cin>>n;
NewNode=(GTreeNode *)malloc(sizeof(GTreeNode)*n);
NewNode->val=val;
NewNode->NChild=n; //new node has n children
for(i=0;i<n;i++)
NewNode->Child[i]=(GTreeNode*)NULL; //initiall ymake them all Null
Root=&NewNode; // root points to newnode. root is root of tree.
CreateGeneralTree(Root,n); //now get details of n childern of root & their children
//ShowGeneralTree(NewNode);
getch();
}
void CreateGeneralTree(GTreeNode **r, int n)
{
int i,k, m;
char ch ;
//allocate memory for n children.
(*r)->Child=(GTreeNode**)malloc(n*sizeof(GTreeNode));
//Enter values of Child Nodes.
cout<<"\nNode Created at : "<< r<<"\n";
for(i=0;i<n;i++)
{
cout<<"\tEnter value for Child "<< i << " : ";
cin>>(*r)->Child[i]->val;
(*r)->Child[i]->NChild=0;
(*r)->Child[i]->Child=NULL;
}
ShowGTNode(**r); //chek is it ok.
cout<<"\nDo You Wish to Enter Info of Child Nodes : ";
ch=getche();
if(ch=='y' || ch=='Y')
{
for(k=0;k<n;k++)
{
cout<<"\n "<<k<<" Enter No. of Children of " << (*r)->Child[k]->val<<" : ";
cin>>m;
(*r)->Child[k]->NChild=m ;
CreateGeneralTree(&((*r)->Child[k]),m); //recurisive
}
}
}
void ShowGTNode(GTreeNode *R)
{
int i, n=R->NChild;
cout<<"\nNo. Of Children : "<<n ;
for(i=0;i<n;i++)
cout<<R->Child[i];
}
void ShowGeneralTree(GTreeNode *r)
{
int i,n;
n=r->NChild;
cout<<"\nAddress : " <<r<<"\tValue : "<<r->val<<"\tChildren : "<<n<<"\t";
for(i=0;i<n;i++)
cout<<" "<<r->Child[i]->val;
getch();
for(i=0;i<n;i++)
ShowGeneralTree(r->Child[i]);
}
(*r)->Child=(GTreeNode**)malloc(n*sizeof(GTreeNode*));
for (int i = 0; i < n; i++)
(*r)->Child[i] = (GTreeNode*)malloc(sizeof(GTreeNode));
//....
(*r)->Child[k]->NChild=m ;
#include<iostream>
#include<process.h>
using namespace std;
// node structure follwos. each node has 3 parts : Value or info, No. of children, an array of pointers (dynamic) to point to n child nodes
struct GTreeNode
{
int val;
int NChild;
GTreeNode *Child;
}*Root=NULL;
void CreateGeneralTree(GTreeNode*, int);
void ShowGeneralTree(GTreeNode*); //will show details of all nodes, one by one, not working fine.
void ShowGTNode(GTreeNode *R); //will dhow details of one node. ok.
int main()
{
int i, val, n;
GTreeNode *NewNode ;
system("cls");
//Create Root Node.
cout<<"\nEnter Root Val " ;
cin>>val ;
cout<<"Enter No. of Children " ;
cin>>n;
NewNode = new GTreeNode;
if (n > 0)
NewNode->Child = new GTreeNode[n];
else
NewNode->Child = NULL;
// continue here
#include<iostream.h>
#include<conio.h>
#include<alloc.h>
struct GTreeNode
{
int val;
int NChild;
GTreeNode **Child;
}**Root=NULL;
void CreateGeneralTree(GTreeNode**,int);
void ShowGTNode(GTreeNode *R);
void main()
{
int i, val, n;
GTreeNode *NewNode ;
clrscr();
//Create Root Node.
cout<<"\nEnter Root Val " ;
cin>>val ;
cout<<"Enter No. of Children " ;
cin>>n;
NewNode=new GTreeNode;
NewNode->val=val;
NewNode->NChild=n;
NewNode->Child[i]=new GTreeNode[n] ;
Root=&NewNode;
CreateGeneralTree(Root,n);
ShowGTNode(*Root);
getch();
}
void CreateGeneralTree(GTreeNode **r, int n)
{
int i,k, m;
char ch ;
//allocate memory for n children.
for(i=0;i<n;i++)
{
(*r)->Child[i]=new GTreeNode;
cout<<"\tEnter value for Child "<< i << " : ";
cin>>(*r)->Child[i]->val;
(*r)->Child[i]->NChild=0;
*((*r)->Child[i]->Child)=NULL;
}
cout<<"\nDo You Wish to Enter Info of Child Nodes : ";
ch=getche();
if(ch=='y' || ch=='Y')
{
for(k=0;k<n;k++)
{
cout<<"\n "<<k<<" Enter No. of Children of " << (*r)->Child[k]->val<<" : ";
cin>>m;
(*r)->Child[k]->NChild=m ;
CreateGeneralTree(&((*r)->Child[k]),m);
}
}
}
void ShowGTNode(GTreeNode *R)
{
int i,n=R->NChild;
cout<<"\nInfo about "<<R->val;
cout<<"\n\tNo. Of Children : "<<n ;
for(i=0;i<n;i++)
cout<<"\n\t"<<i<<" "<<R->Child[i]<<" "<<R->Child[i]->val<<" "<<R->Child[i]->NChild;
for(i=0;i<n;i++)
if(R->Child[i]->NChild>0)
ShowGTNode(R->Child[i]) ;
getch();
}
// put here the code I posted previously
...
// continue here
NewNode->val=val;
NewNode->NChild=n; //new node has n children
GTreeNode empty = { 0 };
for(i=0;i<n;i++)
NewNode->Child[i] = empty; //initiall ymake them all Null
Root=NewNode; // root points to newnode. root is root of tree.
CreateGeneralTree(Root, n); //now get details of n childern of root & their children
//ShowGeneralTree(NewNode);
cin >> i;
return i;
}
void CreateGeneralTree(GTreeNode * r, int n)
{
int i,k, m;
char ch ;
//Enter values of Child Nodes.
cout<<"\nNode Created at : "<< r<<"\n";
for(i=0;i<n;i++)
{
cout<<"\tEnter value for Child "<< i << " : ";
cin>>r->Child[i].val;
r->Child[i].NChild=0;
r->Child[i].Child=NULL;
}
ShowGTNode(r); //chek is it ok.
cout<<"\nDo You Wish to Enter Info of Child Nodes : ";
cin >> ch;
if(ch=='y' || ch=='Y')
{
for(k=0;k<n;k++)
{
cout<<"\n "<<k<<" Enter No. of Children of " << r->Child[k].val<<" : ";
cin>>m;
r->Child[k].NChild=m ;
if (m > 0)
r->Child[k].Child = new GTreeNode[m];
else
r->Child[k].Child = NULL;
CreateGeneralTree(&r->Child[k],m); //recurisive
}
}
}
void ShowGTNode(GTreeNode *R)
{
// continue here
#include<iostream.h>
#include<process.h>
#include<conio.h>
struct GTreeNode
{
int val;
int NChild;
GTreeNode *Child; //now its not an array of pointers, Please explain, why ?
}*Root=NULL;
void CreateGeneralTree(GTreeNode*, int);
void ShowGTNode(GTreeNode *R);
int main()
{
int i, val, n;
GTreeNode *NewNode ;
system("cls"); //does not work.may be becoz i m using Turbo C++ version 3.0
clrscr();
cout<<"\nEnter Root Val " ;
cin>>val ;
cout<<"Enter No. of Children " ;
cin>>n;
NewNode = new GTreeNode;
if (n > 0)
NewNode->Child = new GTreeNode[n];
else
NewNode->Child = NULL;
NewNode->val=val;
NewNode->NChild=n; //new node has n children
GTreeNode empty = { 0 };
for(i=0;i<n;i++)
NewNode->Child[i] = empty; //initially make them all Null
Root=NewNode; // root points to newnode. root is root of tree.
CreateGeneralTree(Root, n); //now get details of n childern of root & their children
cout<<"\n OUTPUT : \n";
ShowGTNode(Root);
getch();
return 0;
}
void CreateGeneralTree(GTreeNode *r, int n)
{
int i,k, m;
char ch ;
//Enter values of Child Nodes.
cout<<"\nNode Created at : "<< r<<"\n";
for(i=0;i<n;i++)
{
cout<<"\tEnter value for Child "<< i << " : ";
cin>>r->Child[i].val;
r->Child[i].NChild=0;
r->Child[i].Child=NULL;
//if empty was global, can it be written here also. i mean, is not empty as
//declared by you is serving same as NULL.
}
cout<<"\nDo You Wish to Enter Info of Child Nodes : ";
//cin >> ch; //chaged to getche();
ch=getche();
if(ch=='y' || ch=='Y')
{
for(k=0;k<n;k++)
{
cout<<"\n "<<k<<" Enter No. of Children of " << r->Child[k].val<<" : ";
cin>>m;
r->Child[k].NChild=m ;
if (m > 0)
r->Child[k].Child = new GTreeNode[m];
else
r->Child[k].Child = NULL;
CreateGeneralTree(&r->Child[k],m); //Recursive
}
}
}
void ShowGTNode(GTreeNode *R) //now its simple.
{
int i,n;
cout<<"\nInfo about "<<R->val;
n=R->NChild ;
cout<<"\tChildren : "<<n <<"\tAs : ";
for(i=0;i<n;i++)
cout<<R->Child[i].val<<" ";
for(i=0;i<n;i++)
if(R->Child[i].NChild > 0)
ShowGTNode(&(R->Child[i]));
}
void ShowGTNode(GTreeNode *R)
{
int i, n=R->NChild;
cout<<"\nNo. Of Children : "<<n<< " ";
for(i=0;i<n;i++)
cout<<R->Child[i].val << " ";
}
Title | # Comments | Views | Activity |
---|---|---|---|
Need 'Configuration File' Management Library for C++ Project | 1 | 481 | |
TCP/IP Socket - connection close results in data lost | 14 | 110 | |
Focus not getting shifted out of editbox | 2 | 50 | |
How to print into std::string like sprintf using C++11? | 12 | 42 |
Join the community of 500,000 technology professionals and ask your questions.
Connect with top rated Experts
13 Experts available now in Live!