i wanna make function , takes parametrs binary tree ,t, , 2 integers a,b , prints how many integers between , b...
as example here binary tree ,with 3 nodes , data : 10,15,20. call function example : between(t ,10 ,20) , result expect number 1 , not happen.. function :
` int between (tree t, int a, int b) { if (t== null) return 0; if ((t->data >a ) && ( t->data <b)) return 1 + between(t->left,a,b) + between(t->right,a,b); }
can explain me ?
first of all, if number not between , b, have not defined return value, error other program doesnt required, function doesnt return int
value.
secondly, should go on, when value not on range. function should this:
int between (tree t, int a, int b) { if (t== null) return 0; if ((t->data >a ) && ( t->data <b)) return 1 + between(t->left,a,b) + between(t->right,a,b); else return between(t->left,a,b) + between(t->right,a,b); }
better yet, if values on left lower values on right (search tree) can make improvement.
int between (tree t, int a, int b) { if (t== null) return 0; if ((t->data >a ) && ( t->data <b)) return 1 + between(t->left,a,b) + between(t->right,a,b); else if(t->data<a) return between(t->right,a,b); else if(t->data>b) return between(t->left,a,b); }
Comments
Post a Comment