I try write about binary tree , but i have some troubles ,
I insert some symbols into binary tree by 2 function insertLeft () and insertRight () ...When i display the symbols i see to lack some symbols ..
who can help me ???thank very much
#include<stdio.h>
#include<conio.h>
#include<string.h>
typedef char data;
struct element{
data sym;
element *left, *right;
};
typedef element *Tree;
Tree root=NULL;
void insertLeft(Tree &root, data _symMatch, data _symNode){
if(root!=NULL){
if((root->sym==_symMatch)&&(root->left==NULL)){
if(_symNode=='/') printf("\nDivisor\n");
Tree node = new element;
node ->sym=_symNode;
node ->left=node->right=NULL;
root ->left=node;printf("\ninsert ok %c\n",node->sym);
}
else if(root->left!=NULL)
insertLeft(root->left, _symMatch, _symNode);
else if(root->right!=NULL)
insertLeft(root->right, _symMatch, _symNode);
}
else printf("\nMay be u wrong somewhere, try check your code\n");
}
void insertRight(Tree &root, data _symMatch, data _symNode){
if(root!=NULL){
if((root->sym==_symMatch)&&(root->right==NULL)){
Tree node = new element;
node ->sym=_symNode;
node ->left=node->right=NULL;
root ->right=node; printf("\ninsert ok %c\n",node->sym);
}
else if(root->left!=NULL)
insertRight(root->left, _symMatch, _symNode);
else if(root->right!=NULL)
insertRight(root->right, _symMatch, _symNode);
}
else printf("\nMay be u wrong somewhere, try check your code\n");
}
void travel(Tree root){
if(root!=NULL){
//printf("->'%c'",root->sym);
travel(root->left);
printf("->'%c'",root->sym);
travel(root->right);
//printf("->'%c'",root->sym);
}
}
void main(){
clrscr();
//char *s="efab+/cd+*-g-";
root = new element ;
root->sym='-';
root->left=root->right=NULL;
insertRight(root, '-', 'g');
insertLeft(root, '-', '-');
insertRight(root, '-', '*');
insertLeft(root, '-', 'e');
insertRight(root, '*', '+');
insertLeft(root , '*', '/');
insertLeft(root , '+', 'c');
insertRight(root, '+', 'd');
travel(root);
getch();
}