okay the problem is to display only the leaves of the binary tree check the code its running but somehow I don't think the output is quite right.
Code:
#include <iostream>
#include <string>
using namespace std;
struct node
{
char info[12];
node *left;
node *right;
};
void leafdisplay(node *p);
void inorder(node *p);
void preorder(node *p);
void insert(node *&p, char string[12]);
void insert(node *&p, char string[12])
{
if(p==NULL)
{
p=new(node);
strcpy(p->info,string);
p->left=NULL;
p->right=NULL;
}
else if(strcmp(string,p->info) < 0)
{
insert(p->left, string);
}
else insert(p->right, string);
} // You were missing an ending bracket, add this
void inorder(node *p)
{
if(p!=NULL)
{
inorder(p->left);
cout<<p->info<<" "<<endl;
inorder(p->right);
}
}
void preorder(node *p)
{
if(p!=NULL)
{
cout<<p->info<<" "<<endl;
preorder( p->left);
preorder(p->right);
}
}
void leafdisplay(node *p)
{
if(p->left !=NULL && p->right !=NULL)
{
leafdisplay(p->left);
leafdisplay(p->right);
}
cout<<p->info<<endl;
}
int main() // main should always return an int
{
node *root=NULL;
char string[12];
for(int i=0; i<12; ++i)
{
cout<<"Enter the value for your node: ";
cin.getline(string, 11, '\n');
insert(root, string);
// Your insert function takes two parameters but you only provide one
}
inorder(root);
cout<<endl;
preorder(root);
cout<<endl;
leafdisplay(root);
return 0;
}
apr
aug
dec
feb
january
jul
jun
mar
may
nov
oct
sep
feb
jun
may
mar
january
Press any key to continue
so whats up with this leaf function is the logic okay here or did I miss something