 Welcome to this course on data structures and algorithms. These sessions in this week we are going to look at trees. To begin with, we will look at the basic operations on a binary tree. So, first of all what is a binary tree? A binary tree has nodes such as these. There is a parent node and a child node concept. So, for example, this is a parent node 60. It has a left child which has 40, it has a right child which has 90. A node can have either a left child or a right child or both. There are of course special nodes which are called leaf nodes which do not have any children. So, in a nutshell the binary tree starts with one node which is typically called the root node and then it grows to have left or right children. You can notice one property that the nodes are always ordered. So, you can see that a left child has a value smaller than the root and the right child has a value higher than the root. This will be true for any combination of three nodes. The parent will have a value, left child will have a smaller value, right child will have a larger value. In order to represent these nodes internally, we use a node which has the following structure. It has data which is actually the element. Then we have a left index and a right index. So, you have left index which will point to the node on the left of the tree and right index which will point to the node on the right of the tree. Let us look at the way the actual data would be filled in. Assume for example that you have three data values 10, 8 and 12. The first node which comes out here, the value 10 is inserted here. This has left and right child. So, left child has an index 1, right child has an index 2. At index 1, you have the data node which contains 8, but it has minus 1 in both left and right child pointers or indexes because there are no left or right children at the moment below this 8 or below this 12. So, it is a simple representation internally. We will see how we can exploit this representation to constitute or construct a binary tree. The basic operations which we require of course are first and foremost to make a node. Typically for example, the root which has no children will just be making a node. Then we might require operations to insert the left node and the right node. That is how the tree would grow. Once an entire tree is constructed, we need to traverse that tree. Please note the property of a binary tree. It is always sorted implicitly. We might want to traverse it in different fashions. The three traversals which are well known are called pre-order, in-order and post-order. Just to give you a hint, we had seen when we discussed the representation of expressions. We said that normally we would have an in-fix way of writing expression, but we noticed that if we could convert them into either prefix or post-fix, their evaluation became easier because we did not have to bother about the priority of different operators. In exactly the same fashion, pre-order, in-order and post-order traversals are possible and are used very often. Let us look at how a node is made. Suppose we want to create a node with value 60. It is initialized only when the tree is empty. So, we make a node with data as 60. Data is 60 and currently left index and right index are minus 1. So, that is simply making the node or the root node. Having made that, assume that the next value that comes in to be inserted in the tree is 40. Since 40 is less than 60 and there is no left child for 60 at the moment, we will insert 40 as the left child of 60. This index will be 1. That means the data for this node is 40 and its left index and right index are minus 1 because it does not currently have any children. Please note, however, that the left child index of this node 60 will have to be now changed to 1 because it now has a left child. Consider similarly, another value which comes in is 20. For inserting any node, the typical sequence is we start at the root and start traversing from the root towards a value which is close to 20. For example, 20 is less than 60. So, it travels to the left. We notice that there is node at 40, but 20 is less than 40 and there is no left child of 40. So, we know now 20 has to be inserted as left child as 40. Consequently, we will set left index of 40 as 2 and insert the data value 20 in a node here with left index and right index currently as minus 1. We will follow exactly the same procedure. So, in the subsequent slides, we will see how different data values that come in, how they get inserted into the tree. So, here is the value 25, for example. We will do the same thing, 25 is less than 60. Come here, 25 is less than 40. Come here, there is a node called 20. 25 is greater than 20. So, we need to insert 20 on the right of this 25, on the right of this node, that is because 25 is greater than 20 and 20 does not have any right child at the moment. So, we will set the right index of 20 as 3, which points to this new node and the data is 25, left index is minus 1, right index is minus 1. Suppose, we now get 90. Now, starting traversal from the root, we notice that 90 is greater than 60 and there is no right child of 60. So, we insert 90 at the right child and insert the index 4 in the right child index of the parent node 60. We get 23, for example. We again start traversal, 23 is less than 60, 23 is less than 40, 23 is greater than 20. We go to the right, but there is already a node that is 25. 23 is less than 25. So, therefore, we have to create a left node at this juncture, which is how it is created and the left child index of 25 is set to 5, because that is where the new node appears. Very simple and straightforward procedure when applied to nodes one after another, the data elements one after another as they come in, we see that node 95, value 95 may be the next value coming in, that will be inserted at the right child of 90, then the value 80 may come in, that is inserted at the left child of 90. Note that we continue traversal and insertion exactly as we described earlier. Consider 85. So, again we notice 85 is greater than 60, but 85 is less than 90. We go here, we come across 80, 85 is greater than 80. We insert it as right child, because 80 does not currently have the right child indicating a new index 8, which is inserted as the right child index of 80 and we insert this node here. We have seen today how various data elements can be inserted in a binary tree. A binary tree has nodes which have left and or right children and intrinsically the binaries have a property that they represent an implicit sorted order on all data values which are stored in the binary tree. Thank you.