• 2 days ago
Transcript
00:00Now, we have to add notes to our notes.
00:03But those notes to add to our specific technique.
00:06What is the technique?
00:08We know that we have to do that.
00:11We have to do that.
00:11We have to do one, then two, then four.
00:15We have to do one again.
00:18This is the coding.
00:20We have to do depth first search.
00:23We have to do depth.
00:25But we have to do one again.
00:28then we go to 2 and go to 4 and go to 3
00:32so we will try to do this for BFS
00:36or breadth first search
00:41so when we talk about pre-order, post-order, in-order
00:44then we try to do DFS
00:46now we talk about level order
00:48then we try to do BFS
00:49these are DFS and BFS terms
00:52which we read in the graph
00:55we don't use them in the trees
00:58but a basic concept
00:59we learn a little bit
01:02now we go to depth first
01:04breadth first
01:05that we use Q
01:07because Q has FIFO property
01:10FIFO
01:11FIFO
01:13BFS is possible
01:15usually when we want to do BFS
01:17we use FIFO property
01:18Q
01:20here
01:21level order
01:22we will do
01:24iteratively solve
01:25now we have written code
01:27recursion
01:27here we go to iteration
01:30loop
01:30and solve
01:31our process
01:33solve
01:33first
01:34we put our root node
01:37into Q
01:38so
01:391
01:39we put our Q
01:411
01:42Q
01:42between
01:43from the end
01:43as well
01:47we put out
01:49we have the need to clarify
01:50the node
01:50we are going to do
01:51into a field
01:53by the end
01:54then
01:54we put out
01:54which is
01:55by the node
01:56and the end
01:56then we are going to
01:57in-order
01:572
01:58by the end
01:58and the end
02:01we are going to do
02:11once
02:123 left child is empty, so we don't add anything to it.
02:16Why do we add the right child to the 3?
02:18Then we start 4.
02:204 is not left child or right child, so we don't add anything to it.
02:23Then we start 5.
02:255 is not left child and right child.
02:27Then we start 6.
02:286 is not left child and right child.
02:30Then we start 1, 2, 3, 4, 5, 6.
02:33First we start this line.
02:341, 2, 3, 4, 5, 6.
02:37Our output is level order.
02:40But one small thing is missing.
02:42What is missing?
02:43After each level, we need the next line.
02:45What was our output?
02:461, next line.
02:482, 3, next line.
02:504, 5, 6, next line.
02:52But we don't get this output.
02:54We get this output.
02:55Why do we represent the next line?
02:58We will represent the null pointer in the form of the next line.
03:02We need to show the next line.
03:04What will we show?
03:06What will we add in?
03:07We will add null.
03:08In the beginning, we added 1.
03:10If we add 1, we add a null.
03:13This null will show that 1 is our root node.
03:15And the root node is just a fake node.
03:18Then we need the next line.
03:20That's why we added the next line.
03:21So we added the first two.
03:22Now we will add 1.
03:24We will print it out.
03:26Then we add 1 to left child and right child.
03:28Then we add 2 and 3 to Q.
03:29Now we add on the null.
03:32Now the root node will remain.
03:33To the top of the null Confident,
03:34search for the next line will beend zone.
03:37So we print some final stuff.
03:40What's the null going?
03:41Then we print the null now.
03:43For the null center,
03:45'nolly錄' time the null printed in the second way,
03:46to the null value.
03:47On the left- 所以,
03:48find the null pointer.
03:50Then we print up,
03:51Then we show the null needed.
03:52Then.
03:52Augh until the minitone until next line need.
03:56and then we will store the left child and right child in the queue, 4 and 5.
04:01Now Q will be 3, so we will print this here and the left child which is null is null,
04:06right child will be in the queue, 6 will be in the queue.
04:09Now it will be null, just like null comes out, next line will be null,
04:13then null will be in the queue. Now it will be 4 from output,
04:18then it will be 5, 5 will be output, then it will be 6, 6 will be output,
04:22now it will be null again, so the next line will be here again.
04:26But after this null, it will be empty,
04:30so it will be no point back to it,
04:32because now the null will be null and null will be removed,
04:34it will not be necessary.
04:35So as the null will be removed and the queue will be empty,
04:38there will be no point back to it.
04:39Meaning our whole level audit reversal has been completed
04:43and we have printed all our notes.
04:45Now in level audit reversal, the time complexity is bigger of N,
05:12which is bigger of 2N and asymptotic is bigger of N time.
05:18We will write level audit reversal.
05:20Now let's write our tree level audit reversal code.
05:23First of all, let's make a function of public static.
05:26There will be no return type node,
05:28there will be no return type void.
05:30And we will write its name level order.
05:34We will have root node.
05:37First of all, let's check that the tree has been null.
05:40So if the root is null,
05:42then we will not print anything here.
05:44We will simply return because the root is empty.
05:48Now for the level order to implement,
05:50first of all, we have to create a queue.
05:52So queue of node.
05:54In queue, we are going to store our tree's node.
05:58We will name it queue.
06:00We will declare it new linked list.
06:04Now let's import our util package.
06:09import java.util.all
06:13Now in queue,
06:14first of all,
06:16we have to add root
06:16and add null.
06:20So we have to do these two things.
06:21First of all,
06:22we have to add root and null.
06:26Now let's start a loop.
06:27Until our queue is empty,
06:33we will start stealing the elements of the queue.
06:36So we will store the elements outside of it.
06:39By single.
06:40So current node.
06:41Is equal to queue.remove.
06:46Current node is equal to queue.remove.
06:48Current node is from queue.remove.
06:50Current node is equal to queue.remove .
07:01What should we do in that case?
07:03In that case, we will print our next line.
07:05So, as the null comes out, we will print the next line.
07:08Now check that our Q is empty.
07:14The null comes out in our node and why is empty?
07:18It means that we have all the level orders from the queue
07:22and this is the last node.
07:23We can break from here from here.
07:27No, we will put this null back to the next line.
07:31So, in the next level we can print the next line.
07:34In the queue, we will add our null.
07:38But if there is null node inside,
07:41we will print the data.
07:47Current node.data
07:51Current node.data
08:04If current node.left is not equal to null,
08:12then in queue we will add current node.left is not equal to null.
08:14Then in queue we will add current node.left is not equal to null.
08:23So, in queue we will add current node.right.right is not equal to null.
08:28So, in queue we will add current node.right.
08:34So, in this way, our level order reversal is finished.
08:37Let's take a recap.
08:39We have created a small queue.
08:39We have created a root for the next line.
08:42We have added a root for the next line.
08:43Now we will run a loop until our queue is empty.
08:47So, in queue we will no longer be able to leave current node.
08:50If the current node is null here.
08:51So, in queue needn't print the next line.
08:55So, in queue is no longer.
08:56In queue needs to process another element.
08:58So, in queue is no longer.
09:00So, in queue needs to be taken one more.
09:02In queue needs to be taken on queue.
09:05So, in queue we have no longer.
09:10If we have null, we have printed the current node and checked the left child and checked the right child
09:18and checked the right child.
09:21In this way, we work with level order traversal.
09:24Now, we call in the main function of the tree for level order and pass the root.
09:31Let's save it and run it.
09:36Our output is 1, because the first level was only 1.
09:41Then 2 and 3, because the tree was only 2 and 3.
09:45Then 4, 5, 6.
09:47Because the third level was only 4, 5 and 6.
09:51In this way, we implement the level order traversal in our binary trees.