 Hello everyone, my name is Nikhil Gajam working as assistant professor in computer science and engineering department in Walchand Institute of Technology, Solapur. In today's session, we will see what is threaded binary tree and what is red black tree. So, these are learning outcomes. At the end of this session, student will be able to augment with a threaded binary tree and student will be able to augment with red black tree. Now, this is an introductory part before actually going to threaded binary tree. We will see what is binary search tree. Binary search tree. In binary search tree, every node on the left subtree is having value less than or equal to its parent. And every node on the right subtree is having value greater than its parent. So, it is a special case of binary tree. So, this is an example of binary search tree. Now, we will see what is the disadvantage of binary search tree and why we are learning for threaded binary tree. So, threaded binary tree. In binary tree, with n nodes has two n pointers out of which n plus 1 pointers are always null. Now, we will see this is an example which contains 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 nodes are there. For 10 nodes, a binary tree having two n pointers. For each node, there are two pointers one for storing left child and one for storing right child. So, for a binary search tree with n nodes, there are two n pointers. For this binary search tree, there are total 10 nodes. So, 20 pointers are there out of which n plus 1 pointers means 10 plus 1, 11 pointers are there which are always null. So, this is 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 and 11. So, 11 pointers are there which are null. So, this is a disadvantage of binary tree. So, to overcome this disadvantage, we are using actually a threaded binary tree. So, we will see what is actually a threaded binary tree now. A binary tree with n nodes has two n pointers out of which n plus 1 pointers are always null. So, half of the memory allocated for pointers are wasting. So, to utilize this wastage memory by storing some useful information. So, what information we can store in null pointers now? Left null pointers can be used to store address of in-order predecessor. Now, what is predecessor? Predecessor means previous node in in-order traversal. Right null pointers can be used to store address of in-order successor. Now, what is in-order successor? In-order successor means next node in in-order traversal. Now, these pointers are called as threads. So, instead of storing null in a pointer, we can store either in-order predecessor or in-order successor. And a binary tree formed by this is called as threaded binary tree. We will see now. Now, this is one of the threaded binary tree example. If only left null pointers are used for storing addresses or as a threads, this threaded binary tree is called as left in threaded binary tree. Now, what can be stored in left null pointer? Left null pointers are replaced with in-order predecessor. So, in-order predecessor of 9 is 8. So, left null pointer of 9 can be used to store address of node 8. Similarly here, in-order predecessor of 6 is 4. So, 4 can be used as in-order predecessor that can be used for storing address in left child of 6. So, if only left null pointers are used as a thread, so this tree is called as left in threaded binary tree. Similarly, if only right null pointers are used as a threads, so this is called as right in threaded binary tree. In right null pointers, we are storing address of in-order successor. So, we will see in-order successor of 1 is 3. So, address of 3 is stored in right child of 1. Similarly, in-order successor of 5 is 6. So, address of 6 is stored in right child of 5. Similarly, in-order successor of 7 is 8. So, address of 8 is stored in right child of 7. 11 is in-order successor of 9. So, address of 11 is stored in right child of 9. So, this tree is formed where only right null pointers are used as thread. So, this tree is called as right in threaded binary tree. Now, next one, if both left and right null pointers are used as threads, then this tree is called as full threaded binary tree. Left null pointers are replaced with in-order predecessor and right child are replaced with in-order successor. Example, suppose we will take a node as 7. In-order predecessor of 7 is 6. So, left child of 7 contains address of 6. In-order successor of 7 is 8. So, right child of 7 contains address of 8. So, both the null pointers are replaced with in-order predecessor and in-order successor. So, this tree formed by this is called as full threaded binary tree. So, guess the answer, what can be the advantage of using this threaded binary tree? You can pause the video and you can answer now. One of the answer is, first one, you can store extra memory instead of null pointers. And another answer is, in-order traversal is easy in threaded binary tree that can be formed without recursion. So, without recursion you can traverse in-order in threaded binary tree. So, this is one of the advantage of using threaded binary tree in addition with storing memory. Now, we will see what is red-black tree. So, before going to red-black tree, we will see what is balanced tree and what is non-balanced tree. So, there are two trees are there, both are binary trees. This tree is not balanced tree and this tree is balanced tree. So, what is the advantage of using balanced tree over the non-balanced tree is time complexity for searching in balanced tree is less as compared to time complexity for searching in non-balanced tree. Suppose I want to search E, so total five comparisons are required to find out whether E is present in this binary tree or not. But in case of balanced tree, suppose we will take an example of this. So, maximum three comparisons are required to find out whether a particular node is present in this tree or not. So, within three comparisons you can find out whether E is present or not. So, this is an advantage of balanced tree over non-balanced tree. Now, we will see what is red-black tree. In red-black tree every node is either colored with red or black and in this tree all null pointers are replaced with special nodes called as external nodes. Now, there are some properties for red-black trees. We will see these properties one by one. In red-black tree root is always black. All the external nodes are black in color. Red node cannot have red children but red node can have black children. Fourth property is a black node can have red or black children. And last property for each node n, all the path from n to all the external nodes contains same number of black nodes and this number is called as black height of the tree. We will see the example now. This is one of the example of red-black tree. We will see one by one. Root is always black. So, root is 13. So, this is black. Next one all external nodes are black. So, nodes with rectangular shape are called as external nodes. So, these are all external nodes. These are in black in color. Next one, red node cannot have red children but it can have black children. So, these are red nodes. So, these red nodes do not have red color children but it may have a black colored children. A black node can have red or black children. So these are black nodes. Black nodes may have red or black children. For each node n, all the path from n to external nodes contains same number of black nodes. Suppose we will take an example of suppose 13. From 13 to all external nodes, there is same number of black color nodes from path from 13 to these external nodes. Suppose we will take a path from this. So number of black color nodes is one only. Suppose we will take a path of this like this. So number of black nodes is only one. So suppose we will take the path of 13 to this external node. So number of black color nodes in this path is only one that is 25. So last property is for any node n, all the path from this end to external nodes contains same number of number black nodes. And this number is called as black height of the tree. So in this case, black height of the tree is one. Okay, find out the answer. What can be the advantage of using red black tree? You can pause the video and you can answer now. Okay, answer is searching is more efficient as a red black tree is balanced binary tree. So whatever property we have seen just now, using that property, a tree will always construct as a balanced tree. Non balanced red black tree is not possible because of this property. So whenever you construct a red black tree using this property, always red black tree will be constructed as a balanced tree. So we have already seen if tree is balanced. So searching time for balanced tree is less. So this is one of the advantage of using red black tree. Red black tree is always balanced tree. So searching is more efficient in red black tree. So these are the references I have used for creation of this video. Thank you.