 Hello everyone, my name is Nikhil Gajam working as assistant professor in department of combative science and engineering in Valchand Institute of Technology, Solapur. In today's video we will see a program for converting a binary search tree into its mirror tree. This is a learning outcome. At the end of this session, students will be able to write a C program for converting given binary search tree into its mirror tree. Now we have already know what is binary search tree? In binary search tree, 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. Example, suppose 50 is the root, so all the nodes which are present on the left hand side of 50 are having value less than 50 and all the nodes which are present on right hand side, right subtree are having value greater than 50. So 25, 10 and 40, these are less than 50, so these are present on the left hand side of root 50. So this rule is followed at each and every node, so 10 is less than 25, so 10 is present on left hand side, 40 is greater than 25, so 40 is present on right hand side of 25. Like this, this is the property of binary search tree. Now this is how binary search tree and its mirror tree looks like, suppose this is my original binary search tree with 50 as root node, 25 is present on left subtree of 50, 70 is present on right subtree of 50, 60 is less than 70, so 60 is present on left subtree of 70, 40 is greater than 25, so 40 is present on right side of 25, 10 is less than 25, so 10 is left side of 25. So this is my original binary search tree. So after converting my original binary search tree into its mirror tree, so mirror tree will looks like this, so 50 is a root which will remain as it is. In original binary search tree 70 was present on right hand side of 50, it will become now a left side of 50 and 25 which was present on left child, now it is present on right child like this. So this is a mirror tree of original binary search tree. You can pause the video and you can answer, now how to check in your program whether binary search tree is converted correctly into its mirror tree. You can pause and you can answer, okay answer for this is after conversion of original binary search tree into its mirror tree, we can print in order traversal of binary search tree. If answer is in descending order, then we can say that the original binary search tree is converted correctly into its mirror tree. Example, suppose this is my original binary search tree, after printing in order traversal of original binary search tree, the output is 10, 25, 40, 50, 60 and 70. In in order traversal, first we are visiting left subtree, then you are printing the root or node, then you are printing right subtree. So when you print in order traversal of any binary search tree, the output is in ascending order. So output is 10, 25, 40, 50, 60 and 70. After conversion of original binary search tree into its mirror tree, the tree will be generated like this and in order traversal will be 70. First we are visiting left subtree, now left subtree is not present. So 70 will be printed, as we are visiting node, then you visit right subtree, so 60 will be printed, next one is 50 will be printed, next one is 40 will be printed, next 25 will be printed and lastly 10 will be printed. So output of in order traversal of mirror tree will be in descending order. If you get a descending order as an output, then you can confirm that the original binary search tree is converted correctly into its mirror tree. Now algorithm for conversion of binary search tree into its mirror tree is visit each and every node of binary search tree and at each node swap address of left subtree with address of right subtree. Example suppose this is my original binary search tree with 50 as a root, 25 is present on left hand side which is left child of 50, 70 is right child of 50, suppose address of node 25 is 2000, address of node 50 is 1000 and address of node 70 is 3000. So 2000 which is address of 25 it is present on left child of 50, 3000 which is present on right child of 50, left child and right child of 25 are null presenting leaf node and left child and right child of 70 are null representing leaf node. After converting this into its mirror 50 will be root, now left child will contain address of 25 and right child will contain address of 2000 which is 25, so 70 will become a left child and 25 will become a right child. So this is an algorithm you are visiting each and every node of binary search tree and then at each node you are swapping address of left child and right child or at each node. So left child will become right child and right child will become left child. This is an algorithm. Now we will see a program for conversion of binary search tree into its mirror tree. This is an online compiler I am using. This is a program for creation of binary search tree and in-order traversal. This is structure declaration which contains three fields left child then data and then right child. This is an insert function, normal insert function for insertion of new node in binary search tree. This is an in-order traversal. In-order traversal will print output in ascending order in case of binary search tree. Now this is my main function. This is menu driven program in that case 1 will be my insert function, case 2 will be my in-order traversal. Now case 3 I am going to write which will convert the original binary search tree into its mirror. I will execute first to check whether this program is working correctly. I am inserting three nodes suppose this is insert first one is 50 so 50 will become root again insert suppose 40 now 40 will become left child of 50 again insert now 60 will become right child of 50. Now I will display in-order traversal of this binary search tree. So output will be 40, 50 and 60 when you print in-order traversal of binary search tree it will print in ascending order. This program is correct now. I will write now mirror as a function which will convert binary search tree into its mirror tree. I will add one extra case this is case 3 for mirror I am passing root as an argument. Now I am using in-order function only this is my mirror function. Now in-order traversal you are visiting each and every node. So I am using in-order traversal function only now instead of print f this is mirror this is mirror this is struct node star temp this is a temporary variable I am using for a swapping addresses of left child and right child the temp equal to this r arrow l child r arrow l child equal to r arrow r child then r arrow r child equal to temp. So this is a code for converting a binary search tree into its mirror. Now these two are recursive function for visiting each and every node in binary search tree and these three lines are swapping the addresses of left child with right child. I will execute this now I will create one binary search tree 50 as a root then 40 as extra node and another 60 as another node first I will print in-order traversal of original binary search tree. The output will be 40, 50 and 60 then I will convert this original binary search tree into its mirror. So after conversion I will print again in-order traversal. So output will be 60, 50 and 40. So after conversion of original binary search tree into its mirror if you print in-order traversal it will print the output in descending order. So this is a confirmation that original binary search tree is converted correctly into its mirror tree. I will move again to our presentation. These are the references I have used for creation of this video. Thank you.