• 2 days ago
Transcript
00:00Now let's talk about our first code. We have to code to build a binary search tree.
00:04We will give some values. We have to create BST.
00:09Now these values are given.
00:11What do we do to create BST?
00:13We will insert a node in binary search tree so that it will reach the right position.
00:19In the beginning, we will pick up this node 5.
00:23We will pick up a root in the beginning, which we will initialize with null value.
00:28In the beginning, our root value is null.
00:31Now the first thing we have to do is that we will start building the tree for 5.
00:36If our root is null in the beginning, the most simple way is to insert it.
00:40We will create a new node in which we have value 5.
00:43And we will declare it as root.
00:45We will create a new node in which we have inserted 5.
00:50And we will create root.
00:52This root will point this 5 node.
00:56Next, we will increase to 1.
00:57Now we have to insert the tree into this BST.
01:03First of all, we will compare it with 5.
01:055 is greater than 1.
01:07So we are going to insert the tree in left sub-tree.
01:10We will insert the tree here.
01:13Then we will insert the tree here.
01:13Next, we will compare it with 3.
01:15We will compare it here.
01:17So 3 is less than 5.
01:19So we have to come to left.
01:20Now 3 is greater than 1.
01:22So we will insert the tree in right sub-tree.
01:23In this case, we will insert the tree in 1.
01:24In this case, we will insert the tree in right sub-tree.
01:26So we will create the tree for 1 as a right child.
01:29Because it is null in one's right child.
01:31Then we will come to 4.
01:33What will we do for 4?
01:34We will compare it here.
01:36We will compare it here.
01:424 is less than 5.
01:512 is less than 1.
01:552 is less than 3.
01:572 is less than 3.
01:57Less than 3 means that we should come to left sub-tree.
01:59If left child is empty.
02:00We will insert the tree here.
02:02Now we will compare it with 7.
02:04There will be a comparison of 7.
02:057 is greater than 5.
02:07What is in this case?
02:09We will insert 7 in right sub-tree in the right sub-tree.
02:12Then we will insert 7.
02:13So the final tree is coming to 7.
02:15The final tree will be in the right sub-tree.
02:16see this algorithm as well.
02:17In which 5 will be above 5, then 1 will be above 3, 2, 4 and in right child will
02:22be above 7.
02:23Now if we do this algorithm in order to reverse this algorithm,
02:26let's see it once again.
02:27First of all, we will come from 5 to 1,
02:31and then 1 will come from left child which is null,
02:33so we will print 1 to 1.
02:35Then we will go from right child.
02:37After 2 comes from 3,
02:38so 2 will be printed first.
02:40Then 3 will be printed in 3,
02:43then 4 will be printed in 4.
02:44Then we will go back to 5.
02:48Then 5 will be printed in 5,
02:49and then 5 will go from right child to the right child,
02:51and then 7.
02:52If we also see in order to reverse,
02:54then that is also a sorted reversal.
03:00What did we follow the approach?
03:03Every time we have taken our new node,
03:05we can put a loop here,
03:07and we will take our values area,
03:08and we will try to insert it in BST.
03:12We will try to insert it in BST.
03:14What is the approach to insert?
03:15It is the approach to insert,
03:17that if our values of I,
03:20its value is greater than root,
03:23then we will insert it in BST.
03:29In this case, we will insert it in BST.
03:37In this case,
03:38we will try to insert it in BST.
03:45In this case,
03:45we will try to insert it in BST.
03:47we have defined our BST.
03:48In this case,
03:51we will create our values array.
03:52First of all,
03:54we define our node structure.
03:55In BST,
03:56the node structure will be the same as binary tree.
03:59Because when the properties come in,
04:01the structure will not change.
04:02So,
04:02we will define our static class node.
04:07In which there are 3 values,
04:09our data.
04:10With that,
04:11our left subtree node,
04:13and the right subtree node.
04:15With that,
04:16we will create a constructor
04:17in which there will be our data.
04:22So,
04:23this.data is equal to data.
04:26And the left and right subtree
04:27is already by default null.
04:29Now,
04:30with the main function of values,
04:32we will define another node,
04:33root,
04:34which we are doing with null.
04:36And if we create an insert function,
04:38public static node insert.
04:44So,
04:44what will this function do?
04:46This function will insert our node in BST.
04:49And the return will also value a node type.
04:52What is the value of node type?
04:54This is our root.
04:55Finally,
04:56what will we do?
04:56When we are running into the subtree,
04:59there will be changes.
05:00After that,
05:02how will the subtree see the subtree?
05:03We will return the node and reflect the node.
05:06If there is a change in the left subtree,
05:07then the node is attached to the left subtree.
05:10So,
05:11the change will automatically reflect.
05:13And last,
05:14what will the insert function return?
05:16It will return the root.
05:17So,
05:18we will write our conditions here.
05:20The first condition is that,
05:21if the value of the root is null,
05:24here,
05:24in parameters,
05:25we will have a root.
05:27We will have a new value.
05:30If the root is null,
05:32already is null.
05:32So,
05:32what will we do?
05:33We will create a new node.
05:35We will create a new node.
05:36We will create a new root.
05:38So,
05:38root is equal to
05:40new
05:42node.
05:43We will pass the value.
05:45And here,
05:46we are going to return our root.
05:49Directly,
05:49we can write a return here.
05:51Because,
05:51we are going to store it automatically.
05:54But,
05:55for clear understanding,
05:57we have created a new node.
05:59We have created a root.
06:00And,
06:00we have returned the root.
06:04If the root is null,
06:06then,
06:06we need to find a right position.
06:08We need to find a right position.
06:09We need to find a right position.
06:11The first comparison is that,
06:13the data of root is greater than our value.
06:17In this case,
06:17we have to insert the left subtree.
06:21So,
06:21we will call the insert to the left root.
06:25And,
06:25we will pass our value.
06:27Now,
06:27the insert left,
06:29then,
06:29insert the left subtree.
06:32we will return a pointer to the left subtree.
06:35Which,
06:35we will store in the root.left.
06:40Now,
06:41what do we need to do?
06:41Another case.
06:42Another case is,
06:44when we insert the right subtree.
06:49Right.
06:50Subtree.
06:51In this case,
06:52root.right.
06:54Insert.
06:58And,
06:58value.
06:59Now,
07:00the right subtree will insert the right subtree.
07:03And,
07:03it will finally be stored
07:04by the right pointer.
07:07And,
07:09we will return our root to this function.
07:12So,
07:12we have handled these three cases.
07:14And,
07:14finally,
07:15this function will return its root.
07:16It will return all the changes.
07:18In the main function,
07:19what do we need to do?
07:20In the main function,
07:21we have to traverse the values.
07:23And,
07:24we have to insert the values.
07:27In our BST.
07:28So,
07:28let's put a loop.
07:29Let's put a loop.
07:29For int.
07:31I equal 0.
07:32I less than.
07:34Values.
07:37Length.
07:38I++.
07:41So,
07:41root is equal to.
07:43Insert.
07:44Root.
07:45And,
07:46values.
07:48Now,
07:50how do we verify that our BST is not formed?
07:53In order traversal.
07:54We will print a function.
07:57In order traversal.
07:57In order traversal.
07:58In order traversal.
07:58We will print a loop.
07:59And,
07:59see what is our BST.
08:01In order traversal.
08:02Print a loop.
08:04It is sorted.
08:05Or not.
08:06So,
08:07we will make a function.
08:08Public.
08:08Static.
08:09Void.
08:09In order.
08:11In order.
08:12Pass our root.
08:15In order.
08:16We will know how it is.
08:18First of all,
08:19we will call.
08:20In order.
08:21For the left root.
08:23Then,
08:24print.
08:25Root.
08:26Data.
08:29Call.
08:30In order.
08:30For the right root.
08:31For the right root.
08:33If the root is null.
08:37We will simply return.
08:40In order.
08:41We have written code.
08:42In order.
08:43First of all,
08:43we have inserted values.
08:44In order.
08:45In order.
08:46In order.
08:47Save.
08:48And run.
09:02A sorted traversal.
09:031.
09:042.
09:043.
09:044.
09:055.
09:05And 7.
09:05So our BST is completely created and inserted notes in it.
09:09So for you, a small homework problem is that from 1 to 10,
09:15all the notes you have to form yourself in BST.
09:20After that, we have to change the values in the array
09:24and then we have to print in order to reverse it.