binary search tree c++ searching operation always giving 0; - c++

in this BST,the searchbst function is searching perfectly but this function is returning 0 always. It is not giving 5 or 8 as i have programmed it to do so what is the error in code because of which this problem is there
#include<iostream>
using namespace std;
struct bstnode{
bstnode *lchild;
int data;
bstnode *rchild;
};
void creatbst(bstnode *&T,int k){
if(T=='\0'){
T=new(bstnode);
T->data=k;
T->lchild='\0';
T->rchild='\0';
}
else if(k<T->data){
creatbst(T->lchild,k);
}
else if(k>T->data){
creatbst(T->rchild,k);
}
}
int searchbst(bstnode *T,int k){
if(T=='\0')
return 5;
else{
if(k<T->data)
searchbst(T->lchild,k);
else if(k>T->data)
searchbst(T->rchild,k);
else
return 8;
}
}
int main(){
bstnode *T;
T='\0';
creatbst(T,36);
creatbst(T,20);
creatbst(T,75);
creatbst(T,42);
creatbst(T,8);
creatbst(T,31);
creatbst(T,25);
creatbst(T,3);
creatbst(T,80);
cout<<endl<<"searching for ";
cout<<searchbst(T,3);
cout<<endl<<"searching for ";
cout<<searchbst(T,1);
return 0;
}

You're not using the return value of the recursive calls.
Replace:
if(k<T->data)
searchbst(T->lchild,k);
else if(k>T->data)
searchbst(T->rchild,k);
else
return 8;
with:
if(k < T->data)
return searchbst(T->lchild, k);
else if(k > T->data)
return searchbst(T->rchild, k);
else
return 8;

Your code most likely has undefined behavior: If you don't follow the conditions leading to the return 5; or the return 8; statements, you call searchbst(), ignore its result, and eventually you fall of the function. You probably meant to return the result of searchbst():
return searchbst(T->rchild, k);
BTW, you have a funny way of writing the null pointer constant: although '\0' works, the conventional way is to use 0 or nullptr (the latter is the C++11 way).

Related

Binary Search Recursion warning

#include <iostream>
using namespace std;
int i = 0;
int binarySearch(int arr[],int left, int right, int item)
{
int midpoint;
bool found{false};
if(left < right && !found)
{
midpoint = left + (right - left)/2;
if(arr[midpoint]<item)
{
binarySearch(arr,midpoint+1,right,item);
}
else if(arr[midpoint]>item)
{
binarySearch(arr,left,midpoint-1,item);
}
else
{
found = true;
return midpoint;
}
}
}
int main()
{
int arr[] = {10,20,30,40};
int x = binarySearch(arr,0,3,40);
cout << x ;
}
How is it returning the correct value of the item searched for although its not even reaching the return statement.
It is reaching the base case when it is only one element in the array, but it should not reach the return statement, thus it should return garbage, but it is returning the correct index every time.
In most cases you don't return any value so you get whatever happens to be in the result register or stack slot at the time. This can work by accident, if you are unlucky.
Turn on compiler warnings and always fix them. Best to turn warnings into errors.

Vector error,a very confusing segmentation error?

So basically,I am doing a code which searches for an element of a vector inside a vector.While I thought of the approach , implementing it got me a segmentation error. I narrowed down the problem
In the code if I decomment the line in the for loop while commenting the above then all elements of B[i] are being displayed.Why then is a segmentation error being thrown. I think the binary_return is more or less correct and if I replace the line with
binary_return(A,0,A.size(),B[1])
then its working.
Here is the code:
#include<iostream>
#include<vector>
using namespace std;
int binary_return(vector<int> a,int start,int end,int seek)
{
int mid = (start+end)/2;
//cout<<start<<" "<<seek<<" "<<mid;
if(end!=start)
{
if(a[mid]==seek)
{
return mid;
}
else if(a[mid]>seek)
{
return binary_return(a,start,mid,seek);
}
else if(a[mid]<seek)
{
return binary_return(a,mid,end,seek);
}
}
else
return -1;
}
int main()
{
vector<int> A{1,3,6,9,23};
vector<int> B{1,4,23};
cout<<B[0]<<B[1]<<B[2];
for(int i=0;i<B.size();i++)
{
cout<<binary_return(A,0,A.size(),B[i]);
//cout<<binary_return(A,0,A.size(),B[0]);
}
return 1;
}
Your code is not handling the last case correctly and ends up in infinite recursion.
This unfortunately in C++ means that anything can happen (you're not guaranteed to get a meaningful error).
Add a debug print at the beginning of the function and you'll see in which cases you're entering infinite recursion.
You have infinite recursion in third if statment
The correct code if the following:
#include<iostream>
#include<vector>
using namespace std;
int binary_return(vector<int> a,int start,int end,int seek)
{
int mid = (start+end)/2;
//cout<<start<<" "<<seek<<" "<<mid;
if(end!=start)
{
if(a[mid]==seek)
{
return mid;
}
else if(a[mid]>seek)
{
return binary_return(a,start,mid,seek);
}
else if(a[mid]<seek)
{
// In your sample you forgot to add +1 (mid+1) for next start
return binary_return(a,mid+1,end,seek);
}
}
else
return -1;
}
int main()
{
vector<int> A{1,3,6,9,23};
vector<int> B{1,4,23};
for(int i=0;i<B.size();i++)
{
cout<<binary_return(A,0,A.size(),B[i]);
}
return 0;
}

char changing after returned

This is an attempted solution of a problem on codefights: https://codefights.com/interview-practice/task/FwAR7koSB3uYYsqDp
My BFS function is not returning the correct character despite me printing right before the return and seeing the correct character in the console. It seems the chracter is being mutated for some reason. When I change the function signature to have a std::string return value, the program crashes. I have no clue what I'm doing wrong. Is it possibly due to lack of freeing pointers or something?
typedef struct proTree{
char value;
proTree* left;
proTree* right;
} proTree;
char BFS(std::vector<proTree*> vec, int currLevel, int level, int pos){
if (currLevel == level){
if (vec[pos-1]->value == 'E'){
return 'E';
} else {
return 'D';
}
}
std::vector<proTree*> newVec;
for (int i=0; i<vec.size(); i++){
newVec.push_back(vec[i]->left);
newVec.push_back(vec[i]->right);
}
BFS(newVec, currLevel+1, level, pos);
}
void createTree(proTree* root, int currLevel, int level){
if (currLevel == level) return;
proTree* eTree = new proTree();
eTree->value = 'E';
proTree* dTree = new proTree();
dTree->value = 'D';
if (root->value=='E'){
root->left = eTree;
root->right = dTree;
} else {
root->left = dTree;
root->right = eTree;
}
createTree(eTree, currLevel+1, level);
createTree(dTree, currLevel+1, level);
}
std::string findProfession(int level, int pos) {
proTree* eTree = new proTree();
eTree->value = 'E';
createTree(eTree, 0, level);
std::vector<proTree*> vec = {eTree};
char result = BFS(vec, 0, level, pos);
if (result == 'E'){
return "Engineer";
} else {
return "Doctor";
}
}
BFS does not return anything (actually returned value is undefined) because last function line is missing return and the value of recursive function invocation is lost. It should be:
return BFS(newVec, currLevel+1, level, pos);
You should pay attention to compilation warnings. In this case compiler should've definitely complained about "missing return in function returning non-void" or something similar.

Searching a string inside a char array using Divide and Conquer

Let's say that I have a struct array and each element has a name. Like:
struct something{
char name[200];
}a[NMAX];
Given a new string (char array), i need to find the correct index for it using divide and conquer. Like:
char choice[200];
cin>>chioce;
int k=myFunction(choice); // will return the index, 0 otherwise
// of course, could be more parameters
if( k )
cout<<k;
I don't know how to create that searching function (I tried, I know how D&C works but i'm still learning! ).
And no, i don't want to use strings !
This is what i tried:
int myFunction(char *choice, int l,int r) // starting with l==0 && r==n-1
{
int m;
if(strcmp(a[m].name,choice)==0)
return m;
else{
m=(l+r)/2;
return myFunction(choice,l,m-1);
return myFunction(choice,m+1,r);
}
}
This is my solution for your above problem. But i have modified a few things in your code.
#include<iostream>
using namespace std;
#define NMAX 10
struct something{
char *name; //replaced with char pointer so that i can save values the way i have done
}a[NMAX];
int myFunction(char *choice, int l,int r) // starting with l==0 && r==NMAX-1
{
if(l>r) //return if l has become greater than r
return -1;
int m=(l+r)/2;
if(strcmp(a[m].name,choice)==0)
return m+1;
else if(l==r) //returned -1 as the value has not matched and further recursion is of no use
return -1;
else{
int left= myFunction(choice,l,m-1);//replaced return
int right= myFunction(choice,m+1,r);//by saving values returned
if(left!=-1) //so that i can check them,
return left; //otherwise returning from here onlywould never allow second satatement to execute
if(right!=-1)
return right;
else
return -1;
}
}
int main(){
a[0].name="abc";
a[1].name="a";
a[2].name="abcd";
a[3].name="abcf";
a[4].name="abcg";
a[5].name="abch";
a[6].name="abcj";
a[7].name="abck";
a[8].name="abcl";
a[9].name="abcr";
char choice[200];
cin>>choice;
int k=myFunction(choice,0,NMAX-1); // will return the index, 0 otherwise
// of course, could be more parameters
if( k !=-1)
cout<<k;
else
cout<<"Not found";
return 0;
}
Hope it will help.

ternary tree giving error

This is simple ternary tree structure . I have written code correctly but while running it says after some time:
Sorry ternary.exe has stopped working.
Can you tell me the cause of this error.
#include<iostream>
#include<string>
using namespace std;
struct tnode{
int data[2];
tnode *ptr[3];
};
void swap(int *a,int *b){
int t;
t=*a;
*a=*b;
*b=t;
}
//for initializing tnode variables as null or null character
void newtree(tnode *&T){
T->data[0]='\0';
T->data[1]='\0';
T->ptr[0]=NULL;
T->ptr[1]=NULL;
T->ptr[2]=NULL;
}
void fillto(tnode *&T,int a){
if(T->data[0]=='\0'){
T->data[0]=a;
}
else if(T->data[0]!='\0'&&T->data[1]=='\0'){
T->data[1]=a;
if(T->data[0]>T->data[1])
swap(T->data[0],T->data[1]);
}
else{
if(a<T->data[0]){
if(T->ptr[0]==NULL){
T->ptr[0]=new(tnode);
newtree(T->ptr[0]);
}
fillto(T->ptr[0],a);
}
else if(a>T->data[1]){
if(T->ptr[2]==NULL){
T->ptr[2]=new(tnode);
newtree(T->ptr[2]);
}
fillto(T->ptr[2],a);
}
else{
if(T->ptr[1]==NULL){
newtree(T->ptr[1]);
T->ptr[1]=new(tnode);
}
fillto(T->ptr[1],a);
}
}
}
tnode *datatnode(string s){
int l=0;
tnode *T;
tnode *E;
T=new(tnode);
char c[0];
newtree(T);
E=T;
while(l<=s.length()){
c[0]=s[l];
cout<<atoi(c)<<endl;
fillto(T,atoi(c));
l++;
}
return E;
}
int main(){
string s="5398124";
tnode *T;
T=new(tnode);
T=datatnode(s);
cout<<T->data[0];
return 0;
}
You should remove '=' sign as below
tnode *datatnode(string s){
int l=0;
tnode *T;
tnode *E;
T=new(tnode);
char c;
newtree(T);
E=T;
int a = s.length();
while(l<a){
c=s[l];
cout<<atoi(&c)<<endl;
fillto(T,atoi(&c));
l++;
}
return E;
}
Its difficult to say from your code (as mentally you have to run it in your head). Better to debug it out. Call some debug at key points in your code and try to locate the exact line of code.... this could produce a lot of debug depending how big your data-set is.
At a guess I would say that you probably hit a bad address or somthing like this, that is usually why programs die un-expectedly and immediatly! So I would suggest being very secure on your pointer checking. For example:
void fillto(tnode *&T,int a){
if (T != NULL){
if(T->data[0]=='\0')
{
T->data[0]=a;
}
:
:
}
else
{
printf("Warning: NULL pointer!\n");
}
}
Basically any time you use a pointer that is passed in to a function you should check it is not null. This is generally good code practise and may help you to find your bugs :)
Also int initialisation can just be:
int i = 0;
instead of
int i = '\0';
The fundamental flaw that causes the error is in the 'void fillto(tnode *&T,int a)' function:
...
if(T->ptr[1]==NULL){
newtree(T->ptr[1]);
...
}
As the function newtree does not check if the pointer is null, you end up dereferencing a NULL pointer in newtree