• 2 days ago
Transcript
00:00Next, let's talk about binary search tree in the binary search tree.
00:02The name of binary search tree is called search tree because it has a special property in search.
00:09Let's talk about what was happening in linear search and binary search.
00:12In the beginning of the search, we analyzed the end to analyze the array
00:17where we tried to find a key to find any other.
00:20So, we analyzed all the elements in this way.
00:24And if we talk about binary search,
00:26the logic of binary search was something like that we have an array.
00:30We stored different elements in this way.
00:34For example, 1, 2, 3, 4, 5 and 6.
00:38Now, every time we have to do a comparison, we have to do a mid value.
00:42Let's say, what is the key?
00:466 equals.
00:47So, first of all, we will do a comparison with 3.
00:50If the value of the key is bigger than our data,
00:53it is bigger than our data, so where do we need to search?
00:56We need to search on the right side.
00:57So, in this case, go right.
01:01And another case was that if our key's value is smaller than our data,
01:05then what did we do in this case?
01:07Go left.
01:08So, we had to keep these two conditions.
01:10So, if we have to search on 6, then what do we need to do?
01:13We will go right.
01:14Then we will go right to mid to mid to this array.
01:17So, it will be 5.
01:18We will compare 5 to 5 again.
01:20So, we will go right again.
01:22Go right and go right again, we will save such an array for our search.
01:26Now, the key and the data will be the same, which is equal to 6.
01:30And finally, we will return true.
01:32So, our binary search works in this way,
01:34which gives us a time complexity of the login complexity in the search,
01:38while the linear gave us a time complexity of the linear.
01:41So, we have to do something in the binary search tree.
01:44In the binary search tree, we have a logic in the binary search tree.
01:47So, if we think that a key is given, which is supposed to be 1,
01:51then what do we do?
01:52First of all, we will compare it with our roots.
01:54After we compare it with roots, we will know what the key is equal to our roots.
01:59Do we need to search in the left tree or in the right tree?
02:09So, when we compare it with roots, we will know what the key is equal to our roots.
02:21So, in this case, our data value is greater than key.
02:25We have to go to the left tree.
02:26So, we will search in the left tree.
02:28Now, we will search in the left tree, we have to go to the left tree.
02:30We will compare it with two.
02:32So, our data value is greater than key.
02:34We go to the left tree.
02:35We go to the left tree and we come to one.
02:37Now, we will compare it with one tree.
02:39So, our condition will be very satisfied.
02:42And here, we will get our node.
02:44We will search our key.
02:46So, in this way, in a worst case scenario,
02:48we can do what we can do with roots.
02:51We can go to the left tree.
02:54So, when we travel to the left tree,
02:58how can we go to the left tree?
03:01How can we go to the left tree?
03:01This is the maximum path of big of h is equal to where h is equal to our height of
03:06the tree.
03:09So, when we search the time complexity of any key,
03:13a normal binary tree in a normal tree
03:15was big of n.
03:18When we go to the left tree,
03:18when we take a balanced tree in our BST,
03:19this is the time complexity decrease in our BST.
03:22The same complexity is decreased in the right tree.
03:23When we take a balanced tree in our BST,
03:28in our BST,
03:29this is the height of log n.
03:31It is equal to log n.
03:33It is substantially lower as compared to n.
03:36So, that means,
03:36it becomes optimized when we search,
03:39but in worst case scenario,
03:41when we search for the right type of n.
03:44The worst case scenario is when we have a skewed tree, that means our tree will show something like this,
03:51in which our nodes will only be one side.
03:58So if we visualize this tree in which one, two, three, four,
04:02we call this tree as a skewed tree.
04:06The skewed tree is the one side.
04:09This is our left side,
04:10it could also be one side,
04:11it could also be one side,
04:12and it could also be the right side.
04:15So if we have to search 4,
04:17we have to travel all the nodes.
04:20In the worst case,
04:21which is big of h,
04:22is big of n equals.
04:24So if someone asked in the interview,
04:26what is the time complexity of binary search tree?
04:29We will say that big of height is equal.
04:33We will not call it directly,
04:34we will call it directly,
04:36we will call it big of n.
04:37We will call it height.
04:39In the average cases,
04:40yes, it can be log n.
04:41In the worst cases,
04:42yes, it can be bigger of n.
04:44But this is the correct term,
04:45which we have to use.
04:46But overall,
04:47we have to understand that binary search trees are more efficient,
04:51as compared to binary trees,
04:52when we have to search.
04:54So binary search trees make our search more efficient.
04:57Next, let's talk about a strategy,
05:00which we will use in every question of BST.
05:01We will use 98%,
05:0390% of questions.
05:05And this is the strategy that we have used in binary trees.
05:09It is simple,
05:10that we usually solve the question of binary trees.
05:12We will solve the question of recursion.
05:14So mostly,
05:15we will use recursion.
05:17Besides,
05:18we will divide our problems in sub-problems.
05:20We will divide in binary trees.
05:21Like,
05:22if we have a binary tree,
05:23let's think that tree is a bit like this,
05:25which is a lot of our nodes.
05:28So there is a high probability,
05:30that the things that we have to find out for this root,
05:33first,
05:34we will find out for this node.
05:35If we have to find out for this node,
05:37first, we will find out for children.
05:38So,
05:39we will find out for this sub-tree.
05:40If we have to find out for this sub-tree,
05:42first, we will find out for this.
05:43So,
05:43first,
05:44we will divide in small problems.
05:46We will find out for these sub-trees.
05:48Then,
05:48finally,
05:49we will find out for our root.
05:50So,
05:51our left sub-tree
05:52and right sub-tree
05:53will give value.
05:54So,
05:55left sub-tree
05:56plus
05:56right sub-tree
05:57plus
05:58some value x
05:59left sub-tree
06:00right sub-tree
06:01maximum
06:01or minimum
06:02in this way,
06:04we will solve our questions.
06:07And the third,
06:08we will put recursive calls
06:09on our sub-trees.
06:11So,
06:12how do we solve these sub-problems?
06:14First,
06:15we will solve our sub-tree
06:15first,
06:16from left sub-tree
06:17and right sub-tree
06:18then,
06:18recursively,
06:19when we give them value
06:20then,
06:20we will get them.
06:21And if we get them,
06:22then,
06:22we will return them
06:23to our parent
06:24with our optimized value.
06:27So,
06:27the logical mindset
06:30is to make us
06:31that we will mostly use recursion
06:33and divide the sub-problems
06:34and then,
06:35we will call the sub-trees
06:36and then,
06:37we will solve the question for the root
06:39of the sub-tree.
06:39So,
06:39as a result
06:39and then,
06:39we will save the sub-tree
06:39we will save the sub-tree