 Welcome to this course on data structures and algorithms. We are continuing our discussion with binary trees. In this session, we shall look at the implementation of Huffman coding. You will recall that last time we discussed Huffman code as a minimal code that can be associated with individual characters to represent them in a more compact fashion. We will look at a computer program which can implement the Huffman coding. We use some simple data structures, a node structure which will have the character, its frequency of occurrence and it will have a pointer to the left or right nodes. Please recall the tree that we had seen. All our characters appearing in the file will always occur as leaf nodes in our tree. So, in this structure the leaf node will actually have the character which is to be represented by some code. Now, since we want to impose an order on the nodes that they appear, we will require a function to compare two different values. So, there is a structure called compare which contains a function object. This particular function object will return true if the frequency of second node is less than the frequency of first, otherwise it will return false. This will help us to compare two frequencies. Next we have the functions which we need, a make node function which will create a node of the stripe structure node. We already seen that the node will contain a character, its frequency and left right child pointers. The second function is actually the main function which will create the Huffman course. What it does is, it will push the node in the priority queue which is defined by the ordering of the function object called compare. So, anytime a new node is to be inserted, it will use this function object compare and decide whether the priority order should be above or below the previous element. This will add two elements having the smallest frequency and will push the result back on to the priority queue so that the order is maintained. So, it is a very simple implementation and we will use the STL facilities which are available for creating priority queues. We will of course require an additional function to display course. These will print the Huffman code for each character and all that we need to do is we identify from the roof node to the respective leaf node to get the code for that particular character which appears in the leaf node. Let us look at the program. So, this is the standard initial, this thing where we are defining maximum tree height as 100 which is an arbitrary thing. The node is defined as care CH in frequency and left and right. You will notice one important thing. We have said that in our tree all actual characters appearing in the file will appear at the leaf node. At the parent node we will not have any character because the parent nodes represent the combined frequency of occurrence of the child node. Therefore, in such nodes we will have to insert an artificial character as we shall see later. This is the compare stuck. So, it is a bool operator which will actually check the frequency of B and frequency of A and it will return if the frequency of B is less than frequency of A it will return true other way to return false. This is the function that we shall be using in order to insert nodes in the proper order in the priority queue that we are going to use. Here is a make node stuck. So, H node temp. Now, we are just saying we are creating a temp node by allocating memory. This is the way in which you malloc depending upon the size of H nodes are you allocate memory and create a temporary node. Once a temporary node is created it has to be filled up with the given character and given frequency which are appearing as parameter. So, we simply set CH to temp, CH temp frequency to frequency and since this node has been just created it has no left child, right child. So, we set these pointers as null is as simple as this to create a node. Next we come to the Huffman coding. Here we have the code max tree height as we have defined already. Now, we define the H node priority queue is the element that we use from the STL library. So, this priority queue has a template in which we define H node there is a vector H node and the compare function which defines the order for insertion into the priority queue. We will arbitrarily name our object as pq which is stands for priority queue. What we do? Up to the size we simply push into pq by making make node each individual character in its frequency that we have got. Please note that the care array and its corresponding frequency is given as parameter and the size is given as a parameter. So, we simply use this to create a iteration to insert all of these in the priority queue. Please note that when we insert them pq dot push function that we use we have seen that function how these nodes get inserted. Now, we have to add code for adding two smallest frequency elements and pushing back the result in the priority queue. At the end we will have to display all the codes for which we invoke the display function. So, note the very simple but important thing we define a priority queue which for our example we have called as pq then we push on to this pq all the characters and frequencies that we have received. Next we will need to write code for adding two smallest frequency elements and pushing the result back into the priority queue. So, let us look at that code now. First code for adding two smallest frequency elements and pushing it back. Now, all that we do is we check whether the size is not equal to 1 that means we have number of things to look at. All that we do is whatever is the current pq we look at pq dot top and assign it to left and pop it out. Then we look at pq dot top again assign it to right and pop it out. So, two elements have popped out of our priority queue one we are calling left another we are calling right. What we do now? We actually make a temporary note again ok where we have the left frequency and right frequency and please remember we need to now insert an arbitrary character because this note does not represent an actual character in the file we are arbitrarily using the hash symbol for internal nodes. Next we assign left to temp left and right to temp right and push back this again on to the priority queue. So, what has happened? We took out two elements from the priority queue we actually calculated the sum of their frequencies. We created a new node and pushed it on top of this node by appropriately setting the left and right pointers. Please note that when we use the push this will be pushed at the right place. Here is the display program which is again simple if the left of the root exists then code index is set to 0 and I display codes root left code index plus 1. If root right returns correct then code index is set to 1 and we display codes root right code index plus 1. Here is the main thing if the left pointer is null and right pointer is null then we now got complete code for that particular character because we have reached the leaf node. So, we output half one code for the character the character which appears there at root ch and we put for i to 0 to that index that we traverse we simply output code i 1 by 1 and that will represent the code for this particular character. So, it is an extremely simple and straight forward way of traversing this priority queue to determine the code for individual characters. The main program is very simple since we are looking at this example where we had only six characters we initialize size to 6. We insert these six characters as a b c d e f and we insert these arbitrary frequencies 15, 10, 12, 18, 55, 16 which we saw in the example last time. All that we do is once this is done we invoke half one codes given this care array the frequency array and the size. This invocation will do everything from creating the appropriate priority queue as a binary tree and then printing out all our codes. You can actually hand execute this entire code and confirm that you get the right Huffman code as was explained in the previous session on Huffman code. To conclude in this session we have used the STL facility of priority queue in which by giving a compare function we ensure or rather the STL ensures that the nodes are always put in the proper order and the proper order in the entire queue is maintained. This is another classic example of binary trees used to represent different characters by minimal code which we call Huffman code. Thank you.