• 2 days ago
Transcript
00:00Now we are going to learn how to traverse in the tree.
00:03The famous way to traverse in the tree is called Pre-Order, In-Order and Post-Order.
00:10And the way to traverse in the tree is called Level Order Traversal.
00:15First of all, we will talk about Pre-Order Traversal.
00:17This will also be clear that in the last video, when we talked about Pre-Order Sequence.
00:21How did the sequence come?
00:23In the tree traversal, the first way is our Pre-Order.
00:27There are three rules.
00:28The first rule is to print the root.
00:31The second rule is to print the root to the left sub-tree.
00:35The third rule is to print the root to the right sub-tree.
00:39If we take this example, first of all, we have to print the root.
00:43We will print the root to the root.
00:45Then we will print the root to the left sub-tree.
00:49When we come to the root, we will come to our 2.
00:53We will do the same thing too.
00:55Now, we will print the root to the root from the left sub-tree.
00:57Move to the root 12.
00:58After, we have to print the root in our 2.
00:59Then we go second to the root on my 3.
01:03When the root could be 4, we will print it on the left sub-tree.
01:21So 2 right subtree is 5
01:22So now we print here 5
01:25Now 5 left child
01:27So he doesn't do that null
01:29So 5 right child is null
01:30So 5 is back to 2
01:322 is back to 1
01:34Now we go to 1
01:36We have 1 to print it
01:40Now we print it
01:421 to right subtree
01:441 to 3
01:481 to 3
01:49Now we print 3
01:51Then we print 3 left subtree
01:53But it doesn't exist
01:54Then we print 3 right subtree
01:576
01:57Then we print 6 left subtree
02:00Which is null
02:00Then 6 right subtree
02:02Which is null
02:03Now from 3 to 3
02:04Everything is printed
02:063 to 1
02:07Everything is printed
02:08So our pre-order sequence
02:10That is equal to
02:121,2,4,5,3,6
02:15We have followed 3 simple rules
02:17First we print
02:18And we print
02:19Which is not the root
02:21In the root
02:23Then we print
02:25And then we print
02:26Then we print
02:26Literally
02:26And we print
02:28And now we print
02:29We print down the tree
02:40Printer
02:41Which is the same
02:42And why do we print
02:47Public, Static, Void, Pre-order
02:52We are going to pass our root node
02:55So there are three rules of pre-order
02:57The first rule is to print the root
03:01So we are going to print the root of data
03:05We are going to give a space in the end
03:08The second rule is to print the root of left sub-tree
03:11Which one will print the left sub-tree?
03:13It will not be a pre-order recursive function
03:14So the pre-order function will pass the root of left
03:20After that we will print the root of right sub-tree
03:24So we will print the root in pre-order
03:29The base case will be that if our root is null
03:34We are going to try to access the leaf child
03:37Or our tree is empty
03:40In that case we will not print anything
03:42We will not do it
03:42We will simply do it
03:44Now we will save it
03:46And what we have built the tree
03:48What we will do
03:50What we will do
03:50We will print the pre-order
03:55Pre-order
03:56Pre-order
04:05So we will print the root of left sub-tree
04:08We will print the root of left sub-tree
04:08We will print the root of left sub-tree
04:26Now we will print the root of left sub-tree
04:35Now we will print the root of left sub-tree
04:37Now we will print the root of left sub-tree
04:56To talk with the root of left sub-tree
05:03To talk about the root of left sub-tree
05:05To talk about the root sub-tree
05:05We have crossed all the nodes
05:08So we have gone one time
05:10If we have n nodes, we should have time complexity big of n.
05:15Our pre-order is linear time.
05:18Big of n is time complexity.