- 2 days ago
Transcript
00:00Next question which we are going to solve, its name is height of a tree. What is height of a
00:05tree?
00:06Height is based on the root of the leaf to the beach, the maximum distance.
00:14Now, in many places, the distance is in terms of nodes and the distance is in terms of edges.
00:22What is the edge? It is the connecting line of the beach, which we call edge.
00:27And this is our tree node. Now, we can count the height in the form of a tree and edge
00:33in the form of a tree.
00:34If I want to calculate the height in the terms of nodes, then in this tree, the maximum distance in
00:39the root of my leaf in terms of nodes,
00:43it will become three nodes. One node, two nodes, three nodes. This is basically equal to how many levels I
00:51have in my tree.
00:52But if I want to calculate edges in the form of a tree, then my height will be equal to
00:56two nodes.
00:57In the form of an edge, the other one will be equal to two edges.
01:02So, I want to calculate the root of my tree to the root of the leaf, maximum possible.
01:06That's why, in the form of an edge, the height of the tree will be equal to two nodes.
01:11We will try to calculate the height of nodes in the form of nodes.
01:15In the form of edges, if you want to do it, then the final nodes will be equal to one
01:20nodes.
01:21We will be equal to one nodes. Every place will be equal to one nodes.
01:23So, the height will be equal to one nodes in the form of edges.
01:26We will be equal to one nodes in the form of edges.
01:26First, let's talk about these two trees' height.
01:29The first tree's height we have already analyzed.
01:32Here, I have the first node, this node has the second node, and this node has the level of my
01:36leaf nodes.
01:38So, height of this tree is equal to three. If we talk about the second tree, then this is my
01:45root.
01:47Now, here, the leaf node is also here. The leaf node is also 4. The leaf node is also 7.
01:52But, we will not calculate the height from this tree and this tree from this tree.
01:55We will go to the deepest leaf node to our height to calculate our height.
01:59So, height in this case will be equal to 1, 2, 3, 4, 5.
02:04So, height of this tree is equal to 5. Why?
02:08Because, root and leaf are possible maximum distance is equal to 5 in terms of our nodes.
02:15Now, mostly the questions of binary trees, binary search trees, in fact, all the trees.
02:20We use recursion to solve them. Why is recursion?
02:33So, values of these rules to make it stand in form in the form weakמׄes.
02:36So, iferry makes 4 different fields entry to me zar, return myself to 5 to my height, then I will
02:43say that,
02:43I can calculate the height of 2. What would be the height of 4 and the height of 5?
02:50With the maximum height of 2, I will add the depth to that.
02:55I will add the height of 4 and 5.
03:00With the maximum height of 4 and 5, the maximum height of 4 will be the maximum height.
03:06The maximum questions in binary search trees.
03:11If we need to find the answer for child nodes or child sub-trees,
03:16then we can recursively answer to our parents.
03:20Therefore, if we have a question of binary trees or BST,
03:23then we should first hit that we can apply recursion.
03:27How do we apply recursion?
03:29First of all, calculate for children nodes.
03:31Then assume that the child node will return the answer.
03:35This will fade the recursion.
03:37We need to calculate the answer for the parent node.
03:40Now, if I talk about height again,
03:43then assume that I have a recursive function
03:45which gives me already the child node
03:47which gives me the height to calculate.
03:49So, how will the recursive function look?
03:51Let's assume that my left height will be equal to
04:02which will be equal to
04:05I calculated the height on my root of the right.
04:09What can I say? If I have left child's height or right child's height.
04:13The 3 of the left child is a null node.
04:17A null node is a null node in this height.
04:20Because there is no height of this left child.
04:22So it will return 0.
04:24And if I talk about 6,
04:26I mean if this is my tree,
04:28which is just a 6 node,
04:30then it's height 1.
04:31So every leaf height is 1,
04:33and every node is 0.
04:35So what can I return 6 to 3?
04:37I return 6 to 3.
04:39So what can I calculate 3 now?
04:41The 3 of the height will be,
04:43if this sub-tree,
04:45this small tree,
04:47this small tree,
04:47this 3 will be its root node.
04:49So how will 3 calculate its height?
04:52Left sub-tree,
04:53and right sub-tree,
04:57the maximum height will be,
04:59the maximum height will be,
04:59in that way,
05:00in that way,
05:00left sub-tree and right sub-tree
05:02will be maximum height.
05:04Here,
05:05left sub-tree height is 0,
05:06right sub-tree height is 1.
05:07So what are the maximum?
05:08What are the maximum height?
05:09What are the maximum height?
05:09They are 1.
05:10So 3 will be 1 plus 1.
05:14we will add one because the parent is one level above, so in height the maximum of left and right
05:20we will add one, so I can remove my parent's height, that will be equal to 2, so what
05:28I can say, my node's height will be, or my root's height will be equal to maximum of my left
05:36height, right height plus 1, so first of all, I have left maximum and added one,
05:42and here the base case can be, if my root is null to equal, or if I have left or
05:50left,
05:52I will return to that case, what will be height for a null node, null node will be 0,
05:57the case of leaf node will be automatically covered, once upon this tree we have the logic
06:02to dry run, first of all, what will happen, first of all, we will come to one, what will
06:09calculate your height to our left tree, then we will turn to one, then we will go to
06:14two, two will become my root, the two will become my root of two, before the left
06:18subtree will be half, from two we are coming to four, now 4 will become the root of the
06:23root of four, before the left subtree will be half and calculate that, when we have
06:274 calcate the height of 4, then go to three and there will be a null node here, here
06:34I will hit the base case here and return 0 as my answer, this null node will also return 0
06:44as my answer
06:45because the height of left subtree will be called for right subtree and then what will my 4 do?
06:52My 4 will calculate its height in this formula. I will remove maximum of 0 and 0. I will add
06:581
06:58So, my height will be equal to 1. So, what will I do? 2 will return its height as 1.
07:05Then, what will I do?
07:072 will remove its height of left subtree, which is equal to 1. Now, 2 will remove its height of
07:11right subtree
07:12because after left, we will remove its height of right subtree. So, we will go to 5.
07:165 will calculate its height of left subtree and return it to 0. And the height of right subtree will
07:24return it to 0.
07:25So, 5 will calculate its height as 1. And the height of 2 will return 1.
07:302 will have left subtree and right subtree. So, what will I do?
07:34It will remove its maximum and add 1. So, 1 and 1 maximum is equal to 1 and 1 plus
07:401 is equal to 2.
07:41So, the height of 2 will be equal to 2. What will I do? 1 will return its height.
07:47So, 2 will return its height to 2. Now, 1 will remove its height of left subtree.
07:52Which is equal to 2. Now, 1 will remove its height of right subtree.
07:56The parent basically sees that my deepest leaf is in my left subtree or in my right subtree.
08:03So, what will I do?
08:05Whenever it exists in my left subtree, we have to find out from there.
08:081 will remove its value to its value. 1 will remove its height of right subtree.
08:10So, first we will come to 3 and then we will go to 6.
08:146 will do its height of left subtree. It will receive its call and return 0.
08:18The right subtree will receive its call and null has returned 0.
08:216 will do its height of right subtree.
08:33left sub-tree call will be called to call 6 before doing 6, which will return 0, now
08:39left sub-tree came 0, right sub-tree came 1, so what is the height of 3? 1 plus 1,
08:44that
08:44is equal to 2, 3 has now 1 returned 2, so what will 1 do? 1 has left sub-tree
08:51height,
08:51right sub-tree height, so 1 will remove maximum height, maximum height will be 2, so 1
08:57height will be 2 plus 1 equals 3, so that's why my tree is complete, I can say height
09:04will be equal to 3, and what is the process? What is the recursive process, where we
09:10calculate the child node's height, then recursively we will calculate the parent node's height
09:15and for that, we will write some pseudo-code, and if you look at this, if you look at this
09:21and calculate, then I have linear time, so I have bigger of n time, why? Because
09:27if I have n nodes in tree, then what can I say, I have recursively only a node
09:32only after a, after a 4, it has returned to height, and after a 5, so recursively I have
09:41n nodes, so I have linear time, which is bigger of n, now we are going to write
09:45I am going to write a code for height. I have already created a classroom name class.
09:50I have already created my static class node, which we are defining the nodes of trees.
09:55With this, I have already calculated this tree.
10:00This is my full binary tree. I have calculated this tree class.
10:05I have directly created a root variable, a root object.
10:09I have created a tree, which I have created a value in which I have passed 1.
10:11After that, I have constructed the whole tree in the root.
10:15What I will do is write a height function for my tree's height.
10:20Public, Static.
10:22It will be return type int, because it will be calculated by the height.
10:26I will give it the name height.
10:28I have a single parameter, which is my node root.
10:33If my root is null to equal,
10:37in this case, I have no node in my tree.
10:39My tree is completely empty.
10:41In this case, my tree's height will be equal to 0.
10:46In other cases, what will happen?
10:48In other cases, recursively,
10:49I have to calculate every root above the root.
10:51First of all, I have to calculate my left tree's height.
10:53Then I have to calculate my right tree's height.
10:55Then I have to calculate my height.
10:56Then I have to calculate my height.
10:57The process of int left height is equal to height of root.left.
11:09And finally, how do I calculate my height?
11:12My height will be math.max of left height, right height, plus 1.
11:19This value, I can store this value,
11:22then I can return it,
11:23or then I can return it directly here,
11:26using this return statement.
11:28This will be my complete function to calculate the height of my binary tree.
11:32Let's do save and main function.
11:33I will call in the main function.
11:36Height of my root.
11:37For this tree's height,
11:39the answer should be 1, 2, and 3.
11:42In this three levels,
11:43and if you calculate the notes,
11:45then this tree's height is equal to 3.
11:47Let's do save and run.
11:53I have the answer to 3 to equal.
11:55If I completely remove this last layer,
11:57or completely remove this layer,
12:00then I will remove this layer
12:01and my height will be equal to 2.
12:04Let's save and run it.
12:06My answer is 2.
12:08So, in this way,
12:08we calculate the height of our tree.
12:11And the question is important,
12:12because many of the questions of the very good level
12:14are going to calculate the height of tree
12:17as a helper function.
12:20So, for this reason,
12:21our basic understanding,
12:22how to calculate the height of tree,
12:24how to traverse on it,
12:26all the basic things
12:27that we need to be clear very well.