Hello all and welcome back.
So in this video let us now continue our discussion on binary trees.
And now let's see how can we print our binary tree.
So while we are printing the binary tree the basic idea is going to be the same.
Let me just draw a binary tree which we made in the last class.
So we created a binary tree which was having three nodes.
Right.
So it will be better if instead of just using some tool or let's say where we cannot use any tool,
we are able to print our binary tree as well.
So this was the overall binary tree which we created.
Everyone in this tree we see that we have three nodes.
Right now.
What I want to do is to again make sure that I am able to print out these nodes in a way that it tells
me the complete binary tree structure.
Okay, great.
So let us try to print this everyone.
Now.
So let me write a deaf print tree.
Binary tree where we will be getting a root.
Okay, great.
So the very first thing, we can just print the root dot data.
Okay.
As with generic trees, our overall idea, our overall approach is going to be the same that because
each and every one is a binary tree in itself.
So each and every of this node is a binary sub tree in itself.
I can very well use recursion, right?
So let me write that as well.
Recursion can be used okay.
So recursion.
Can be used.
As each node is a Binary subtree.
Okay.
For a sub binary tree that would be better because nothing like binary sub tree.
Okay, so as a sub
binary tree.
Date everyone.
So yeah let us just use this understanding.
So what effectively I'm saying is that this is a tree which I can take like this.
This is also a tree which I can assume like this okay.
So yeah let us call on these sub trees and our work is over.
So print root dot left.
Print root dot right.
Okay great.
So let us run this everyone and see if we will be able to see the result or not.
I am passing the root so we know the structure which is Just the one which which I drew.
And let me run this now.
So root dot left.
It is printing the data which is root and.
Oh sorry.
My bad.
So we should have done not print, but we could have used recursion right?
We could have used recursion.
Okay.
So it seems that I'm getting an error.
The error is that none type object has no attribute data.
Okay.
So something like this error we have seen before right.
Whenever we are trying to call this root on none.
Then we used to get this error in linked uh linked list as well as entries.
Now in the generic trees.
If we are not passing none, then why are we getting this particular error?
Well, the reason for that is.
So let us understand this.
Everyone.
I want each and every one of you to understand this as well.
If your node is none, okay, then we should return not node.
It should be root.
Now here.
This is a base case here.
This is a base case.
The reason is that in the earlier implementation I was writing a loop.
The loop was going to each and every node.
Okay.
But this time I am actually calling on the left and right specifically.
Okay.
So just to make it a little bit more clear, what exactly will happen?
I have this node one.
It is going to get printed.
It is going to give me two.
It has sorry.
It has left and right as two and three.
Now what I am doing is when I come to two I will be printing two.
So I will be getting the output as one, then two.
But after that I am making the call irrespective if two has children or not.
Okay.
So either we can restrict the call by checking okay if the root is none or not.
Okay if the root dot left is none or root dot right is none, or we can just use this particular as
our base case this time because it's going to get hit.
It is going to see that hey, none is here and it's going to return.
So this is what this return will do.
Okay everyone.
And that was the reason we were able to see one and two.
So let me just comment this.
We were able to first see one and two.
And then the error was coming because it is trying to get the data for twos left, which is none.
So none dot data is coming here.
Okay great.
So let me uncomment this.
And now if we run this we are able to see okay just one problem.
The same problem which we had in this pre-order kind of a traversal that if it is one, two and three,
the way I am seeing it can even mean a tree like this.
It can very well mean that a tree is like this.
It can very well mean that a tree is something like this as well.
Okay, so again I need to have that distinction.
So for that just we are going to add some beautification to our overall output.
So let us do that everyone.
The very first thing which we can do is we are getting this thing right so we can print the route data
okay.
And let's say we are ending it with a space.
So in the end I don't want a new line.
I just want to print it with a route data.
Now what I'm going to do.
So the way I want to output is, which I think will be a lot better, is something like this.
So I want to see one.
Okay.
Then I want to see the left of one, something like this maybe.
And the right of one.
Now this time just printing out the data will not work, okay.
Because my left and right are different.
That is the reason I am making sure that I explain you this in this much depth so that you never make
a mistake here.
So left and right.
Then I will print out two.
Two has left, which is as none.
And right which is again as none.
Similarly for three.
Left.
None.
Right.
None.
Okay, everyone.
So this is the reason.
This is the way I want to print out my overall output of the tree.
So let us write the code for this.
We just have to make sure that we are able to print it nicely.
Uh, we have printed this information.
Just have to make it a lot more beautified.
If my node dot left is not none what I have to do, I have to print.
If it is not none, then I have to just call my, uh, just I have to print this particular data.
Okay.
Recursion will be called afterwards.
So the reason the way I can print it is something like this using the F formatting L.
And now I can use node dot left dot data okay.
Reason is again the same.
I am able to use it because it says that it is not null.
Else if it is none, then I have to print L null and we can end it with this as well.
So great!
As of now it seems to work fine and it is not root.
My bad.
Sorry.
Not node.
We should be using this root everywhere.
Okay everyone.
So I will just explain till now what we have done along with printing the root data in the tree.
We were running a loop for all my children here.
I know that I have just left and right to handle.
So using that knowledge, I am just trying to basically make two conditions instead because again,
this logic now have become a lot more easier for me, right?
I don't have to work on number of children, right?
Any for loop.
I know that two children are there and thus I can make the overall tree structure a lot better printable,
right?
On the same note, if I can just copy this.
Then what we can do is We can just check if root dot.
Right.
And let us make it r.
Let us make it r.
And let us make it right.
Okay.
So again just I have added these lines.
After that we are just triggering for the read left and right children.
So recur for the left and right children.
Okay.
And let me write the format as well.
So everyone is clear that what exactly is happening.
So my format is going to be something like this.
Format node left child data R right child data.
Okay everyone.
So I think yeah this is going to do just one thing.
We should remove it here.
Okay.
Because we don't need this in the right.
We don't need this particular thing.
Great.
So yeah, let us try to run this now everyone.
It is straightforward code and I'm able to see now my overall data is in a lot better format right.
So I'm able to see that left two right three then four two left is none.
And it is right is none.
Just one thing to add here.
What I can do is in this I can just end it with this.
Right.
So yeah.
Great.
Now I think it is a lot better in terms of everything.
So we are getting one.
The children are left two right, three, two left.
None right.
None.
And three.
Both are none.
Okay.
Again.
Uh, easy overall tree, which we have visualized.
Right.
To make sure that we are able to actually see I have in a similar manner created some predefined decision
or decision trees, just like we created in our generic trees.
So they are the binary trees.
Everyone.
They are the binary trees which are having this structure.
So let us use this code and try to run it for root one just okay.
So I'm just getting this.
So from predefined import define my inputs.
Again uh if you are not aware please make sure that you see the last module.
These are just trees.
In a similar manner I have defined so that we can use the same.
It is just visualized here.
And something which we can actually use directly.
The reason is same.
We don't want to create these trees one by one ourself, right?
This is a lot time taking process.
Now if I just call print binary tree on route one and if I run it, let us see if we will be able to
draw our tree right from this.
So that thing is very important.
Again everyone okay.
Let me run this again so that the first one gets out.
Cool.
So from this understanding, if we are able to draw a tree right, then I think we should be good.
So I'm saying that.
Hey, one is there, one has left as two, right as three, two, three.
Then we have two.
Left is four, right is five.
Great.
Four left.
None right.
None five.
Also both are none three.
It is saying that left is none and right is six.
So I can take this right as six and six.
Also, both are none.
Okay, so this is the tree which I am getting.
Let us just see this quickly.
If this was the same one two.
Yeah.
Seems like we have created a created a right tree.
The overall printing is also working pretty good.
Cool everyone.
So yeah, I hope this is clear.
Now just make sure that you have an idea that why this left and right is getting used.
Although overall I think that this code and the idea which we are spending on this, this binary tree,
that is a lot clearer.
Okay.
Thank you.