 Hello everyone, my name is Nikhil Gajam, working as assistant professor in Walshan Institute of Technology, Zolapur. In today's session, we will see searching in binary search tree. So, this is a learning outcome of this video. At the end of this session, student will be able to write a code to search a particular node in binary search tree or to find whether a particular node is present in given binary search tree or not. Before going to searching directly, just revise the thing, binary search tree, what is binary search tree? In binary search tree, each node on the left hand side having value less than or equal to its parent and each node on the right hand side or right subtree having value greater than its parent. Example is this one, 50 is the root, so all the nodes present on left subtree are having value less than 50 or equal to 50 and all the nodes which are present on right subtree are having the value greater than 50. So, this is a basic property of binary search tree. Now, we will see how actually searching is done or performed in binary search tree. Suppose I want to search a 60 in this binary search tree, this is a binary search tree. Always remember that insertion and searching is done from root level to the leaf node. So insertion or searching is done from root to leaf node, not from leaf node to root. So this is a direction from root to leaf node. So searching is starts from root level, so first 60 is compared with 50, so 60 is not equal to 50, then we will see whether 60 is greater than or less than 50, so 60 is greater than 50. So we come to know that 60 should be present on the right subtree. As 60 is greater than 50, 60 will not be present on the left subtree, so 60 should be present on the right subtree. So again now next searching will be done with the next level, next level node is 70, again 60 is compared with 70, now 60 is less than 70. So 60 should be present on the left subtree of 70, as 60 is less than 70. If 60 or any element if it is greater than 70, then next searching will be done on the right subtree. Now in this case 60 is less than 70, now next iteration or next searching will be done on the left subtree of 70. Now next 60 is compared with 60, again this, now here 60 is equal to 60, now we found the 60, 60 is present in this binary search tree, now searching will be stopped here. So this is how actually searching will be done in binary search tree. Now we will see another example how searching will be done if node is not present in binary search tree. Now first I want to search 45, whether 45 is present in this binary search tree or not. First 45 is compared with 50, 45 is not equal to 50, so we have to continue our searching for next iteration now. Now next to find out whether 45 is present on the left subtree or right subtree, now 45 is less than 50, so 45 should be present on the left subtree, in this case we are discarding complete right subtree. Now next searching will be done with 25, now 45 is compared with 25, now as 45 is greater than 25, so next searching will be done with right subtree or in right subtree. Next again 45 is compared with 40, now 45 is not equal to 40, again we will check whether 45 is less than or greater than 40, so 45 is greater than 40, so next searching will be done on the right subtree of 40, now there is no right subtree of 40, so there is the end of binary search tree here, now we conclude that now 45 is not present in given binary search tree. So this is how actually searching is performed in binary search tree, remember that searching will start from root level to leaf node. Now we will see a code for searching a binary search tree, now this is a partial code for main function, assuming that all remaining necessary code will be there like this, now enter ok printf enter a key to be searched in the binary search tree, I am storing that node value in the variable key, now node equal to search, so search is my function where actually searching is done, I am passing root and key, so root I am passing because searching will start from root, so key is a element which I want to search in given binary search tree, now that function will return a node if it is actually present or not present, if a key or element is not present in the binary search tree then my search function will return a null, otherwise it will return a address where it is actually present. If node double equal to null means, if the search function returns null means in that case that node is not present in the binary search tree, if it is not equal to null means a node is present in the given binary search tree, now we will see how actually search function looks like, now search function will accept two parameters first one is root, second one is element which is to be searched and return type is address of that node, now this is recursive code, now this is exit condition of this recursive function if root reaches to null or element is found at particular node, in that case I am returning a root or address of that node, now I am checking if that element is greater than root key or less than root key, if key is greater than root key means next searching should be done on the right subtree, so that is why I am calling a search function again recursively with passing root arrow right, so right is a right address or right pointer, so in that case I am passing a right subtree for next searching, a key will be an element to be searched, else or otherwise if key is smaller than root key, in that case searching should be done on the left subtree, so for next recursive function I am calling search function with passing two arguments, first one is left root arrow left means I am passing left subtree along with the key which is to be searched, if element is found at root in that case I am returning root or address of that node, if element is not present in that case I am returning null, so this is a search function, now you can pause the video now and you can answer, write a code to find out smallest node present in the given binary search tree, we will see the answer now, now first of all find out where actually a smallest node is located in given binary search tree, suppose this is a binary search tree with 50 as a root, so smallest node present in this binary search tree is 6 and where it is located, it is located on the left most side of the binary search tree, so if you traverse from root to left most side, so left most node is nothing but your smallest node present in the binary search tree, similarly for maximum node or largest node present in the binary search tree, so in this case maximum node or largest node is 90, so how to traverse, go from root to right most node, so right most node is your largest node, now we will find out this, how to find out smallest node present in the binary search tree, suppose I have written a find smallest as a function name, I am passing root, again searching or any operation in the binary search tree will starts from root, so remember that for each function you are passing root as a initial argument, so temp is temporary variable I am using, first it is located or pointing to a root, I will traverse from root to left most node, so if temp double equal to null means, actually binary search tree is not present in that case I am returning from this function, otherwise while temp arrow left child not equal to null, now left most node and its left child is null, otherwise all remaining nodes left child having some pointers or it is pointing to some of the node, but six node is a left most node its left child is empty, so left most node always have left child as an empty position, so that's the condition I am using inside this while, while temp arrow left child is not equal to null, I am traversing temp from temp to left most side, so at last temp will reach or point to six, in that case I am printing smallest element in the binary search tree's %d temp arrow data, so in this case it will print six, now what is the complexity for searching a particular node in binary search tree, complexity of finding or searching a particular node in given binary search tree with n node is given as big of log n to the base 2, so why it is a base 2 because after each searching iteration you are discarding half part of the tree, suppose I want to find out 20, so you are comparing from root as 20 is less than 15, so 20 should be present on the left subtree, I conclude that it will not present on the right subtree because of the property of binary search tree, so at each iteration you are discarding half part of the binary search tree, that's why it is to the base 2, so complexity of searching a node in binary search tree is given as big of log n to the base 2, now these are the references I have used for creation of this video, thank you.