- 2 days ago
Transcript
00:00What do I have done in my main function? I have already constructed this tree.
00:04For this I have written code. I have my root tree.
00:07I will create a function on top of public static.
00:11I will create void type.
00:13And I will name it top view.
00:16In top view, I have a single parameter for my tree root node.
00:20In this top view function, I will print my top view of a binary tree.
00:24I will print it first.
00:27Because I know that I have to do level order traversal.
00:30So, I will create a cube.
00:33Why do I have to store what I have to do?
00:36Every node has two information.
00:39Every node has data.
00:42The node itself.
00:44Every node is associated with its horizontal distance.
00:47So, I have to store two information for any node.
00:51One is the complete node information.
00:54The other is its horizontal distance.
00:55So, why do I store the data structure within the node type?
01:00Now, what can I do?
01:01I can create a static class.
01:03I will name it info.
01:05This will store the whole information.
01:07What will it store?
01:11First, we store our node.
01:15The whole node information.
01:17The other will store the node.
01:18The node is the horizontal distance of node node.
01:21So, the node is the node node.
01:24The node name belongs to node node.
01:25The node is the node node.
01:26Here, I will create a constructor.
01:29This is for the node.
01:31and my horizontal distance, this.node is equal to my node and this.hd is equal to my horizontal
01:41distance.
01:43So I have started class with information name, already a information class exists here,
01:50I am going to do what I am going to do, I will comment out on the top of that,
01:56I will
02:00type key.
02:01I have started class with static class, now in queues I am going to do what I am going to
02:04do,
02:05information of the objects I am going to store.
02:07I will name the queue is equal to new, linked list, we will include the utility package
02:16to import java.util.all.
02:26First of all, why did I create a queue for level audit reversal?
02:30With my horizontal distance and nodes to store data, I have to create a map.
02:35So I will write hash map, this will be hash map of integer and node.
02:43Integer and node, why?
02:44value.
02:44Because integer will be my horizontal distance, which will be the map key.
02:49And node, which will be my value, will work in my key pair.
02:54I will name it map is equal to new hash map.
02:58So I will create hash map in this way, within Java.
03:02Besides, I should have two variables.
03:04One is my minimum variable and one is my maximum variable.
03:08I will initialize both values with zero.
03:13In the beginning, I want to add a queue in my root node.
03:17I will add a new information object to the root node.
03:20Which I will pass in my root.
03:22And with the horizontal distance of the root, which is always by default equal to zero.
03:28So I have added this.
03:29The extra thing that we add for level-wise traversal is a null node.
03:34The null node tracks where my single level is finished.
03:38So if the level-order traversal is clear,
03:40I am going to write the code, it will also be clear.
03:43If you have a little doubt about the level-order traversal,
03:46why do you do the work in which way?
03:48So you will revisit the previous video.
03:51We can try to dry-run a sample example.
03:54We can try to dry-run a sample example.
03:56We can try to dry-run it.
03:58So we should clear the basic code in the previous level-order traversal.
04:01We should clear the code so that we can solve this problem.
04:04So I have added a queue in my root node and the root node.
04:09Now I will process everything.
04:12This loop will run until I run until my queue is empty.
04:16First of all, what will I do?
04:18I will remove my current information.
04:20That will be queue.remove.
04:22When I have current information.
04:25First of all, I will check the null.
04:27In the current, the object of my information is a null load.
04:31So either my level is finished,
04:33or my entire loop is finished.
04:36So if my current is equal to null,
04:39what do I have to do?
04:41If my queue is empty,
04:44I will remove the null.
04:45I will remove the null.
04:46So in this case,
04:47I will break from this loop.
04:48If I do not,
04:49I will remove the null to the next level.
04:51So in the queue,
04:52I will add my null value.
04:55So in this way,
04:56I will check the null here.
04:59What can I do now?
05:00Now I can process my normal nodes.
05:03I will process my normal nodes.
05:03The process is that
05:04I will check the current node
05:07what the horizontal distance already exists in the map.
05:10If it does not,
05:11then I will add it.
05:13So if myMap.
05:15There is an additional function
05:18which we call contains key.
05:20We can check the contents key
05:21that no key exists in the map
05:24or does not.
05:26If the contents key
05:27returns true,
05:28that means the key exists in the map.
05:31And the contents key
05:32if it returns false,
05:33that means the key exists in the map.
05:35It doesn't exist in the map.
05:37So what I will do?
05:38What is my key?
05:39What is my key?
05:40What is my current horizontal distance?
05:42If my current information
05:44of my horizontal distance
05:45does not exist in the map
05:47then I will add it.
05:49So I will add it.
05:50So I will add the not operator.
05:52To not exist,
05:53it means that
05:54this is the first time
05:56my horizontal distance is occurring.
05:59First time,
06:00I will have the horizontal distance
06:01of the value of my horizontal distance.
06:02If it exists already,
06:04already,
06:04it means that
06:05in my upper level
06:06there is already
06:07a node exists in the map
06:09which is the same horizontal distance
06:10which is mine.
06:11So I will not include the top view
06:12in the top view.
06:14But because I am first time
06:15so I will include the top view
06:16in the top view.
06:17So I will add it in the map.put.
06:22I will add the key
06:24key will be current.ht
06:26and then add the value
06:29and then add the value
06:30and then the value
06:30will be the current node.
06:33So in this way,
06:34I will add the map
06:35into my key and value
06:37pair.
06:39This line is important
06:40because you can track
06:41that the first time
06:42which is the horizontal distance
06:43that is my top view
06:45node.
06:46Then I will check
06:47that if my current node
06:48is not left child
06:49null
06:50then I will add the key
06:51to the value
06:52to its horizontal distance
06:53track.
06:54So if current.left
06:57current.node.left
07:02is not equal to null
07:05first,
07:06I will add a new
07:08information object
07:09which I will pass
07:10to which I will pass
07:11root
07:11and this time
07:12current node
07:14left
07:16and
07:17horizontal distance
07:18will become the current horizontal distance is minus 1. Now I compare this distance with minimum
07:24and maximum I will not compare with both here because we assume current dot hd
07:29that will already compare. Now the horizontal distance of left child is obviously minus 1
07:34and it will be more degrees. That's why we can compare it with minimum
07:37and maximum also but it will not make sense here because maximum value
07:43left child will never change as minimum value will never change from the right child.
07:48So I will make the minimum. Math dot minimum of minimum and current horizontal distance
07:55minus 1.
07:58Besides right child also, current k node's right is not equal to null.
08:05If the right child is not null, then I have to add new information.
08:17Now I will compare this distance with maximum. Math dot maximum of max and current dot horizontal distance plus 1.
08:27So I have to compare this distance with left child and right child.
08:32And I have to compare the horizontal distance with minimum and maximum variable.
08:37Here I have checked the horizontal distance first time. This is my null condition.
08:42So this is my level audit reversal is complete here.
08:46But until now I have not done my top view.
08:48So I will print the top view to top view. I will run a loop.
08:51I is equal to minimum.
08:53This will go until I is less than equal to my maximum.
08:57I plus plus.
09:00Every time I will print the value.
09:02My map dot.
09:03Get.
09:04I will be the horizontal distance for which I will print my node.
09:09So I will pass to get I.
09:11I is basically my key.
09:13Now I will return the value to my map.
09:17I will not print the node.
09:18I will print the node.
09:19Because the node is an object.
09:20It will print it.
09:21So it will print it.
09:22So it will print it.
09:23That's why I will print the node's data.
09:24So I will print the node's data.
09:25In this loop,
09:26I will print the minimum horizontal distance to maximum horizontal distance.
09:30I will print all nodes.
09:32And print it.
09:33I will print it.
09:34Here I will give a space.
09:36And last make next line.
09:39I will do save.
09:40And I will summarize it.
09:42Summary is something like that.
09:44I will use two concepts.
09:45One is horizontal distance.
09:46One is map.
09:48What I have done before?
09:49Level order traversal.
09:51So I have created the queue.
09:53Because I have stored the information in the queue.
09:55So I have created a new class.
09:57Information.
09:58In which I will store my node.
10:00And the horizontal distance.
10:01I have created the minimum and maximum.
10:05With this 0.
10:06Now I have added the queue in the queue.
10:08And the horizontal distance 0.
10:10And with that, I have added the null value.
10:13Now I will run a while loop.
10:14Until my queue is empty.
10:16Every time I will remove the element.
10:18I will store it in the current information.
10:20If the current is null.
10:22These are the simple conditions.
10:24Which we did in level order reversal.
10:26What will I do?
10:27I will check for the current.
10:31If I have done that.
10:32I will do that.
10:32If I can do that.
10:33So I will add it.
10:35First time distance is the top view.
10:38This means I have to come in the top view.
10:38So I have put it in the map.
10:40I have put it in the current.
10:42And current node.
10:44Then I will check for the left child.
10:45If it doesn't have to be null.
10:46Then I will add it in the queue.
10:47And then I will add it in the queue.
10:48That will become the horizontal distance.
10:49That will become the current distance.
10:52And then the minimum.
10:53And then I will compare it in the same way.
10:55If the file exists, then add it in the Q and the horizontal distance will become the current horizontal distance
11:01plus 1
11:01And compare it with the max
11:04Last, I played a loop with minimum HD and maximum HD
11:08And for everyone, I got a particular key to get the map from the map
11:13And I printed the data
11:16Let's save it
11:17And in the main function, I will call for this particular tree
11:19I will call the value
11:20I will call the top view
11:23I will pass the root
11:26Let's save and now run
11:31Exception
11:32That HD is not defined
11:34Which line is above 140
11:35What do I have to do?
11:39These three lines
11:40I need to include
11:42Why do I need to include
11:45If my current value is null
11:47So, in this case, we will execute these lines here
11:50And the current value is null
11:52So, neither the node exists nor the horizontal distance exists
11:56So, I take an else condition here
11:59And I will store these three lines
12:02So, I need to include the else
12:05So, I need to include the two lines
12:08So, let's run
12:10So, let's run
12:13So, this time the top view of the binary tree
12:16This particular tree
12:17I have printed
12:18First, 4
12:20Then, 2
12:21Then, 1
12:22Then, 3
12:23Then, 7
12:25So, the top view is
12:266
12:274
12:272
12:271
12:273
12:277
12:295
12:295
12:315
12:315
12:325
12:325
12:335
12:335
12:335
12:335
12:335
12:346
12:345
12:346
12:355
12:385
12:385
12:506
12:506
12:507
12:517
12:517
12:518
12:518
12:5110
12:5111
12:5111
12:5212
12:5214
12:5214
12:5315
12:5315
12:5316
12:5316
12:5315
12:5317
12:5320
12:5319
12:5320