• 2 days ago
Transcript
00:00Now we are going to talk about how to search in a binary search tree
00:03We have understood that in a normal binary tree
00:07Big of n time complexity
00:09And in a BST
00:11Big of h time complexity
00:14We will focus on this
00:15And we will understand how to actually search
00:18First of all, we have our key which is equal to 1
00:22We have to search for 1
00:23And we will try to make some rules
00:24We will try to make rules when we search
00:26Although we have already read the rules
00:28So we have got a lot of hints
00:31First of all, we have to search for 1
00:33So we will compare the root
00:34With our key
00:37In this case, our root is greater than key
00:42So we know that if the root is greater than key
00:44Then the key is in the left subtree
00:47So here
00:48Our condition comes here
00:49Go to left subtree
00:55So we will go to 5
00:57We will compare 5
00:59We will compare 5
00:59With the key is equal to 1
01:00So we will come to this condition
01:02Condition 1
01:03So we will go to left subtree
01:05We will come to 3
01:05We will compare 3
01:07We will compare 3
01:07So 3 is greater than 1
01:09In the left subtree
01:10Here we have finally
01:12So what happened?
01:13Our key
01:14We have matched the root
01:15So we will return true
01:17We have returned true
01:19So once we get a key
01:21If we got a key
01:21Then we have to return true
01:24We have to return true
01:26So we will return true
01:28We will return true
01:28Here we will return true
01:29Here here
01:30And finally
01:31Our root
01:32We will return true
01:32And then
01:33We will return true
01:34Another case
01:34In the other case
01:36We will try to find another condition
01:37To find another condition
01:49which is greater than 8, this case is called root is less than key.
01:55When root is less than key, where do we go from this case?
01:58In this case, we will go to our right subtree.
02:05We are coming from 8 to 10.
02:07We will compare 10 again to 11, we will go right again.
02:10If we are coming from right, then we will go to 11.
02:12We will compare 11 again, so this time we will get our key.
02:15As we get the key, we will return to true.
02:19Our parent will return to true.
02:21Our grandparent will return true.
02:23In another case, there are three cases.
02:26Wherever we get the root is equal to key,
02:28what do we do in this case?
02:31Return true.
02:34Now we can also return to null node.
02:37If we search for key, we will search for key is equal to 7.
02:42What will happen in this case?
02:42What will happen in this case?
02:43In this case, there will not exist.
02:45But how will the process remain?
02:47We will compare with 8.
02:498 is greater than 7.
02:51In this case, we will go to the left subtree.
02:53In this case, we will reach 5.
02:555 is less than 7.
02:57In this case, we will reach right subtree.
02:596 is also less than 7.
03:02In this case, we will go to the right subtree.
03:04In this case, we will encounter null.
03:07We will return false value.
03:08As we are null here, we will return false value.
03:13Here we will return false value.
03:14There is another case here.
03:16If our root is null at any point,
03:20what will we do in this case?
03:23Return false.
03:26Here we have false value.
03:28Here we have false value.
03:30Here we will return false value.
03:31Here we will return false value.
03:33Here we will return false value.
03:34Here we will return false value.
03:35Our key is equal to 7.
03:36We will not exist in this case.
03:39So, what will be simple?
03:40There will be 4 cases that we have analyzed.
03:42We will write these 4 cases in recursive fashion.
03:45We will search for our key.
03:47Now, if we talk about time complexity,
03:49then we have already analyzed why the big of H will be.
03:53Because in the worst case,
03:54we have to travel to the root of the leaf.
03:58So, the root of the leaf is equal to the height of the tree.
04:05And this height, in the worst case,
04:06can be bigger of N.
04:07In an Xq tree.
04:09And when it is balanced tree,
04:11it can be logged in.
04:12So, there is a very varied range.
04:14Depending upon how many nodes you have,
04:16in which way our BST has formed.
04:18Now, let's write in the binary search tree,
04:20the code of search.
04:22We will create a function.
04:24Public, static.
04:25We will return the function type of boolean.
04:28We will give it the name search.
04:30In the search,
04:30we have our root node.
04:32And above it, a value or key.
04:36For which we want to search.
04:38Now, we will mention our cases.
04:41At any point,
04:42if our root is null,
04:44that means we will not exist there.
04:46So, we will return null.
04:49Sorry.
04:50We will return false.
04:53Another case will come.
04:55In which case,
04:56our root is equal to our key.
04:58If there is a case,
05:00the root data is equal to equal to key.
05:03So, we will return true.
05:08The third case will come.
05:09In which case,
05:10we will search in the left tree.
05:11So, if the root data is greater than key.
05:16In that case,
05:17we will search in the left root.
05:20And there, we will do the key.
05:21Pass.
05:22So, the root of left,
05:23which we will return,
05:24which we will return to the root of the left tree.
05:27Is it the key or not?
05:28If it is the true return,
05:30it will return true.
05:30It will not.
05:31What will the left tree return?
05:32False.
05:33Whatever we will return,
05:34we will accept it.
05:35And we will send it to our parents.
05:37As our answer.
05:38So, we will write directly from the left tree.
05:40If it is the right tree or not,
05:43depending on true and false,
05:45we will return.
05:46The fourth case comes.
05:48When we search in the right tree,
05:51we will search in the right tree.
05:55If it is the right tree,
05:56if it is the right tree,
05:57If it is not true,
05:58then it will return false.
05:59Whatever it is,
06:00we will return directly to our parents.
06:02If it is the right tree,
06:02So, these are the four conditions.
06:04We have discussed these four conditions.
06:05And these are the four conditions.
06:06We will write in search.
06:08We will save.
06:09Now, we will call the main function.
06:11We will call the search.
06:14Search root.
06:16First, we will search for one.
06:18If it is found,
06:19then we will write found.
06:22If it is found,
06:22then we will write not found.
06:27We will save and run.
06:34If it is found,
06:35then we will find one.
06:35We will find one.
06:35Now,
06:37if we try to find seven,
06:41then we will also find one.
06:42But,
06:43we have made one tree.
06:44One, two, three, four, five, seven.
06:46If I try to find six,
06:50then six will not exist in our BST.
06:53So, here will not found.
06:56And,
06:57we will remember the time complexity of this search.
07:00We will remember the big of H.
07:02...
07:02...