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();
}

hi
I think your insertion logic is not correct.
While inserting u are checking the _sysMatch symbol to be same.
If the symbol match and particular side(left/right) is not null u are recursively calling the insert function with the same _sysMatch sysmbol for checking, but then that particular symbol wont be there and there will be some other symbols so from now your condition for normal insertion (root->sym==_symMatch)&&(root->left==NULL)will fail because the symbol in root->sym is different now.

let me clarify using your code itself

insertRight(root, '-', 'g');
 insertLeft(root, '-', '-');
 insertRight(root, '-', '*');
 insertLeft(root, '-', 'e');
 insertRight(root, '*', '+');
 insertLeft(root , '*', '/');
 insertLeft(root , '+', 'c');
 insertRight(root, '+', 'd');

after line 1:
....... '-'
.... ' ' 'g'
after line 2:
...... '-'
... '-' 'g'
after line 3:
.... '-'
. '-' ... 'g'
i guess at this point u should get yoyr error message:
"May be u wrong somewhere, try check your code"
This will be clear if u do a dry run of your program in paper.
and your symbol '*' should not get inserted to the tree.

after line 4:
................ '-'
............ '-' .... 'g'
......... 'e'

after line 5:
................ '-'
............ '-' .... 'g'
......... 'e'

[becoz it wont find the symbol '*']

after line 6:
................ '-'
............ '-' ... 'g'
......... 'e'
[becoz it wont find the symbol '*']

after line 7:
................ '-'
............ '-' ... 'g'
......... 'e'
[becoz it wont find the symbol '+']

after line 8:
................ '-'
............ '-' .... 'g'
......... 'e'
[becoz it wont find the symbol '+']

your final tree should be:
................ '-'
............ '-' .... 'g'
......... 'e'

hope that helps...........
thanks

after line 1:
....... '-'
.... ' ' 'g'
after line 2:
...... '-'
... '-' 'g'

after line 3: (correction in here)
............ '-'
. .....'-' ..... 'g'
..........'*'

after line 4:
................ '-'
............ '-' .... 'g'
....... 'e' ... '*'

after line 5:
................ '-'
............ '-' .... 'g'
....... 'e' ... '*'
here it wil fail because of your logic. wen u didn't match the matching element u are always going left and as u can see if u traverse left on getting a mismatch u wil never reach to '*'. Hence this insertion wil fail.

after line 6:
same as above

after line 7:
the symbol '+' is not there hence u wont b able to insert.
afetr line 8:
same as above.

I would suggest u to correct your logic for insertion. u first clarify where u want to insert a new element and what parameters u want for that.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.