My program doesn't work properly. Please help me
Code:
#include "stdafx.h"
#include <iostream>
#include <conio.h>
#include <stdio.h>
#include <string.h>
using namespace std;
class node{
public:
node *child_left;
node *child_right;
char value [11];
};
node *reshe = NULL;
//#######################################################
void insert (node *c)
{
node *a;
node *b;
b = NULL;
a = reshe;
while ( a!=NULL)
{
b = a;
if(strcmp(c->value,a->value) < 0)
{
a= a-> child_left;
}
else
{
a= a-> child_right;
}
}
if( b==NULL )
{
reshe=c;
}
else if(strcmp( c->value,b->value) < 0)
{
b -> child_left = c;
}
else
{
b-> child_right = c;
}
}
//#######################################################
void preorder(node *r){
if( r!=NULL)
{
cout<<" "<< r->value;
preorder(r->child_left);
preorder(r->child_right);
}
}
//#######################################################
void inorder(node* r){
if( r!=NULL)
{
inorder(r->child_left);
cout << r->value << " " ;
inorder(r->child_right);
}
}
//########################################################
void postorder(node* r){
if( r!=NULL)
{
inorder(r->child_left);
inorder(r->child_right);
cout << r->value << " " ;
}
}
//#######################################################
void main()
{
int choice=0 ;
while (choice!=3){
system("cls");
cout<<"\n\n\t 1. Insert a Expression.";
cout<<"\n\n\t 2. peymayesh.";
cout<<"\n\n\t 3. Exit.";
cout<<"\n\n\t Enter your choice : ";
cin>>choice;
switch (choice)
{
case 1:{ system("cls");
node *c=new node;
cout<<"\n\n\t Enter your Expression Trees: ";
cin >> c->value;
c->child_right=NULL;
c->child_left=NULL;
insert(c);
}
break;
case 2:{ system("cls");
cout << "\n\n\t peymayesh Preorder : ";
preorder(reshe);
cout << "\n\n\t peymayesh Inorder : ";
inorder(reshe);
cout << "\n\n\t peymayesh Postorder : ";
postorder(reshe);
getchar();
getchar();
}
break;
case 3: break;
}
}
}
Comment