00:01
In this question, we will be writing a code in c++.
00:06
So, we are going to use an insert function in this case.
00:23
So, in this insert function, we compare the value to be inserted that is the value, this value that is there with the current node's data.
00:38
So, we will compare this value with the current node's data.
00:49
Then, if it is less, then we check if the left child is null.
01:02
So, if this value that is there, if it becomes less, then we have to check the left child, we have to check if the left child is null.
01:33
Then, if it is, now we will check here if the left child is null or not.
01:40
Now, if it is, then we will create a new node with the given value and we will assign it to the left child.
01:48
Now, if the left child is not null, then we will recursively call the insert function on the left child.
01:56
So, similarly, by this case, if the value is greater than or equal to the current node that is there, then we will check the same thing about the right child.
02:08
And then we will create a new node with the given value and then we will again assign.
02:14
So, this approach will ensure that the new value is inserted at the correct position in the bst based on its value.
02:24
So, let us look at how we will be writing this code in c++.
02:30
So, for that, we will be looking at a basic compiler.
02:36
So, we will write our code here.
02:37
First thing that we will do is we will include the necessary header file and the namespaced standard.
02:49
So, now we have to include a bst class which is defined with public fields.
02:58
So, for that, i will proceed to do, i will proceed to write these sentences.
03:05
Now, what will this do is, as you can see that here the integer data to store the node's value and bst left and bst right to store the pointers to the left and the right children are initial defined.
03:25
So, the default constructor that is there, we have to initialize it now.
03:31
So, for that, we will initialize this as the default constructor.
03:37
So, as you can see, the default constructor that is bst, this will initialize the node's data to 0 and set both left and right pointers to null.
03:51
So, this constructor is used when creating a new empty node.
03:55
So, the parameterized constructor that is bst or bst .integervalue, we have to take that, we have to now define that parameterized constructor.
04:11
So, for that, we will write these sentences.
04:15
So, now here as we explained, as i explained to you the logic earlier, what we have done here is we have taken int n that is there, it has to take an integer that is n as input and initialize the node's data with the given value.
04:38
So, it also sets both left and right pointers to null.
04:42
So, this constructor is used when creating a node with a specific value that is there.
04:48
So, now we have to look at an insert function that we will be using as explained earlier...