- 2 days ago
Transcript
00:00Next question which we will talk about is Top View of a Binary Tree. This is a very important question
00:05because we are going to use a small data structure in a chapter.
00:10So, I am giving you a brief here because when we use that data structure, we can efficiently solve this
00:17question.
00:18I have not done this question yet because we are studying trees and the topic is fresh.
00:23So, now we will understand this question. So, we will remember more for long term. Notes and trees will become
00:28more.
00:29Otherwise, we want to solve this question when we are studying maps.
00:35Because there is a data structure called maps, which we will study dedicated chapters.
00:40So, we will study all the basic things later.
00:43But I will give you a small brief in this chapter so that we can solve this question.
00:49Top View of a Binary Tree is that if I have a binary tree for me given,
00:54I will see this binary tree from above.
00:57I mean, there is a lens on top.
00:58So, I will see this binary tree from above.
01:01So, I will see which nodes of tree of nodes.
01:04If I will print those nodes, then it will become my binary tree from top view.
01:09As we see the nodes of node.
01:11And, if I see our binary tree again, then I will show this node.
01:26then I will see this node. Then I will see this node. Then I will see this node. Then I
01:33will see this node. Then I will see this node.
01:34This 5 and 4 will not see me. Why does not see me? Because actually my tree structure form, first
01:40if I have 2, here is 4.
01:45So my 5 is exactly my 2. Then I have 2 on the right.
01:52I will see this node. So my 3 is exactly the same way.
01:58If I talk about actual tree structure, then I have seen you differently for understanding.
02:03But if you make it properly, then my 4 and 5 overlap and the same position.
02:11And my 3 will be 6.
02:146. So, if you look at this level, you will see 4 on this level, because it doesn't exist
02:21on this level. In this level, you will see 2 on this level, it doesn't exist on this level.
02:25In this level, you will see only 1 on this level. 5 for 4 will not be able to see
02:295 for
02:294, because it will get stuck from 1. Then, I will see 3 on this level and 6 on this
02:34level.
02:35Therefore, I will see the final output. I will write 4 for 2, then 1, then 3, then 6.
02:40So, the top view of this binary tree is going to be 4, 2, 1, 3, 6. Next, I will
02:45take an example
02:46of my other binary tree. If I take this example of this tree, what will be the top view?
02:53First, I will put a lens on this level, and we will see from this level.
02:58When we see from this level, I will see this node, which is 2. Then, I will see this node.
03:05Now, from this node directly below, I will have 4. So, this 4 will never see me in top view.
03:12Then, I will print my 1. Then, I will see this node. Then, I will see this node.
03:16Then, I will see this node. From this node, I will see this node.
03:18From this node directly below, I will have 5 node. Which I will never see.
03:22So, I will print 3 in my output. Then, I will print the node in next level. That is equal
03:27to 6.
03:28So, I will print 6. So, from this type of output, that will be equal to 2, 1, 3, and
03:356.
03:35So, my node is 4 node. And my node is 5 node.
03:39In this tree, I will also find these nodes. Now, as I have top view of a binary tree,
03:44I also have bottom view of a binary tree. So, if I put the lens at the bottom,
03:49I will put it in a problem. But, we will not discuss that question.
03:51We will not discuss that question. It will get you in a homework problem.
03:53If there is no homework assignment included,
03:55then, you have to do that question yourself.
03:57Because, we will use the same logic within it.
04:00We will use the same logic. We will solve the same way.
04:02We will solve the same question. Top view, which I have understood,
04:04the bottom view is not going to be difficult.
04:07So, before we discuss the top view of a binary tree,
04:09we will discuss the approach in a very briefly,
04:12we will discuss the hash map.
04:14The map is a data structure.
04:17The map is a data structure which stores the pairs.
04:21The C++ name is a pair.
04:23The pair type of data structure is a pair.
04:25The pair means a pair.
04:27The map is two values.
04:29The one value is a key.
04:31The one value is a value.
04:34The one key is a value.
04:37For example, if I take my countries' data,
04:40I have many countries here.
04:44And every country has its population.
04:47The first country is China.
04:49I have a population of China.
04:51We have a population of 1 to 5.
04:53Then, India.
04:55The population of India is 100 equal.
04:58Then, I have the US.
05:00The population of US is 50 equal.
05:03So, in this way,
05:05every country will be a key.
05:08And its population will be a value.
05:11Now, there are some properties that are related to hash map.
05:14When I store this type of data,
05:15this key value is a pair.
05:17I store it in hash map.
05:20I store it in the form of hash map.
05:21Here, the key is a unique value.
05:23And my size is the value that is equal to a unique value.
05:30In the sense of this,
05:32the country in each country is not a name.
05:36The name of a country is India.
05:38Even a country is the name of the US.
05:40It's not that the two countries' name of the same will not be the same.
05:42So, that way,
05:43the country becomes the key.
05:45The key is always unique.
05:47But, if I talk about the population,
05:49So it could be Nepal and Bangladesh's population same.
05:53I don't know if it's the same thing or not, but in example, if we assume it's the same thing,
05:58then it's possible to be possible.
05:59I mean, Nepal's population, I have taken 5k equal to this,
06:02and Bangladesh's population, I have taken 5k.
06:05So both of these values will be the same.
06:07But the keys will never be the same, the keys will always be unique.
06:11So when we form data in a structure,
06:17we try to form that data structure, we call that map.
06:20What does the map do?
06:21Pairs form, pairs store, pairs are our key and value pairs.
06:26Now when we create map,
06:28the syntax of the syntax remains something like that.
06:31If I want to create the map,
06:33first of all, you have to import java.util.hashmap,
06:42or then I can import all here too.
06:45Like I always use the keys or the stacks,
06:47we import all,
06:49so we have to import utility package.
06:51And for creating, we write hash map.
06:55Maps actually have 3 types.
06:57One is hash map, one is linked hash map, one is tree map.
07:00For using this question, we have enough hash map,
07:03so I will not discuss the rest of the discussion now,
07:05because there will be confusion.
07:06We will discuss the rest of the next chapters in detail.
07:10So the hash map is created,
07:12the structure of the structure is created.
07:13First I write hash map.
07:14Then I define what type of data will be stored.
07:17If the key type is in it,
07:20because I store country,
07:21then I can give the key type, string,
07:23and then I can give the values type, integer.
07:27So I will write here,
07:28first of all,
07:29the key type,
07:30or string.
07:31Then I will write your population type,
07:34or integer.
07:36Then I will write your map name,
07:38is equal to new hash map.
07:43So this is the syntax of this way,
07:46when we create a hash map within Java.
07:49What will happen internally?
07:50This will be a data structure form.
07:52Basically, we can imagine it,
07:54a table will be formed.
07:56I will have two parts of the table,
07:58in which one side will be countries,
08:01and one side will be population stored.
08:04So this table type of data,
08:06I can imagine it,
08:08in which one side will be created in Java.
08:10And I will write this statement.
08:13If I have such a data,
08:15in which two sides are integer and integer,
08:17then I don't have to do anything.
08:18Simply, in this string,
08:19I will write the key as integer.
08:21But we always have to focus on
08:23that whatever we store the values in the map,
08:26the values are unique.
08:27The key value is unique.
08:29The key value of the corresponding pair,
08:31that can be our duplicate.
08:33But the key is always unique.
08:35If you have done this,
08:37if you have done this already,
08:38if you have done this for China,
08:40there was a population of 150 stored.
08:42And then, what did you do?
08:43If you have done this again in China,
08:45you have done this for 180 population store,
08:47then you will not be able to create new China.
08:50You will have to remove this.
08:51You will have to remove this 180 value.
08:53This is the way we work on the map.
08:55So, if you have done this duplicate value,
08:58then you will not be able to add this map.
09:00The map is very efficient data structure.
09:02So, if you have done this,
09:04the time complexity of the map is also constant.
09:07The time complexity of the map is also constant.
09:10The time complexity of the map is also constant.
09:11And the time complexity of the map is also constant.
09:13That is also constant.
09:15That is why maps internally
09:16have implemented the hash table.
09:19Or, I can say that,
09:21the logic of the hashing.
09:23Hashing is a very beautiful concept,
09:26when we talk about programming in general.
09:28Also, if you have a hacking background,
09:31you have done hacking,
09:32password, injection,
09:34or those things,
09:35then you will learn hashing.
09:37Hashing is nothing but,
09:39we have passwords for Facebook,
09:41or Instagram,
09:43or WhatsApp,
09:46but if you have a big organization
09:48or a good level,
09:49who has made a tech,
09:50they have to store your password.
09:52They don't directly store your password.
09:55They have to hash your password first.
09:57It means,
09:59change the form of password.
10:01There are different hashing algorithms
10:03that change the form of password.
10:04So, if you have a password,
10:07you have to change the form of password,
10:08and you have to change the form of password,
10:10and you have to change the form of password,
10:11and you have to change the form of password.
10:14So, in case,
10:15if a company's data is leaked,
10:17or a hacking attack,
10:19then the attackers will have this hashed form.
10:22Your password will not get the original password.
10:25So, this is basically used to protect information.
10:29So, the concept of hashing,
10:31we also talk about hash maps,
10:32hash sets,
10:33we will discuss in detail.
10:36I have briefed you very little.
10:38Hash map, hash set,
10:39all these are great data structures.
10:41They are working in constant time,
10:42and there is constant time complexity.
10:45And after that,
10:46we will also learn in the next chapters,
10:47how this time complexity is constant.
10:51But for now,
10:52we can just remember that
10:54we can add and get in constant time.
10:56Now, if I have learned to create a hash map,
11:00if I want to add a hash map,
11:03I want to add to the hash map,
11:04then I will use the put name function.
11:06So, I will do map.put.put.
11:10First, I will pass the key
11:12and then pass the value.
11:14So, I will write the key map,
11:16then I will call the .put method.
11:19First, I will pass the key to the value,
11:21then I will pass the value to the value.
11:23I will pass the value to the data.
11:26And the third function is
11:27that I have a get.
11:29If I have a get,
11:30then I will write map.get.
11:32I will pass the key.
11:34So, if I need the key to the value,
11:36I will return the value to the value.
11:39So, the key to the through,
11:40we can get the constant time.
11:42If I have a get in my key,
11:44then I will return the corresponding value.
11:48These three things are important for this question.
11:51I will give a little brief.
11:53I will give a little brief.
11:54I have a little brief.
11:55My name is hash map, Java,
11:57which is stored pairs.
11:59It is stored pairs.
12:02And we can visualize it in a table,
12:05in which one is key stored,
12:06one is value stored.
12:08One important thing is that
12:10the key is always unique.
12:12Duplicate is never possible.
12:13If you try to put the key here,
12:16then it will not do anything.
12:17The previous key will keep the same
12:18and replace the value.
12:20So, the key will always be unique.
12:22Three functions are important for me.
12:24First of all, create.
12:26We write such statements.
12:28Then, put function.
12:29I will use the .put method.
12:32I will pass the key and value.
12:33Then, get function.
12:35I will pass the .get through key.
12:37I will pass the key.
12:38I will return the key.
12:39I will pass the corresponding value.
12:42If I have the key as China,
12:45then I will return the 125.
12:47If I pass the key as India,
12:49then I will return the 100.
12:50If I have put here,
12:52then I will write map.put.
12:55I will pass India.
12:57And I will pass the key.
12:58I will pass 100.
12:59Then, I will return the row to my map.
13:03So, in this way,
13:05my hash maps work in Java.
13:07The other thing I have to understand
13:08is the question to solve for me.
13:10It is the horizontal distance.
13:13In the trees,
13:13there is a horizontal distance.
13:16By default,
13:18the root node,
13:19the root node,
13:22the horizontal distance is 0.
13:25If you see the number line,
13:26if you see the numbers,
13:28how do you see the number line?
13:29The number line is like,
13:30which looks like,
13:31in which my origin,
13:34the number is 0.
13:37After that,
13:38when we talk about positive numbers,
13:391, 2, 3, 4, 5, 6,
13:42then all the positive numbers
13:44are in the right side.
13:46They exist on the right side.
13:47They exist on the positive x axis.
13:49But if I talk about negative numbers,
13:51like minus 1, minus 2, minus 3, minus 4,
13:55these numbers exist on the left side.
13:58These numbers exist on the left side.
14:01As we learned the number line,
14:02we have learned the number line in math.
14:03That is the horizontal distance
14:04in the trees.
14:05But here,
14:06we can say,
14:07the number line in the tree,
14:08is like this.
14:09Where the origin,
14:11or the 0,
14:12the 0,
14:12the 0 node,
14:13the 0 node.
14:15The horizontal distance will be equal to 0.
14:18Now,
14:18the right side of the nodes,
14:21the horizontal distance will be positive.
14:23And the left side of the node,
14:24the horizontal distance will be negative.
14:27So,
14:28if I want to calculate,
14:28I want to calculate,
14:29the root of the rightXT tree.
14:32What I do here,
14:33the base line is at scale.
14:34there will be a basic line here
14:35The horizontal distance means 1 to equal,
14:39First this node,
14:40directly from this node,
14:41the horizontal distance means 1.
14:43Then chocolate like this line.
14:44Just horizontalanntle hg both the homogeneous lines,
14:47you should remove this direction,
14:51which is 0 to the precinct value.
14:52Because here,
14:52If I move one level, this distance is equal to 1
14:57So I moved 1 to 3 and 5
15:00In the same line, I moved 2 to 6
15:04So this horizontal distance is equal to 2
15:07If I move 1 to left side, I moved 1 to 1
15:12To reach node number 2, this horizontal distance
15:16will be equal to 1
15:18So this horizontal distance concept
15:20With which I can find out
15:22Which I can find out
15:23Which nodes I have
15:26I have all my nodes
15:28For my nodes
15:29If I move the horizontal distance
15:32This node is equal to 1
15:33This node is equal to 0
15:35This node is equal to 1
15:35This node is equal to 1
15:40If I start to traverse
15:41In our tree
15:43The first node
15:45Which I will encounter
15:47The first time for the horizontal distance
15:49Calculate
15:50That will include my top view
15:52If I start to go to the top view
15:55First I will go to the one node
15:57The horizontal distance is 0
16:01Horizontal distance
16:02And my node
16:03The distance 0 is the first time
16:06Encounter
16:06What will happen?
16:08I store my node 1
16:09Next I will store
16:10After the left
16:12I will calculate the way
16:13Horizontal distance
16:15The horizontal distance will be equal
16:17The width of node 2
16:18The width of node 1
16:21As I will store the width of node 2
16:22As I will go to the right
16:23The width of node 1
16:26This horizontal distance
16:28The width of node 3
16:30Back in the top view
16:32Then when I go to this node, I will add 0 to this
16:35I will not add 0 because 0 has already added
16:39After that, 1 will come, 1 will also add 1
16:42After that, 6 will come, 6 distance is 2
16:45The first time I will add 6
16:47So in the top view of a binary tree, I will have 4 nodes
16:51Which will be 1, 2, 3 and 6
16:54So I will print 1, 2, 3 and 6 as my top view of the binary tree
17:00So I have to do it
17:01To search for horizontal distances
17:03Which will be the first time
17:05And to do it
17:07Now in the horizontal distance
17:09If I have any node
17:12Horizontal distance
17:14Let me tell you that I have a node
17:16And this node exists in the left sub tree
17:18And this node exists in the right sub tree
17:22And this node exists in the right sub tree
17:24So if I can say it
17:25Horizontal distance X
17:26Because
17:28Because this node is 1 distance
17:30It is further
17:32So it will be horizontal distance X-1
17:34And this node is 1 distance
17:37So it will be horizontal distance X-1
17:41So if I have any node
17:43Horizontal distance X-1
17:44Horizontal distance
17:44Then left child
17:47This is the child I am talking about
17:49Horizontal distance
17:57Horizontal distance
17:58So we will calculate the horizontal distance
18:00Horizontal distance
18:00And go up to different nodes
18:02If I start with root
18:04Then the horizontal distance
18:07By default we always take 0
18:09Horizontal distance
18:10Horizontal distance
18:10Horizontal distance
18:11Horizontal distance
18:27Horizontal distance
18:27How much is menos 1
18:28Plus 1
18:29For X-1
18:30So it will be 0
18:32With this left
18:33The power of i-1
18:35And the power of i-1
18:36And it will be 0
18:37Horizontal distance
18:38Horizontal distance
18:38So there will be 0
18:38Which is equal to 1
18:40And then the power of i-1
18:43So this is the power of i-1
18:46And the power of i-1
18:50And then the power of i-1
18:56So this way, we can traverse all the roots from the root and every node we can calculate the horizontal
19:03distance for each node
19:05So the approach is to remove the top view of a binary tree
19:07We will use two things which we will use today
19:11First of all, we will use our maps, hash maps
19:15Second of all, we will use our horizontal distance property
19:18We will use these two things and we will calculate our top view of a binary tree