Hello all and welcome back.
So in this video let us continue our discussion.
And now let's take the input of our binary tree level wise.
So let's get started.
Now when we took the overall input as level wise then we were not using recursion.
We were actually using the concept of queue.
The reason for that is that we want to make sure that instead of going deep down into one side, let's
say in this particular binary tree left side, I should be working in this manner, right?
So this is the manner on which I want to work with the overall benefit we saw in the generic tree as
well.
It's better if let's say I have a one which is the node coming first.
I am able to work on that first, get all the left right child for that particular node, and then move
forward in the order in which my nodes are coming to me.
So one will come first, I will pop it out and ask that, hey, what is the left child of one?
What is the right child of one?
If there will be a child, then I will be pushing them in.
For example, let's say two will get pushed in first.
Then I will ask for two.
Hey, what is the left child of two?
Okay.
And if there is a left child of three after that, actually I will be asking for three.
So in this manner, the whole way in which we could have gone a lot deep down on a particular side,
we would just be taking the same level wise, which will be a lot helpful to me.
Okay.
So I can actually help you imagine that as well, that how exactly will it go?
Let's say if I add another level here and say another level here.
Now to make sure that we are able to go level wise, just let us do one thing.
Yeah.
So let me stop it.
Let's see the level order here.
Visualize level order.
See how exactly it is going to be.
Let me make the speed average and let us visualize this.
So this is the first one.
Then this is the second.
This is the third and this is the fourth.
Earlier what we were doing that was our pre order.
Right.
So let me help you imagine that as well.
Everyone pre order speed will be slow okay.
So pay attention first.
This will be pushed up then this then three then four.
And we will go all the way back up and then three okay.
So I hope this overall visualization is helping a lot as well.
So visualization.
The reason I use it a lot is I have seen that over my experience, over my full career.
If I am able to visualize something, if I am able to build the intuition by visualizing the same,
then it always stays with me, right?
That's the reason I draw so many diagrams and make sure that each and every thing is a lot clear via
intuition and visualization.
Great.
So let us write the code everyone.
If Diff EQ.
Input.
Level.
Wise.
Okay.
So let us now continue.
First thing I'm going to ask for the data again.
This time I can say that hey give me the data of root instead of node because I am this time working
with queue, not recursion.
So I will just be asking this once that hey, can you please give me this root?
So this will work fine.
Let us correct the indentation.
Let me zoom out as well.
Uh, and just make some space.
Great.
Now I think we can zoom in back, everyone.
Okay.
So this is the function which we are writing.
Let me add some extra space here.
So the function which is in, uh, front of us is the one which we are completing.
We know that if we have got the data, then we have to create a root.
Root will be created with binary tree node and the data which the user has given.
Okay, great.
Now going forward, what I have to do.
I have to create a queue for that queue.
If you remember in the generic tree section, in the generic tree module I was importing dequeue from
collections import deck or dequeue like I like to call it d queue.
Okay, so this queue will first have just one element or object which is root okay.
Not the data of root as we were drawing it here.
No the whole overall root because I need its left and right as well.
Okay.
This is one very common mistake which many students do.
They just add the data, which is wrong.
Okay.
While length of my queue is not zero and I can of course write it like this as well.
While the length is not zero.
Basically, while my queue is not empty, let me get the current node.
Okay, let us get the current node.
It can be q dot pop left.
Okay.
So we saw this in the last module as well.
How q dot pop left is used.
Okay then let us ask specific question this time.
Okay.
So pay attention everyone.
This time we are not going to run the loop in respect to number of children.
We are not going to run it for a two, let's say, because we know that there might be two children.
No, we will be making sure by asking user that, hey, do you have a left child?
So let me get the left child data.
It's going to be int input f.
Enter data.
Enter left child for current node dot data.
Okay.
So we want to make sure that we show this very nicely.
Great.
If users say.
If users say that yes, I want a left child, it is not equal to minus one.
Then what we are going to do?
Everyone, we have to create this left node.
Okay.
We can create the left node using this and left child data.
Great.
Once I have created this then I think till now I am here.
I have created an oh one second.
Yeah.
So I have created a one minute everyone.
So we have created the root as of now, which is say one.
And I have created two, which I say is the left child.
This is creating.
But I should also make this particular connection.
This is in my current node.
Okay great.
So let us move forward now.
Many of you might be thinking that we have done the same thing in the generic tree as well.
Well, in these kind of problems I am actually extra careful because they seem a lot similar.
And of course the overall idea or doing is not similar.
But still we make a mistake because very minor changes are there, right?
So always make sure that these type of functions, which are very basic, which expect you to understand
the structure very nicely, always you are writing them yourself, okay.
Don't just feel that they are taking input function, which is just just helping you to take the tree.
No, you are actually constructing the tree here with all the inputs and all the connections.
So this is as good as any other question which will hone your skills in any particular data structure.
Okay, great.
Once you have done this then we should also make sure one minute.
Yeah.
So we should also make sure that we are adding our two here okay.
In the queue.
Great.
So queue dot Append.
Left node.
Okay.
Again node is to be added, not the data for right I can actually just use this one.
Right.
So let us make this right.
Right child data.
Enter right child data for current node data.
If right child data is not equal to minus one, that means we want a right child data.
We'll create a right node with the right child data.
Okay.
Then add should add it to right.
Just making sure that everything instead of left should be right.
Okay.
Once I have done this once my queue is empty, I will be pretty sure that my overall tree has been created
and I can just return the root.
Okay, so great, let us see the input.
Everyone, what should we enter?
Uh.
One minute.
Okay.
I think I pushed it down a lot.
So, yeah, this is where we can use it.
Okay.
Uh, take input level wise.
And let us try to add this as an input only, okay.
One.
Two three.
Three and four.
Uh, should be fine.
We can, uh, just use that.
Great.
So let me run this.
Enter the data for the route.
A little specific question one.
Okay.
Enter left child for one.
One left child should be two.
Okay.
Enter right child for one.
See, this time it didn't ask or go into this depth.
It is asking me for this child.
Right child is three.
Great.
Enter left child for two.
It's three only.
We saw that.
How left child for two is three.
Okay.
Great.
Enter right child for two.
Two don't seem to have a right child like it is giving me an option to add, but right now it is none.
Okay, great.
So it is minus one.
Now instead of going in depth.
Okay, it is asking me for this three.
Ideally we should have avoided this duplicacy, but should be fine.
I know that it is asking me for three.
Okay, this is the right one.
Three.
So no left child.
No right child.
The left child for three will be four, and right child is minus one.
For four we don't have any child and we can see the same everyone.
So pay attention.
This is what we have tried to construct.
One.
Let me make it here so that we can check if our understanding or our tree is right.
We have created one, and one has left as two and right as three.
Okay.
Two has left as three and right as none.
That is good.
Three has left as four And right as none great.
And three and four seems to have no extra child.
Okay, great.
So yeah, I think this should match what we have made here.
Uh, okay.
Not able to see the diagram.
1233412334.
Seems to match.
Okay great.
So yeah that is helpful everyone.
Now I want all of you to make a function for me.
Pretty sure that this overall way of taking the input will be clear.
We have just used another data structure which is Q similar format understanding we followed in our
generic trees.
Okay.
Just there the number of children were not fixed.
Now what I want all of you to do is I want that you print the tree binary tree level wise.
Okay.
So this is some code which I want all of you to write for me in this.
As you can imagine, if I want to showcase you the output, the output is going to be something like,
so we can make for this tree.
Or let me just make it in your notes so that you have a reference for that.
Okay.
Let me write it here so that you have a reference for this.
Okay.
What we have to do is print.
Three level wise okay.
What does that effectively mean.
How would I like to see the output if we have this particular tree.
Let me copy it.
We will be using Q only because level wise it's first come and first serve.
So my answer is going to be like this.
First I will write one left.
It will say that hey, left is two and right is three.
Then I'm going to write two.
It's going to say left is for right is none.
Then three should come.
Okay.
Four should not come.
Three left.
None.
Right?
None and four.
Left.
None.
Right.
None.
Okay, so this is the output, which I'm expecting that you will have once you complete this particular
function.
We will also make sure that we are able to try it out on these trees.
Okay.
So I have already given you these root one, root two and root three where of course we will be able
to see the output.
Right everyone.
So write this code down.
Then please make sure that you dry run it yourself and then finally check the answer.
Because again this is going to help you a lot in understanding overall what or exactly how the process
is happening.
Watching me code and explain is honestly a little bit easier, but once you actually get your hands
dirty, that is when you are going to learn about coding.
Okay, so cool everyone do give it a try.