Binary tree traversal issue

why the below where i have made the queue class myself is not working fine.the output showing only the nodes in the right subtree.why what is the mistake?
#include
//#include
using namespace std;
//implementing templated queue
template
struct queue{
t *arr;
int front;
int rear;
int cs;
int ms;
queue(int ds=10){
ms=ds;
arr=new t[ds];
front=0;
rear=ms-1;
cs=0;
}

bool empty(){
return cs==0;
}

bool full(){
return cs==ms;
}

void enqueue(t data){
if(!full()){
rear=(rear +1)%ms;
arr[front]=data;
cs++;}
}

void dequeue(){
if(!empty()){
front=(front+1)%ms;
cs–;
}
}

int getcs(){
return cs;
}

t getfront(){
return arr[front];
}

void display(){
for(int i=front;i<=rear;i++)
cout<<arr[i]<<" ";
cout<<endl;
}
};

struct node{
int data;
node *left;
node *right;
node(int data){
this->data=data;
left=NULL;
right=NULL;
}
};

node *buildtree(){
int data;
cin>>data;
if(data==-1)
return NULL;
node *root=new node(data);
root->left=buildtree();
root->right=buildtree();
return root;
}

void printpost(node *root){
if(root==NULL)
return;
printpost(root->left);
printpost(root->right);
cout<data<<" ";
}

void bfs(node *root){
//1.declare a queue and push the root node to the queue
queue<node *> q;
//q.enqueue(root);
q.enqueue(root);
//2.print and pop the front of the bqueue
while(!q.empty()){
node * no=q.getfront();
//cout<<no->data<<" ";
//q.dequeue();
q.dequeue();
cout<<no->data<<" ";
//3.add the children of the popped node into the queue
if(no->left)
q.enqueue(no->left);
if(no->right)
q.enqueue(no->right);
}
}

int main(){
node *root=buildtree();
printpost(root);//post order traversal
cout<<endl;
bfs(root);//level order traversal
return 0;
}

@Rj.25 there is problem in your enque code ,you have to insert element at rear,instead of arr[front]=data; it should be arr[rear]=data
Also you may use inbuilt queue of stl
Hope your doubt is resolved.

@Rj.25 I am closing this doubt ,please give rating so that we can work more on your feedback and feel free to ask any doubt :slight_smile:

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.