 Hello everyone, my name is Nikhil Gajjam working as assistant professor in department of computer science and engineering from Valchan Institute of Technology, Solapur. In today's video, we will see construction of binary search tree using pre-order, in-order and post-order traversal. So these are the learning outcomes. At the end of this session, student will be able to construct a binary search tree using pre-order and in-order traversal. Student will be able to construct a binary search tree using post-order and in-order traversal and student will be able to acquaint with binary search tree time complexity. Now before going to actually construction of binary search tree, we will see what is binary search tree, just revise the thing. In binary search tree, all we know, each node on the left subtree is having value less than or equal to its parent and each node on the right subtree is having value greater than its parent. So this is an example of binary search tree. First node 50 we call it as root. All the nodes present on left subtree of 50 are having value less than or equal to its parent and all the nodes present on right subtree are having value greater than its parent. So these are binary search tree traversal techniques. We all know there are three binary search tree traversal techniques. We call it as pre-order traversal, in-order traversal and post-order traversal. In pre-order traversal, we are visiting root or node first, then we traverse left subtree in pre-order traversal and then we traverse right subtree in pre-order traversal. In in-order traversal, first we traverse left subtree, then we visit node or root and then we traverse right subtree in in-order traversal. In post-order traversal, first we traverse left subtree in post-order, then we traverse right subtree in post-order and at last we are visiting root or node. As an example, suppose this is a binary search tree and we want to find out what is pre-order, in-order and post-order traversal. So pre-order traversal is 50, 20, 10, then 80 and then 60. In-order traversal is 10, 20, 50, 60 and 80 and post-order traversal is 10, 20, 60, 80 and 50. We know that root is visited at last in post-order traversal and root is visited first in pre-order traversal. Now we will see how to construct a binary search tree with given pre-order and in-order traversal. Suppose we have given pre-order and in-order traversal. So while construction, we are taking one-by-one node from left to right from pre-order traversal. So one-by-one node, first node is A. So first node in pre-order traversal as we know is always a root. So A will become a root. So next node is B. So B is present on left hand side of A. So B should go in left subtree of A. Next node is D. D is present on left hand side of A and left hand side of B. So D should present on left subtree of A and left subtree of B. Next node is H. Now H is present on left hand side of A and right hand side of D. So H should present on left hand side of A and right subtree of D. Next node is E. E is present on left hand side of A and right hand side of B. So E should present on left hand side of A and right hand side of B. Next node is C. Now C, we search C in in-order traversal. Now C is present on right hand side of A. So C should be a right child of A. Next one is F. F is present on right hand side of A and left hand side of C. So F should be present on right subtree of A and left subtree of C. Next node is I. I is present on right hand side of A and left hand side of F. So I should be present on right subtree of A and left subtree of F. Next node is G. G is present on right hand side of A and right hand side of C. So G should be present on right hand side of A and right subtree of C. Next node is J. Now J is present on right hand side of A and left hand side of G. So J is present on right subtree of A and left subtree of G. Last node is K. K is present on right hand side of A and right hand side of G. So K is rightmost node in this binary search tree. So in this way you can construct a binary search tree from pre-order and in-order traversal. We are taking one by one node from pre-order traversal and then we are searching that node where that that is placed in in-order traversal. So using pre-order and in-order traversal you can find out or you can construct a given binary search tree. So think and write question is construct a binary search tree using this in-order traversal and pre-order traversal. You can pause the video and you can answer. Okay, we will see the answer. So you are taking one by one node from pre-order traversal. So first node is A. So A will become a root. Next node is B. Now you are searching where actually B is present in in-order traversal. So B is present on left hand side of A. So B is on left subtree of A. Next node is D. Again you are checking. D is present on left hand side of A and left hand side of B. So D is present on left subtree of A and left subtree of B. Next node is E. Now E is present on left hand side of A and right hand side of B. So E E is present on left subtree of A and right subtree of B. Last node is C. Now C is present on right hand side of A. So C is present on right subtree of A. So in this way you can construct a binary search tree with given in order and pre-order traversal. Now similarly you can construct a binary search tree using post order and in order traversal. Now first we will see what is the difference between constructing a binary search tree with in order and pre-order traversal and post-order and in order traversal. Now in pre-order you are taking one by one node from pre-order traversal from left hand side to right hand side. Now when you are constructing a binary search tree from post-order and in order traversal you are taking one by one node from right hand side to left hand side. Because last node present in post-order traversal is always a root. So A is root in this case. So A will become a root. Next node is C. Now you are checking where C is present on in order traversal with comparison with A. So C is present on right hand side of A. So C is right subtree of A. Next node is E. E is present on right hand side of A and left hand side of C. So E is present on right hand side of A and left subtree of C. Next node is B. Now B is present on left hand side of A. So B is present on left subtree of A. Last node is D. D is present on left hand side of A and right hand side of B. So D is present on left subtree of A and right subtree of B. So in this way you can construct a binary search tree with post-order and in order traversal. Now we will see what is binary search tree time complexity. Now binary search tree with n nodes required big of log n comparisons to find out a particular node. Now suppose we have given this binary search tree. So maximum number of comparisons required to find out whether a particular node is present in this binary search tree or not is 3. So you are starting comparing from root. So A is your first comparison next level either B or C and then next level G, D, E and Z. So this is level 3. So maximum 3 comparisons are required to find out whether a particular node is present in this binary search tree. So big of log n how it comes is with first comparisons you are checking whether that node is present on left subtree or right subtree. With value you are checking if it is less than a root you are checking next searching in left subtree. If searching value is greater than root you are doing a next searching operation on the right subtree. So with one comparisons you are selecting half part and you are discarding half part. So in this way the complexity time complexity for searching a particular node in binary search tree is given as big of log n. Again this complexity is average time complexity. Now worst case time complexity of binary search tree is given as big of n. Now worst case if binary search tree is like this we call it as skew tree where all the nodes are present on one side. So in this case the maximum number of comparisons are required is 3 and number of nodes present in this binary search tree are 3. So time complexity of this binary search tree in worst case given as big of n. So these are the references I have used for creation of this video. Thank you.