There is a function with name levelorderprint which gets called with cin>>root that indicates that operator overloading has been done there but this function has a slightly different code then what prateek sir has taught in trees contents so i just want to suggest you please add a video or any type of explainaiton explaining the levelorderprint function that is being used in operator overloading.
Why opoerator overloading is not shown?
hello @we_kaash
are u talking about this function ?
void bfs(node *root)
{
if(root==NULL)
{
return;
}
queue<node *>q;
q.push(root);
q.push(NULL);
while(!q.empty())
{
node *f=q.front();
if(f==NULL)
{
cout<<endl;
q.pop();
if(!q.empty())
{
q.push(NULL);
}
}
else{
cout<<f->data<<",";
q.pop();
if(f->left)
{
q.push(f->left);
}
if(f->right)
{
q.push(f->right);
}
}
}
return;
}
this is already discussed in one of the binary tree videos.
basically here we are pushing an additional null in the queue that indicates where a particular level ends.
rest of the logic is same
no sir, I am talking about this code below:
void levelOrderBuild(node*&root)
{
int d;
cin>>d;
root=new node(d);
queue q;
q.push(root);
while(!q.empty())
{
node*n=q.front();
q.pop();
int c1,c2;
cin>>c1>>c2;
if(c1!=-1)
{
n->left=new node(c1);
q.push(n->left);
}
if(c2!=-1)
{
n->right=new node(c2);
q.push(n->right);
}
}
}
in input we are given level order traversal and we need to construt tree from that.
so in this code we are doing the same level order traversal.
first we are reading data of root . and pusing it in queue.then in while loop
we are poping out one node, we are reading two number those two numbers indicate data of left and right tree. if data is -1 then it means data doesnt exist . other wise we are creating a node and assigning it to left and right of the current node and pushing it to the queue.
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.