So in this video we are going to see how to implement a binary tree. We are going to actually write down the code for the implementation of binary tree right. In the previous video we have already discussed what is a binary tree and different types of binary tree as well as properties of binary trees right and how to represent a binary tree.
tree fine now first of all we will write down the code and as well as we will discuss the execution of that code so now what is binary tree see as we have discussed each node is going to contain at most two children it moves to means either 0 1 or 2 children like this fine now this is what a node node is having some information as well as node may or may not contain the links to another node right so actually the node is we are going to represent this node something like this this node is having three part one is data part one is what left link and one is what right link means this is going to contain address of the left child. This part is going to contain in address of the right child right so this tree we are going to represent something like this each node see this node is containing the right no right child is there that is why this is what null here 7 is having no left child that is why the left pointer is null right and minus 1 this is leaf node this is leaf node means the left and right both pointer would be null so now we will see how we are going to create this binary tree right see now these are what nodes how we are going to create the nodes that we have already already discussed when we were discussing the linked list implementation right so now this is what we are going to dynamically allocate the memory to these nodes fine in the list in the linked list we always maintain what the head pointer fine you have the head pointer if you have the head pointer you can easily traverse all the nodes of the linked list same in the same way in the the tree we have what we will have with us the root pointer or you can say the pointer to the root node right here we will contain what this one address of this root node that is 100 if you have the address of the root node then you can easily traverse all the nodes from the root node right so here we have what you can say pointer root pointer and there we are going to store this one address of this one right and we just maintain this root here i am taking the name only root but this is what a pointer not root node this is what the root node so now see here each node is having what the data part that is integer the data type of this is integer and data type of this and this is what these are pointers right so these are pointers to node fine so now you have we are going to define your own data type that already we have discussed in linked list implementation so this is how we are going to define our own data type three parts struct node is the data type three parts is there one is data part and two are left pointer and one is right pointer right now see in main function what you will call we are going to create this tree right so now in main function we are going to call what create function fine now what we want after creating this tree what you are going to store this value the this address that is 100 fine so now i want this this root the sorry this create function would return this 100 this pointer right so the pointer is what this pointer is pointer to node pointer to this node struct node so obviously you are going to define this pointer first of all right so here first of all you will write struct node asterisk root so now here asterisk means this is the pointer and here why you are writing this struct node this thing also we have discussed many times see this root is containing address of this node and here always you will write what the data type of that variable whose address this pointer is going to store this pointer is going to store address of this node and data type of this node is we have already defined that is struct node that is why here I am writing struck node now whatever this this create will return means create will return what address of this node that I am going to store in root. So here you will write what root.
So now this is what calling of this create function. Now we are going to define this create function, right? See now what should be the data type of this create function.
See when a function will return integer type then always we type here we write here int if function does not return anything then we write here void but now this function is going to return what address of this node right so this is going to return what pointer value but you can say pointer to this node pointer to this node so now what data type you should write here struct node is straight because it is going to return pointer to node. So now for inserting these values in the tree, first of all, we are going to create this node, right? And you are supposed to dynamically allocate the memory to these nodes.
So now here, see this thing already we have discussed when we were discussing the implement sorry the that linked list in the data structure many times we have discussed this thing so first of all you are going to create a node you can say you can take new node and dynamically how we are going to locate the memory using malloc function in c in c plus plus we will use what that new keyword fine and the syntax also we have discussed many times malloc here you will write size of keyword and size of this one for this node I want how many bytes see that you will write here the data type of this node that is struct node the data type is struct node now this malloc is going to return the void pointer but here this new node is what this is pointer to a node so you have to typecast this thing and whatever this will return we are going to store in new node and now we will ask for the value from the user which value he wants to insert in that true using printf and scanf so you need some variable to store that value suppose I am taking X so here you need to declare this variable int X right now suppose here see here I don't want to insert any node further fine so how we are going to write down that condition that base condition returning condition. So suppose When you are entering the data, if you don't want to enter any child, so you can insert simply minus 1. And if you have entered minus 1, then simply it will return 0 or you can say null. This execution also I am going to show you.
Fine. So here simply you can write enter minus 1 for no node. Right.
And here you can check one condition. If x is equal to is equal to minus 1. In that case, simply you are going to return 0. what you can say null right else now you are going to write down what see now see here in this case we are going to use what recursion because suppose we have entered four then the left child you have entered three then again the left child again the left child and once you don't want any left child then here i don't want any left child also i don't want any right child then back to here any right child you want suppose you have entered minus one means back to here here i want that right child So this is what a recursive call. This is what recursion. So the prerequisite for this video is what you need to know what is a recursion and how recursion works.
Or basically you need to know how the memory is to be allocated when you are going to execute the recursion. That thing is very important. If you know that thing working of that recursion, the memory allocation in case of recursion, then you can easily understand the working of this binary tree implementation.
Right. so now I have entered suppose the data 4 now obviously you need you have created this new node but initially this new node is what don't have anything so you need you need to store that value 4 in this new node right so here how you will write new node and in data part i will store x fine see now i'm going to rub this thing and i'm going to tell you how this is going to work see this is how we have have created this new node and address i have stored in this new node pointer that is 100 suppose and here i have stored in the data part 4 if you want here you can store null and null and null zero and zero but here i'm creating this tree because i want the left side also So right child also here I will store address of the left child. That is why I am not storing here null. Initially suppose I am storing in root also null.
Right. Now what you will do. So here you can write simply after. Creating this root pointer here you can simply store root as equal to 0 or null.
Right. Now see here. Now I want to create a left child. So again you need to call create function.
Right. And also I want to create right child. So in right here also you are going to call the create function. So within this create I am again going to call create function.
This is known as what? Recursion. Because a function create is going to call itself again.
Right? So this is called a recursion. Fine.
Now, here after storing the x value here, here I will ask from the user enter the left child. Right? So here simply you will write left child of This one.
That is left child of 4 I want. And here now x value is 4. So you can here write. Left child of. Sorry here you will write percentage d.
And here you will write x. Means left child of 4. Right. And here same you are going to what?
Call this function create function. Because same processor I want here. This node should be created.
Right? So here simply you will call this create function again. And what this create function is going to return? A pointer to node.
Means if you call create function here. Here I am calling to the left of this one. Left child of this one.
here. Here I am calling that create function. So suppose the address of this one is 200. So it should return 200 so that I can store this 200 here.
Obviously I want to store this address here. Right. So now see whatever this create function will return where you are going to store here. That is new node left. Right.
Because to access this node you need a pointer and pointer to this node is new node. Right. So here simply you you will write new node left right now see please do not get confused after creating this node that new node is going to point here so this new node is not going to point here that is why i am saying you first of all please read how the memory is to be allocated in the function call because see when a function is called from the main function then the memory is to be allocated to this function in the stack and the most important point is different copies of all the local variables would be created for each function call right so this is what X this new node these are what local variables right so whenever you are going to again call this create function then one copy of this new node would be created here also if you call at this side then here also one copy of this new node pointer would be created so here these are what the copies of this local variables it's not like that after create creating this this new node would point here and this link would be broken see this thing how this recursion recursion is going to work this thing also we will discuss in a separate video in detail right now after this left you need what right child also so here simply you will ask printf enter Write child of percentage d and here you will print x.
Right. So here same you will call create function. And what this create function will return here?
This address of this. right child suppose address is 300 so it is going to return 300 so that i can store here 300 right so now how you can access this part new node right so whatever this create will return that thing we are going to store in new node right in this pointer and Finally, we are going to return this one, the address of the root node so that I can store this 100 here so that this root can point here. And once this pointer is established, we are going to have only this root pointer value.
Right. So here after that, you will write return new node. Fine. Now see the working of this code.
See whenever the function is called from the main function the control will go into the main function. One root pointer would be created initially we will store here null. Right. Like this here we are going to store.
going to store here null now i am calling this create function whenever the create function the any function is called from the main function the memory is to be located in the stack right so now see the working of this recursion here suppose i have allocated the memory in this stack right for this create function so now the create function whatever the these statements in the create function these should be copied here all these statements from here to here so here i have numbered these statements right roughly i have numbered all these statements would be here in this stack the memory has been located for this create function in this step right so mainly i'm writing the main main statements here this two statement means this new node would be created right as we have discussed here the third statement in the third this dynamically memory has been allocated we have entered the data that is this is the fourth statement this is the fifth statement we have entered the data x now i have checked six if x is equal to null sorry minus one then return zero but here i'm entering four so this This condition would not be executed. Although this statement is here. So this statement would not be executed.
Now. New note data and x is equal to x. So I have put here 4. Now see.
Enter the left child of x. Means enter the left child of 4. That would be printed. Right. Now.
This statement. The 10th statement. See now. x. This is the 6th statement.
This one 6, 7 and you can say 8. This is what? Bye. 10th statement now again create so from the 10th statement again we are going to call create function means again this you can say stack would be there again all see different copies of these local variables would be created for each function call so here also all the statements would be there Here you can say 1, 2, 3, 4, 5 and up to 13 statements.
Right. Now here also we have see after this 10th statement we have 11 then we have 12 statement. Right. And then we have 13 statement.
So now see in this call again. All the statement would be executed here. Right. Again this new node is to be created. So now here suppose I have created this one.
This new node. So now here also I have a different copy of this new node pointer. Suppose this is 200. So here I have 200. Right.
So here you can say. Here also we have new node is 200. Here in this case new node is having 100. These are two. you can say copies of the same variable, right? For different function calls.
Fine. Now, memory is to be created minus 1. We haven't entered minus 1. Suppose I want to insert some data. I want to insert here 5, right? So, these statement would not be executed here.
Again, data would be entered here. Now, again, it will ask for the left child, means this one, the 9th statement and the 10th statement. So, here, again in the 10th statement, I am calling create function. So here again I am calling save all the statement would be executed here that this create function. In this case suppose here I have new node is equal to 300. Here I have created a new node right and here I want to store 7 fine.
Now in this case new node is having 300. So this new node is pointing to here right this new node. 3 copies of this new node I have now as you can see. See here suppose 300 same these statements would be created.
Now see here the x value is 7 because I have entered 7 here. In this case at this case I have entered 5. 5 and here I have entered 4 right. Now I have entered 7 it means these statement would not be executed. Now again enter the left child of x the 9th statement in this stack would be executed. Now at the 10th statement again create create function would be called, right?
Here you can see create function would be called. Now, again the calling of the create function now same, all different copy of these local variables would be here created in this stack for each function call, right? So, here also all these statements 1, 2, 13 statement would be there. Now, we will see the execution. Now, suppose here this new node would be created, but here I have entered at the 5th and 6th statement, I have entered what x value is minus 1. right, now if x is minus 1, then the control will go within this if statement, means return 0, the control will not execute these statements, right, because now this statement is true, fine, now return 0. It means it is going to return.
Return means here. Where you have called. Here I have called in the previous stack. Right? So now it will turn 0. That would be storing new node left.
So here it will return 0. Here in this case new node is what? 300. Means here. So new node left. Here I will store 0. Now see here this is what the base condition when x is equal to minus 1. one from here You need to return. Once the base condition is reached, from there the function is going to return the value where it has been called.
Right. And then the memory has been destroyed from this stack and this process continues. Right. So now here I have written here 0. Now, the The control is here. I have returned the value here.
Now after 10 here I have 11, 12 and 13 statement. 11, 12 and 13. Now enter the right child of x. Here x is 7. So enter the right child of this 7. Again you are going to call. create function right suppose i am calling again here see here at the 12th statement at the 12th statement again this create function would be called right one stack would be created now suppose here i have entered x is equal to minus 1 same in this case so it is going to return back to here right it is going to return what null and whatever it will return we are going to store here new node right so it is going to return here here the new node is 300 300 right here you will store null fine now after these statement after the 12th statement the 13th statement control will go to here now in 13th statement you are returning what return new node now it will return what new node that is 300 would be returned where the 300 would be returned here back here because here i am calling this function right so now 300 would be returned so 300 would be stored here right so now this is what the left child of this one fine Now see, after returning of 300, here, after 10th statement, here some statements are there which are still not executed.
Right? Means this one, 11th, 12th and 13th. 11th, 12th and 13th. We will ask for the right child of X. here x is 5 right child of 5 means this one right suppose right child of here x means it is again going to call create function so here this 12th statement is again going again going to call create function right suppose here i am i have entered x is equal to minus one means here the return zero it is going to return zero here fine so now here you will store zero there is no right child right now after this 13 statement is what return new node it means it is going to return here because here i am calling this this function now here this is the function call that is it is going to return what 200 return new node here the new node value is 200 so it is going to return 200 so here you will store 200 so this is what a link to here that's exactly we want right now here also 11 12 and 13 statements 11 is right child of x so here again 12 statement is going to call that create function right and suppose here also you want to create here this new node you want to insert some value so here you can insert x value is you can say 10. Fine.
So here also same execution would be there. That I have discussed here. Right. This is just a brief of how the recursion is going to work. This thing we are going to discuss in detail also.
Right. The working of recursion. Now see.
Here suppose I am entering 10. Null. Null. So this 300 would be returned here. Right.
And at last. At last. When.
After these returns. 13. Here also this return new node. Means 100. 100 would be returned finally.
So this 100 would be stored in new node. So here you will store 100. So this is going to point here. So this is the implementation of binary tree using recursion. Now here if you want to know that you have inserted this data at the correct position or not.
Then what you can say? Here you can find out the inorder traversal or preorder or postorder traversal of the tree. right this thing how to find out the in order traversal pre-order traversal and post order traversal that thing we have this we will discuss in the next video with code fine how to find out these traversal this thing i have already discussed but there i haven't discussed the code but in the next video we will discuss these thing all the binary tree traversals with the coding right so now i'll see you in the next video till then bye bye take care