• 2 days ago
Transcript
00:00Next question which we will talk about is the diameter of a tree. What is the diameter of a tree?
00:05What is the diameter of a tree? We will hear the diameter in circles.
00:08When we take a circle of example, it was a radius and the double amount of radius,
00:13which is equal to a circle of a circle, this is equal to the diameter.
00:19We also define a property in this tree, which we call diameter.
00:24If I talk about proper definition of diameter,
00:26the diameter is defined as the number of nodes in the longest path between two leaves.
00:42This means that if we take a binary tree,
00:45we take two leaves,
00:47and we take a path between two leaves,
00:52which leaves the longest path of those nodes.
00:57What can I say?
00:58If I take this tree,
00:59if I take this leaf and this leaf,
01:03then the path between these two?
01:05It will be equal to this path.
01:09If I take this path between four and five,
01:10then how many nodes will be in it?
01:12The nodes will be equal to one, two, and three.
01:15This is not the longest path.
01:17But if I take this path to four,
01:19and I reach six to six,
01:20then from four to six,
01:22there will be something like this.
01:24In which there will be many nodes?
01:25One, two, three, four, five.
01:27So, this way,
01:28the diameter of this tree will be equal to five.
01:31The diameter of this path between four and six,
01:33or five and six,
01:37the same number of nodes will be equal to five.
01:41So, the diameter of this tree will be equal to five.
01:44That is equal to five.
01:46If I take the second tree of this example,
01:47in this tree,
01:49the leaves,
01:50I try to take a path between nine and three.
01:52and I try to take this path behind me.
01:54This path will look like this.
01:57How many nodes will be?
01:58One, two, three, four, and five.
02:01So, there will be one possible value,
02:03which I have,
02:03that is equal to five.
02:05What is the other possible value?
02:06What is this leaf and this leaf?
02:09This path will look like this.
02:10That path will look like this.
02:13So, there will be nodes.
02:14One, two, three, four, five, six.
02:17So, a parallel value will be 6.
02:19Now, what do I do?
02:20In this node.
02:21In this node.
02:22In this node.
02:22In this node.
02:22In this node.
02:22In the leaves,
02:23If I loose the path,
02:24that will be equal to each of these two leaves.
02:25One, two, three, four, five, six.
02:29This value will also be six.
02:31In this value,
02:32What?
02:32Maximum is equal to six.
02:34I can say,
02:34that my tree diameter is 6.
02:39That is six.
02:39to 6. Now let's say that if we have another node which is equal to 10, this is my tree
02:48diameter. This is fixed diameter and diameter is made in case 7. When we calculate tree
02:57diameter, usually we have two cases. The first case is in which my tree diameter goes
03:04to the root node. In this case, my tree diameter will include my root node. The second case
03:12is when the diameter of a sub-tree exists and it doesn't go through root node. For example,
03:20if I want to pass through a line to leaves, it will not become my diameter. My longest
03:28path will be between any two leaves. This path will be in this path. In this path, my root
03:33node is not included. So, there are two special cases. If I have a root of a tree, then
03:39either my entire tree diameter can go through or put it in a sub-tree or put it in a
03:45sub-tree.
03:46Recursively, I have to calculate that if the root of a tree is through or exists in a sub-tree.
03:56This is why, first of all, we will discuss the approach number 1. For the approach number 1, we will
04:01discuss the time. We will discuss the time with n-square. This is the n-square approach. If I have
04:09any tree node on any tree node, I have left to left sub-tree and right to right sub-tree.
04:17So,
04:18I can remove possible diameter which will be through. If I have left sub-tree and right sub-tree
04:26I have my diameter. If the roots are through, then my diameter will be equal to left height
04:32plus right height plus 1.
04:34Why? Because in diameter there will also be these nodes and the nodes. These nodes also
04:38include them. Plus the additional node of my node. So, if I have any node for a
04:44value, you can see it. Now, if I have left and right height to some node, I have a
04:48assumption kiya ki my diameter passes through root, to us case me my diameter hooghi that
04:57will be equal to left height plus right height plus one, aur agar mein dousra case loo
05:03jis me diameter doesn't pass through root, to us case me my left subtree ke under already
05:10coi diameter calculate ho chukhi hooghi, aur my right subtree ke under already coi diameter
05:15calculate ho chukhi hooghi, to me kya karu, left subtree me jho maximum possible diameter
05:19is, aur right subtree ke under jo maximum possible diameter is, aur phir douno ki jho maximum
05:26value hooghi, usko mein is value ke saath compare kari loo, yaani, meinne kya kiya, apne left
05:32ki diameter nikaal li, aur meinne apne right ki diameter nikaal li, aur phir in douno values
05:38ko is ke saath compare kari dya, in tino me se jho maximum value a jaayegi, woh meire
05:43tree ki diameter ben jayegi, maan lietay meire paas kuch aisa tree exist karta hai,
05:48jiske anndar bohut sare kuch aise nodes hai, agar mein apne is node par hoon, to meera
05:57ye wala left subtree kya karega, ek toh, iski mein height nikaal li, ek iske liye mein diameter
06:03nikaal li, toh, sabse pahle mein call laga dung ki apne left subtree ke liye, uski diameter
06:07calculate karne ke liye, toh phir meera jho tree ben jayegga, woh kuch aisa ben jayegga,
06:11jis mei mein is root par a jaungi, ye root kya karega, apne diameter calculate karega,
06:16kya mein kaya sakti ho, is tree ki diameter hai, one, two, three, four, five, six, seven
06:20ke equal, toh, yaahan se meere paas return ho kar ayegi value as seven, aur agar mein
06:24apne right subtree ki diameter calculate karu, ismei meere paas single node hai, toh, iski
06:28diameter ben jayegi one ke equal, toh, right subtree return kar dega mujhe one, ab
06:33mujhe eek aisi diameter nikaal liye, joh, meere is node ke through jati ho, toh, uski
06:37value ho jayegi, yaahan se, joh maximum possible height hai, woh a jayegi, right se maximum
06:42possible height a jayegi, aur usmei me include kar dhu, apne node ko, yani, mei iski
06:46agar height nikaal hiu, toh, left height ho jayegi, four ke equal, right height
06:50meere paas nikal kar ayegi, one ke equal, aur meeri joh khud ki diameter ho ghi, yani joh
06:55is tariqe se pass karayegi, us diameter ki value meere paas ho jayegi, self diameter, that is
07:01equal to four plus one, left height plus right height plus one, this value is equal
07:05to six, aur meeri joh left diameter hai, usne already kya return kiya, usne already
07:09mujhe seven return karke diya hai, aur meeri right diameter ne, mujhe already one return
07:14karke diya hai, toh, in teen ho meinse joh maximum aega, woh meere tree ki diameter
07:19ben jayegi, right diameter ki value kahti hai, ki meri diameter aisi dikhni chahiye, jis
07:23mei meri paas single node ho, left diameter kahti hai, ki meri diameter aisi dikhni
07:27chahiye, jis mei meri paas ye values include ho rhi ho, aur diameter, joh meri root ke
07:33through jati ho, woh kya rahi hai ki meri diameter aisi dikhni chahiye, jis mei meri
07:36root bhi included ho, aur inki values hai six, seven and one, toh isme so obviously,
07:41meri joh tree ki final diameter baneegi, that will be equal to my left diameter, yani
07:46is puray tree ki joh meri paas diameter baneegi, woh kuch aisi dikhayegi, jis mei total number
07:50of nodes meri paas seven ke equal ho, toh jab bhi hum tree ki kisi bhi node par ho
07:54te, toh meri paas three choices hoti hai, pehli choice ye hai ki meri tree ki diameter meri
07:59left subtree ke anndar kahin exist kar jai, dousari choice meri paas ye, meri tree ki
08:03diameter meri right subtree ke anndar kahin par exist kar jai, aur tisari choice meri paas
08:07ye, ki meri tree ki diameter meri joh root hai, us node ke through pass kerti ho, ye
08:13tino ki mujhe value calculate karni hai, har ek node par jake, aur unke liye, jab
08:17mein maximum nikaal loo, toh that will become the final diameter of my tree, is puray
08:22process ko ek baar dry run karte hai, phir uske baad sudo code iske liye likh kar
08:25dekhenge, mujhe kya karna hai, har node par jake kar tine values calculate karni hai, sab
08:30se pahle mein four par agar jau ho, four par kyun gai, kiunki recursively pehle
08:33yam left subtree ke liye calculate kar lye teh, phir right ke liye, toh one kya
08:37karega, left ke liye jayega, toh two pa ra jayega, two, phir four ke liye calculate
08:40karrega toh four par a jayega, toh four ke liye, agar mujhe calculate
08:43karna hai, four ke liye left subtree ke joh diameter ho, jayegi that will be equal
08:47to zero and iski height bhi ho, jayegi zero, right subtree karega, toh iski
08:52right diameter bhi ho, jayegi zero and right height bhi ho, jayegi zero, toh four
08:56kya karrega, four dekhe ga ki meri diameter, kya meri left subtree
08:59me laye karti, right subtree me laye karti e, ya meri through ho kar jati
09:02a, agar left subtree me laye karti e, toh zero value eski, right me laye karti
09:08it should be equal to left height plus right height plus 1. So it will be 0 plus 0 plus
09:161.
09:17So 4 has 3 choices in value. 1 is 0, 1 is 0, 1 is 1. 0 is choice when
09:24left diameter is my final diameter.
09:270 is choice when right diameter is and 1 is choice when self goes through.
09:35So 4 will return 2 to 1 as final diameter. So 1 will become left diameter.
09:42Who will become left diameter? 2 will become left sub-tree.
09:46With the diameter I will calculate left height here. This sub-tree will return 1 to equal.
09:54Now what will happen to you? Right sub-tree will go to right sub-tree.
09:575 will go to 5. Left diameter is 0, left height is 0. Right sub-tree, right diameter is equal
10:04to 0, right height is equal to 0.
10:06What will happen to you? It will calculate values.
10:09Values will come. 1 will become left diameter, 1 will become right diameter, 1 will become self diameter.
10:15Left diameter is 0, right diameter is 0. What will become self? 0 plus 0 plus 1 is equal.
10:21So here will be 1. So here will be my final value. So 5 will return 2 to 1 as
10:28its final diameter.
10:30So 1 will become right diameter and right height is equal to 1.
10:34Now what will be 2? 2 will calculate its own diameter. So 2 will calculate its own diameter.
10:39First of all, left diameter will be left. Left will come from 1. So 2 will have 1 choice.
10:45What is the right diameter? What is the right diameter? What is the right diameter?
10:49What is the right diameter? 1 is equal to 5. So the other choice is 1.
10:53Now self diameter will be what will happen. Self diameter will be left height plus right height plus 1.
10:59Which is equal to 1 plus 1 plus 1.
11:03So self diameter will be 2 in 3.
11:06Here will be maximum 3. So what will happen to you? 3 will return 3 as its final diameter.
11:13So left diameter will be left here. I will remove them for clarity.
11:18Now left sub tree has 1. Left diameter 3 returned 3.
11:23Which diameter is? It is this diameter.
11:25Which is left sub tree. Now the left sub tree will return height.
11:29Height will be equal to 2.
11:31Because from here, the height is equal to 2.
11:34Now 1 will ask from right sub tree.
11:36Right sub tree will go to 3.
11:38First of all, 3 will go to left sub tree.
11:40Left sub tree has left diameter returned 0.
11:43Left height returned 0.
11:44Now right sub tree will ask.
11:46What will happen here?
11:48Left diameter will also be 0. Left height will also be 0.
11:51Right diameter will also be 0. Right height will also be 0.
11:54So in this case, 6 will return 1 as final diameter.
11:59Because 6 has 3 choices.
12:010, 0 and 1.
12:03So 6 has returned 1.
12:04Now what are the choices?
12:063 has the first choice.
12:07Left diameter is 0.
12:09Because 3 has left left is 0.
12:11Now right sub tree has the choice.
12:14Because right diameter is 1.
12:16That will be equal.
12:16The other choice is to remove self through diameter.
12:19Self through diameter is 0.
12:21So that will be left height plus right height plus 1.
12:24That will be equal to left height plus right height plus 1.
12:27So here 3 has the right height.
12:29That was equal to 1.
12:31This is my right diameter.
12:32So here value will be self through diameter.
12:35Which will be this diameter.
12:37So here win will be 2 value.
12:40And 3 will return 2 as its right diameter.
12:44And right height will be equal to 2.
12:46These things will be equal to 2.
12:49So finally, what do you have to do?
12:50What do you have to do?
12:51What do you have to do?
12:52Left height is 2.
12:53Left diameter is 3.
12:56Right diameter is 2.
12:57And right height will be equal to 2.
12:59So here's the right height is 2.
13:00Left height has something like this.
13:02And right height looks like this.
13:04Now you will calculate 1.
13:06First of all, you will know.
13:09Left diameter will be 1.
13:11Left diameter will be 1.
13:113 will be equal to 1.
13:12Right diameter will be 1.
13:142 will be equal to 2.
13:15And self diameter will be 1.
13:17Left height is 1.
13:18Right height will be 1.
13:20This is equal to maximum of 3, 2 and 5.
13:24So value will be 5.
13:26So this tree will be removed from diameter that should be equal to 5.
13:30So what do I have to do recursively?
13:32I have to remove its diameter for each node.
13:35So remove its diameter first, assume that there is a function called diameter
13:41which will return me left diameter for my root.left
13:45and that function will return me right diameter for my root.right.
13:53Now remove left height and right height.
13:56What have I already done before?
13:58I have already solved my height function.
14:00What happens with height function?
14:01We have to calculate some tree or sub-tree height.
14:05We have already read it and written it.
14:08So here I will pass the root of the left and left sub-tree height.
14:11And I will pass the root of the right.
14:13So what will my right sub-tree height?
14:16Now, as you have any other node, these are 4 values.
14:19These are the most important.
14:21If I have any other node, if I have these 4 values,
14:24then I can calculate its diameter in any other node.
14:27How do I calculate it?
14:29First of all, remove self-tree diameter.
14:31Self-tree diameter will be left height plus right height plus 1 to equal.
14:36One choice is, I have this.
14:38The other choice is left diameter.
14:40The other choice is right diameter.
14:41What do I have to do?
14:42I have to remove maximum of my left diameter, my right diameter and my self diameter.
14:50Whatever maximum value will be returned as our final diameter.
14:55And the base case, obviously, we have seen in dry run,
14:58the base case happens when I have null node.
15:00The diameter of the null node is 0.
15:02The height of the null node is 0.
15:03The height is 0.
15:29The height of the null node is 0.
15:46I have a maximum diameter.
15:48What do I have to calculate the maximum diameter?
15:49So, I will name it public static int diameter, its return type int is because at every level
15:57we will calculate our diameter and return it as my return statement.
16:03It will be single parameter which will be my root node.
16:07First, let's write the base case.
16:09If my root value is null to equal, then my diameter value will be equal to 0.
16:13I will calculate my left diameter and my left height, my right diameter and my right height.
16:22So, what will I do?
16:24My left diameter is equal to diameter to call root for left.
16:31Moreover, my left height is equal to height to call root for left.
16:39Then my right diameter is equal to diameter to call root for right and my right height is equal to
16:52height to call root for right.
16:55So, if I have 4 values, I will calculate my self diameter.
17:01That will be equal to left height plus right height plus 1.
17:09Now, whatever the maximum value will be,
17:12or math dot max of, I will pass self diameter,
17:16and math dot max of, left diameter and right diameter.
17:24Whatever the maximum will be, I will do my function with my return.
17:28I will save this time.
17:29For this time, if we are writing the code and the line,
17:33then what will happen?
17:34First of all, the max function will be left and right.
17:37The maximum diameter will be left.
17:38Then, the maximum diameter will be left.
17:41We will compare it with self diameter.
17:43Then, the maximum value will be left.
17:47I will return the maximum value to my left height.
17:56I will change the method at that time of root for right and right.
18:16So, I will save my function in this parameter.
18:18This is my function function.
18:19This is my function function.
18:19This is my function function function.
18:19node पर जा रही हूं तो उस node पर जाने में मुझे जो time लग रहा है वो तो linear
18:22time होगा यानी end time पर हर node पर जाते जाते मैं बार बार बार अपने
18:282 nodes की height भी calculate कर रही हूं जब एक बार height calculate होती है तो उसमें end time
18:33लगता है अगर मैं n nodes पर जाकर height को calculate
18:37कर रही हूं तो time complexity बन जाएगी n multiplied by n इसको मैं ऐसे visualize कर सकती हूं कि
18:43अगर
18:45मुझे अपनी इस node पर जाकर एक तो diameter calculate करनी है पर इस node पर जाकर अगर मैं height
18:50calculate
18:51करूं तो height को calculate करने में individually big of n मेरे पास time लगता है फिर मैंने इस node
18:57पर भी big of n time लगाया
18:58इस node पर भी big of n time लगाया इस node पर भी big of n time लगाया height को
19:03calculate करने में तो इस तरीके से n nodes में
19:06अगर मैं big of n time लगा रही हूं तो मेरी time complexity बन जाएगी n square के equal that
19:11is why यह जो process होता है
19:12diameter को calculate करने का जिसमें मुझे अपनी diameter और अपनी height को अलग-अलग calculate करके निकालना
19:18पड़ता है अलग-अलग function से यह थोड़ा सा मेरे पास tedious process होता है
19:22साथ के साथ यह inefficient है क्योंकि इसमें time complexity बहुत जादा लगती है
19:26इसलिए हम एक second approach को discuss करेंगे and इस second approach को हमें सबसे जादा maximum times यानि हमें
19:34जब भी use करना है
19:35इस second approach को हम use कर रहे होंगे जिसको हम करेंगे अपने linear time में
19:39अब यहां पे काफी सारी students का सवाल हो सकता है तो दीदी हमने first approach को क्यों use किया
19:44first approach इसलिए important है क्योंकि वो understanding build करती है
19:47second approach में तो हम क्या करेंगे first approach को थोड़ा सा और clean बना देंगे
19:51उसकी और time complexity को कम कर देंगे
19:53बस first approach से हमें actually समझ आता है कि diameter recursively calculate होती कैसे है