Address is printing not the data

my code
#include
using namespace std;

class tnode{
public:
int data;
tnodeleft;
tnode
right;

tnode(int data){
	data=data;
	left=NULL;
	right=NULL;

}

};
tnode* buildbst(int arr[],int s,int d){
if(s>d){
return NULL;
}
int mid=(s+d)/2;
tnode root=new tnode(arr[mid]);
root->left=buildbst(arr,s,mid-1);
root->right=buildbst(arr,mid+1,d);
return root;
}
void preorder(tnode
&root){

if(root==NULL){
	return;
}
cout<<root->data<<" ";
preorder(root->left);
preorder(root->right);

}
int main() {
int t;
cin>>t;
while(t>0){
int n;
cin>>n;
int arr[100];
for(int i=0;i<n;i++){
cin>>arr[i];
}
tnode* root=buildbst(arr,0,n-1);
preorder(root);
t–;
}

return 0;

}

Hey
Which cases are not working?
Also, can you post your code in coding blocks ide link?

for every case i am getting address of the node instead of data as output

hello i have shared the code as the output i am getting address of the root what am i doing wrong

Hey @toshitvarshney11
Sorry for late reply
Hey the problem is that
See in your constructor,you can’t use same name for parameter of constructor and object element. This gives garbage value to data of object when you write data=data
tnode(int data){
data=data;
left=NULL;
right=NULL;

} 

What you should do is
either you use ‘this’ keyword, you can do it like:-
tnode(int data){
this->data=data;
left=NULL;
right=NULL;

}

else you can just change the name of the above parameter.
so you do any of the above two ways.

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.