 Welcome to this course on data structures and algorithms. Today we will continue our discussion on binary trees. This time we are going to look at a computer program which implements a simple binary tree that we had seen earlier. The data structures that we would use would be a node first of all. Each node will have data left index and right index as we have already seen. For representing the binary tree, we are going to use a object of class vector. So, b t for example, will use to represent binary tree. It is of type structure node. The functions that we will need to implement binary tree, one is of course an insert function which will insert the data element by calling the appropriate functions which could be either make a node if tree is empty or insert at left of the parent or insert at right of the parent. So, this is exactly what we looked at when we saw the construction of a binary tree and this insert then is the main function which will help us construct the tree by inserting elements one at a time. Next, we would require a function called make node which will insert the data at the root and of course, a function for insertion at left and a function for insertion at right. Let us look at the program. This is the structure of the node. It has an integer data, an integer left index and an integer right index. The class binary tree itself is defined to have remember what we said we will use a vector class. So, it is a vector struct node bt. So, bt now becomes our binary tree. There are of course, public member functions in this binary tree which are make node with data as a parameter, insert left and insert right with an index and data as parameters and just insert if you want to insert the data in a particular node. We would also need traversal. So, we have to write a function which we call traverse function. Note that we have only a single function for any of the three types of traversals which we indicate by including a parameter called order. Depending upon a particular value, the traversal will be either in order or post order or preorder. So, this is the end of this class. Of course, please note that when we use the vector class, the standard template library gives us an extremely powerful set of functions which can operate on vector. We are just going to use one of those inbuilt functions. Look at this binary tree make node function. Well, it is very simple. We use pushback. Pushback b will actually create a node with b which will have data and minus 1 minus 1 to represent both left and right indexes because there is no child at the moment. Here is an insert left function. Say stuck node b which will have data and minus 1 minus 1 as the initial values. Now, we say left index of the BT index is to be assigned the value BT dot psi. What is the BT dot psi? It depends upon how many nodes are there in the tree. So, if you have the size as only 1, this will have a value 1. When we pushback b, this value b gets pushed back at that particular index value that we now wish to insert the new node in. Exactly similarly, if I have to insert at the right, I still have that node b as this. But now I set right index of the BT index as BT dot size and I simply say BT dot pushback. So, this node gets pushed as the right child of the parent. Next, let us consider the binary tree insert function. Now, there are two things. One, when the tree is empty, we will have to write some code and two, when tree is not empty and we are inserting a node, then we will have to write the code which will require us to find out whether it has to be inserted to the left or right or whether we have to traverse the tree to find a place for insertion, etc. Let us look at the program in some details. Now, let us look at the code we need to write if tree is not empty. If tree is not empty, then we come to the else part. Please note that if the size was 0, we would have created a node. If it is not 0, then we set index equal to 0 and walk through the tree, traverse through the tree as long as index is less than the current size. As we proceed traversing, we need to write code to insert data at the left of parent or data at the right of parent. In either case, when the insertion happens, the while loop would end and the else part will also end. Let us look at these two options and how they are implemented. First, the code to insert data at the left of the parent. Please note that this will happen when the data to be inserted is less than the data of the parent. So, if btindex.data is greater than or equal to new data, then it will have to be created on the left. Now, we will check is there place of the left or already there is a left index in this particular parent node. If it is minus 1, then there is a place. At that point, we invoke the function insert left with this data. If not, then we set index to the left index and again start going back doing exactly the same thing and that is because we need to traverse the tree till we find a proper place to insert the new data element. Insertion of a new data element to the right of parent is exactly written in the same fashion. If that is not less, then I got a value which is greater than the parent and therefore, I have to look at the right. If on the right hand side, there is no node that means the index is minus 1, I will simply insert it. Otherwise, I will say index is set to right index and continue looking further to the right of that tree to find out what is the next right position. Here is a traverse function. We just go by the order. We have arbitrarily chosen 1, 2 and 3 as representing 3 different traversal orders. If order is 1, we simply output the B index dot data. If left index is not minus 1, that means there is something on the left, then I will say traverse BT index dot left index order. So, you can see that I am calling this traversal again depending upon whether the left index is minus 1 or not. Same thing about the right index that we will do. If the right index is minus 1 or not, we will traverse it repeatedly and these traversals are entirely dependent upon what is the order stated. If order is 1, if order is 2, if order is 3, I simply output BT index dot data. This simple mechanism permits us to do the traversal in all the 3 possible orders depending upon the parameter that we give. Here is our main program. So, binary tree is declared as B1. First of all, I will simply insert in this tree in the order in which they come. We assume that the values come in the order 60 followed by 40 followed by 20 followed by 25. Notice this is the same order in which we saw how the tree was getting concerted. We all use insert, insert, insert, insert, insert. Now, the pre-ordered traversal, the B1 dot traversal 0 and 1. For in-order traversal, we use the order value 2 and for post-order traversal, we use the order 3. So, exactly the same function will traverse it differently depending upon whether we wanted pre-order, in-order or post-order traversal. Note that because the functions which we have written and the strong STL functionality of pushback, we are able to create a binary tree simply by a sequence of inserts. As a matter of fact, if I have 200 or 500 nodes to be inserted, I would obviously put these in a loop and a simple iteration will create the binary tree of any length appropriately. If we look at the output created by this program, this will be the pre-order output, this will be the in-order output and this will be the post-order output. You can go back to the previous session and cross check that these indeed represent the correct order in which the node should come out when traversed, when the tree is traversed in pre-order, in-order or post-order fashion. In a nutshell, we have actually seen a computer program which uses the STL library facilities of a vector with a pushback kind of function already available and we have seen how a binary tree could be implemented and different traverses could be done for the binary tree that we so create. Thank you.