• 1 day ago
Transcript
00:00Now, how do we insert a word in Trice? Now, the operations of Trice are going to insert and search
00:06in Microsoft, Google, Amazon, D-Sherge, and companies are also coming to individual questions. You can directly ask you to
00:15code and show you how a try code is. How do we insert and search? Or in your coding test,
00:21there is a question in which you have to see a try.
00:26Now, in tris, the insert time complexity remains big of L. Here, L is the largest word length of L.
00:36Let's see how big of L comes. Now, to insert in tris, I already have root node exists already.
00:45And in the root node, I have no valid data store. In the root node, by default, my end of
00:50word is false. And in this, there is only information about my children. So, I have an array of 26
00:57sizes. Now, as I have a word, I insert all the words in word by word. And in word, I
01:03also insert letter by letter in word. Let's take my first word. The first word is T-H-E.
01:09So, first of all, I went to T. I will see that I will traverse each level and see what
01:16T exists on this level. Now, in this word, T is my first letter. So, I will see what T
01:23exists on the root level. So, I will see what T exists on the root of children. I will see
01:27that T is the index of T.
01:32This is my expression. When we minus the character character character, we always give an integer value. This is very
01:40basic. In fact, in 1st and 2nd class, we taught our java. Therefore, if I minus the java, it will
01:45be 0. If I minus the java, it will be 1. If I minus the java, it will be 1.
01:50If I minus the java, it will be 2. So, directly, if I minus the java characters, I get my
01:55index. So, my children's array, I will remove the index for t and see if I don't like it.
02:03If null exists, it means that the node exists not. And if there is a valid node exists, it means
02:14that the node exists already exists. So I don't want to create it again. So what do I do? First
02:19of all, I will remove the index index. And I will check what T exists here. So what do I
02:27do? I will insert the new node.
02:30The new node means that I have created a new node and created the root of the children array. I
02:38have stored this node's address. This node will automatically create this node. Because this node is on the T index,
02:47it will automatically be stored in this node. Now I will do the same thing for next index. Here I
02:54will remove the index from H-A.
02:59Array where my index will be already exist, so I will create a new node to next level. Where I
03:07will store H. Next letter which is E. I will calculate a new index from E minus A. Now, E
03:15minus A will be equal to 4, so fourth value, fourth index, I will store a new node to next
03:23level. Here I will store it.
03:27equal. And here the end of word will be false. For this node, the end of word will be false.
03:33But because E is my last node, so here the end of word will be true equal. So, in this
03:39way, I have stored a single word in my try. Now, let's try to try the rest of the words.
03:45Next, what I will do is equal.
03:47So, I will take A to, A will be single character which is A. Now, I will see if there
03:52is a child in the next level, which is not stored in the next level. So, I will create a
03:57node where I will store A to. Now, because A is the end of the word, so here the end
04:04of the word will be true. Next, I will go to the string, it will be t-h-e-r
04:08-e. So, I will take next word which is t-h-e-r-e.
04:13First I will go to T, I will see T exists on the root of next level, T exists on
04:18the root of next level, T exists on the root of next level, T exists on the root of next
04:20level, T exists on the root of next level, T exists on the root of next level, T exists on
04:23the root of next level, T exists on the root of next level, T exists on the root of next
04:26level, T exists on the root of next level, T exists on the root of next level, T exists on
04:26the root of next level, T exists on the root of next level, T exists on the root of next
04:26level, T exists on the root of next level, T exists on the root of next level, T exists on
04:26the root of next level, T exists on the root of next level, T exists on the root of next
04:28level, T exists on the root of next level, T exists on the root of next level, T exists on
04:32the root of next level, T exists on the root of next level, T exists on the root of next
04:45and because e is my end-of-the-word, so the end-of-the-word will be true or equal.
04:50Then I will come to our next string, next string will be t-he-ir. I covered this, now the
04:55two is t-he-ir. First, the t-i, t see the root of the next level, then t see
05:02the next level, h.
05:03HB already is, then H on the next level is E, EB already is, then E on the next level
05:08is I. I don't know, so what do I do? I create a new node after E and store it
05:13in which I will value I. And after I saw that R is what is R, so R will also
05:18create a new node in which my character R will store. Because R is the last character, so here it
05:23will end of the word. In fact, let's write a pseudo code for this. What will happen?
05:30I will take a current node type, which I will initialize with root. Now, in my current, I will go
05:36every time level-wise. Where will I start? I will start level is equal to zero. Where will I go?
05:43I will go to my string's length. Whatever word will be, it will go to my level. This will be
05:51my level zero.
05:52The first character will be my level 1, the second character will be level 2 and the third character will
05:59be level 3. In this way, I will go to L levels. L here is my string length equal. Level
06:05wise, I have to add root. What will I do every time? I will see if my child has current
06:11index stored in this node. I will check if my child has current index stored in the beginning. If my
06:16child is stored in this node, I will check if my child is stored in this node.
06:22Children in which index? The index of my character is minus a equals. If I have null stored in this
06:31case, I will create a new node. And if it is already stored in this case, this is my if
06:40case, which value is null. What will I do in the case? I will perform no action. And simply, what
06:46will I do in my current children node?
06:51Children of CH-A, this is my index node. This node I will create my next current. I mean, current
07:00is root, next time current T will become T, next time current H will become H, I will go to
07:06the next level. Then I will come to the next level, next level, next level. One time, let's see our
07:12next word. Next word which I have, that is equal to A-N-Y. A-N-Y, first of all,
07:18I will come to A-N-Y.
07:20In the next level, a node exists in its children. Basically, my children's array has a node stored. If it
07:31is not stored, then I will create a new node. If it is stored, then I will not do anything.
07:35I will simply update it. Where will I go? On this node. I will see if it exists in a
07:44child or not. What will I do here?
07:49n to create. Next level we will check for this y letter. So, n to the next level, child node
07:55exists in the child node. So, I will create a node. And because y is the last character, here the
08:01end of word will be true or equal. Now, I will come to my next word, next word will be
08:06t-h-e-e. So, t-h-e-e, first check for t.
08:10In root of children nodes, T and A are already exist. Now, I will check in T's children. In T's
08:18children, H has already exist. Then, E will check in H's children. Then, I will check in E's children. Then,
08:23I will check in E's children.
08:24Now, in the children nodes, a r exists and a i exists. I will create a new node for that.
08:33I will store e in it and here, my end of word will be true or equal. So, I will
08:38have a final try structure in which I have stored all the words in which I have stored all the
08:43words in which I have stored only one time. Now, let's write the code for this pseudo-code and understand
08:49that in terms of terms, our time complexity comes only big of l equal.
08:53I have made my node class in which I have defined my children array and my end of word. Now
09:00I am going to make a function. I will name this function as public static. I will make it void
09:05type and in this I will name insert and in parameter I will have string word.
09:11which word I want to store. What do I want to do here? I create a node in current name
09:16which I will initialize with root. Now I want to traverse level-wise. First of all, I am on level
09:210. Where do I want to go? All my word's letters will store level-wise. So, all my word's characters
09:28will have levels in my word. So, my level will be word.length equal. Every time I will do my
09:37level plus plus.
09:38First of all, I will remove my index. The index will be word.character at which I am at which
09:44level I am at which level I am at which level I am at which level I am at which
09:48level I am at which level I am at which level I am at which level I am at which
09:51level I am at which level I am at which level I am at which level I am at which
09:53level I am at which level I am at which level I am at which level I am at which
09:54level I am at which level I am at which level I am at which level I am at which
09:56level I am at which level I am at which level I am at which level
10:08dot children at index ho jayega new node or last me mein kya kar dhungi current ko bana dhungi current
10:15dot children of
10:18index. For next level, my child node will become my current node and now I will go to children's level
10:24level. This will be enough for all my characters to cover word and then go to last. Now I have
10:32done all the things where there was not a node, I have created a node and the rest I have
10:36done updates. If I reached the last node in my current node, my last node will be stored. Then I
10:43have done the end of word.
10:46true or equal. In other places, the end of word by default is false. Just where my last character comes
10:51from my word, my end of word will be true. Let's do this again, save. Now, what will I do
10:56with main function? I will insert all of them. I will call insert function. I will call a for loop.
11:02int i equals zero. i less than words dot length i++. I will call insert function for my words of
11:15i.
11:17In this way, I have stored all the words in this way. The code is only stored in this code.
11:24The code is only stored in this code. The code is only stored in this code. The code is only
11:27stored in this code. The code is stored in this code. The code is stored in this code. The code
11:31is stored in this code. The code is stored in this code.