 Hello everyone, my name is Nikhil Gadjam working as assistant professor in computer science and engineering department in Valchand Institute of Technology, Solapur. In today's session we will see binary search tree introduction and its traversal techniques. Now learning outcomes of this video is at the end of this session student will be able to construct the binary search tree with given keys and traverse the given binary search tree using various binary search tree traversal techniques. Now first we will see the introduction of binary search tree. A binary search tree is actually a binary tree. We know binary tree in which each node can contain maximum two nodes or in other words we can say each node can contains either zero or one or two nodes. So in binary search tree each node on the left hand side of tree is having value less than or equal to its parent and each node on the right hand side of a tree is having value greater than its parent. So this is an important property of binary search tree or this is a property by which binary search tree differs from binary tree. So example of binary search tree is given here. So 50 is the first node we call it as root. So all the nodes which are appearing on the left hand side of 50 are having values less than 50 and all the nodes which are appearing on the right hand side of 50 are having values greater than 50 and same property will appear for each node. Example 25. Suppose we have taken 25 as a node. So 10 is less than 25. So 10 is appearing on the left hand side of 25. 40. 40 is having value greater than 25. So 40 is present on the right hand side or right subtree of 25. Suppose we have taken example of 70. So 70 is a node, 60 is the node, 60 is having value less than 70. So 60 is present on the left subtree of 70. So this is an important property of binary search tree in which all the nodes which are having value less than its roots root all these values or all these nodes are present on the left subtree and all the values which are greater than its root are present on the right subtree of root. Now we will move forward. Now we will see how to construct a binary search tree from the given keys. Suppose we have given with these keys 24, 50, 10, 18, 55, 99, 25 and 2. So first key is 24. So 24 is the first node. First node in the tree we call it as root. So 24 will become a root here. Next node is 50. So 50 is having value greater than 24. So 50 should be present on the right subtree of 24. Next node is 10. So 10 is having value less than 24. So 10 is present on the left hand side or left subtree of 24. Next node is 18. Now we should traverse from root. So 18 is having value less than 24. So it should be present on the left hand side. So already on the left hand side 10 is present. Again we compare 18 with 10. 18 is having value greater than 10. So 18 should be present on the right subtree of 10. So this is the correct position of 18. So next node is 55. Now we should start from root. 55 is greater than 24. So 55 should be present on the right subtree. Again we compare 55 with 50. 55 is greater than 50. So 55 should be present on the right subtree of 50. Next node is 99. Again we compare with 24 first. 99 is greater than 24. So 99 should be present on the right subtree of 24. So 99 is compared now with 50. Again 99 is greater than 50. So 99 should be present on the right subtree of 50. Again we compare 99 with 55. So 99 is greater than 55. So 99 should be present on the right subtree of 55. So this is the correct position of 99. Now next node is 25. Again we start from root. 25 is greater than 24. So 25 should be present on the right subtree of 24. We compare 25 with 50. Now 25 is less than 50. So 25 should be present on the left hand side of 50 or left subtree of 50. So this is the correct position of 25 now. Next node is 2. We start with 24 first. 24 is the root. Now 2 is less than 24. So 2 should be present on the left hand side of 24. Again we compare 2 with 10. 2 is less than 10. So 2 is present on the left subtree of 10. So this is the correct position of 2. So this is the binary search tree we have constructed from given keys. Now we will move forward. Now we will see how to traverse the given binary search tree. Traversal means to visit all the nodes present in the given binary search tree. So there are three binary search tree traversal techniques. First one is pre-order. Second one is in-order. And third one is post-order. We will see one by one now. First one is pre-order. Algorithm for pre-order traversal technique is given as. There are three steps. First we are visiting root. Then traverse the left subtree in pre-order traversal. And next traverse the right subtree in pre-order traversal. So these are main three steps of pre-order traversal. We will see the example now. Suppose given this is the binary search tree given. Now we have to find out what is the pre-order traversal of this binary search tree. As we know there are three steps in pre-order. First visit root. Then traverse left subtree in pre-order. And then traverse right subtree in pre-order. So first is 50. As we have visited root is first 50. So 50-50. Then we are traversing all the left subtree in pre-order traversal technique. So this is the left subtree. Again 20 is the root. First 20 will come. And then traverse left subtree. Left subtree contains 10. So this is 10. Then right subtree. Right subtree contains 30. So this is 30. Now this is the completion of traversal of left subtree. Now we have to traverse right subtree in pre-order. In right subtree 80 will become a root. So this is 80. Then traverse left subtree. Left subtree contains 60. This is a 60. Then traverse right subtree. Now right subtree contains 90. So this is a 90. So this is the pre-order traversal of this binary search tree. Now next traversal technique is in-order traversal. In in-order traversal. There are again three steps. First one traverse left subtree in in-order traversal. Then visit root. And then traverse right subtree in in-order traversal. We will see the example. So suppose we have to find out in-order traversal of this binary search tree. In in-order traversal. First we are traversing left subtree. Then visit root. Then traverse right subtree. So we start from root. First we will traverse left subtree in in-order traversal. Left subtree contains now 20 will become root. Again 10 is left subtree. Then 20 will become root and 30 is right subtree. So first 10 will appear as an output. So this is 10. Then visit root. 20 is root. Now then visit right subtree. 30 is the next answer. So this is the completion of in-order traversal of left subtree only. Then visit root. Now 50 will appear as an output. Then traverse right subtree in in-order traversal. Now 80 will become root. Traverse left subtree 60. 60 as an output. Then root. Then 80 as an output. Then right subtree. Right subtree contains 90. So 90 as an output. So this is an in-order traversal of this binary search tree. Remember that when you find out in-order traversal of binary search tree, output will be an ascending order. So this is an important property. When you find out in-order traversal of any binary search tree, we will find out output as an ascending order with natural ordering. Now third one is post-order traversal. Again three steps. First we will traverse left subtree in post-order. Then we will traverse right subtree in post-order traversal. And then visit root. Now we are visiting the root in the last, in the post-order traversal. Now we will see the example. Now again we are finding the post-order traversal of the same binary search tree. In post-order we are traversing left subtree first, then right subtree. And then at last we are visiting the root. So 50 is a root that will appear as the last post-order traversal. First we will find out now. First we will find out post-order traversal of left subtree. Now first 10 will appear as an output. This is a left subtree. Then right subtree 30. 30 is an output. Then we are visiting the root. Now 20 will be root. So this is a post-order traversal of only left subtree. Again we will find out post-order traversal of right subtree. So first 60 will appear as an output. 60, then right subtree 90. 90 will appear as an output. And then root 80 will be appear as an output. So this is the completion of traversal of post-order of right subtree. And at last then we are visiting the root. So 50 is the root. So remember that in post-order traversal, the root will appear as the last element in the traversal. Now this is a summary of pre-order, in-order and post-order. In pre-order it is node, left, right. In in-order first left, then node, then right. In post-order first we will traverse left subtree, then right subtree and then last node. Now you can pause the video and you can find out the answer. Find out pre-order, in-order and post-order traversal for following binary subtree. We will see the answer. You can check the answer. Pre-order traversal of these binary subtree is 50. In pre-order we are visiting root first, then left subtree and then right subtree. So pre-order traversal answer is 50, 20, 10, 80 and 60. In-order traversal is, it is an ascending order, 10, 20, 50, 60 and 80. And post-order traversal is 10, 20, 60, 80 and 50. So 50 is a root, so that will appear in the last. Now again another question, find out the answer for, I want to find out the visiting sequence for the node in the tree to get output as descending order. As we know in in-order traversal, we will get output as ascending order. So you can make the changes in in-order traversal only to get the output as descending order. You can pause the video and you can find out the answer. This is one of the solution to get the output as descending order. First we will traverse the right subtree in in-order traversal. Then we are visiting root and then we are traversing left subtree in descending in-order traversal. So these are the changes we can make in in-order traversal to get output as descending order. These are the references which I have used for creation of this video. Thank you.