• 1 day ago
Transcript
00:00Next time we talk about that if I have a try given which I have inserted all the notes inside,
00:05then how do I actually search my word? Let's take a example of this key which I have to search
00:12first in the key.
00:15Try already exists in some way. How can I search D in code? My code is going to be very
00:23similar to insert. In fact, my time complexity will be big of L to equal. To search, I will go
00:33to my letters. So if I want to search D, then I will go to T.
00:38I want to see what I have to search for level-wise on my first level. Because index is on
00:430, so I will search for the first level. I mean, I will search for the next level on the
00:47root of any other children. So what will I have to say? It will be true.
00:53Then I will try to search the next letter on this level. Then I will try to search the next
01:02letter on this level. Then I will try to search the next letter on this level. Then I will try
01:07to search the next letter on this level. Then I will try to search the next letter on this level.
01:11Then I will try to search the next letter on this level. Then I will try to search the next
01:16letter on this level. Then I will try to search the next letter on this level. Then I will try
01:20to search the next letter on this level.
01:22in these three nodes. E exists. Now, when I have seen that if all letters exist, if all letters exist,
01:32then I have to check last one more condition. That is, what is my end of word true or equal?
01:39Searching means that it should exist. Let's take this try.
01:45T H E E R Stored, so if I search for T H E E, this will not return true,
01:52why not return true? Because my word is not finished, my try actually T H E E R Stored, T
01:58H E E Stored, so I have to check this condition in search, if my end of word is true,
02:05so if E is where I get, so I will see that the end of word is already true because
02:09it is marked from pink, so this will be true.
02:12So in this case, I will return the search for THEE to value true.
02:17Now if I search THOR, which obviously doesn't exist in this try, then this process will also be the same.
02:24First of all, I will search T for T, then I will search H for H, then O for O.
02:29In children, O doesn't exist anywhere in H, then I will return false. So I have to go level-wise
02:36and level-wise, I have to search in my current node in children's children if my desired letter exists or
02:44doesn't exist. For this, let's write a pseudo code.
02:47My code will remain in this way like my insert code. I will take a current with initialize which I
02:52will initialize with the root. Now I will go to levelwise. Levelwise can run a loop. Level is equal to
02:5802. If it is my key, then my loop will go to my key.length. Now I will remove index.
03:05Index will be key.caret. I can write i and level. I will remove index. I will check every time
03:15that if
03:18current of children, if it is null, there is no node exists, so in this case, I will give me
03:25this false return. No, then I will update current.childrenOfIndex. Now I will go to next level, this will be
03:37my for loop, and last I will check for my end of world in my current. Now I will convert
03:44this code into our Java code.
03:47I have already created the insert function in the code. Now I am going to create a search function. Public,
03:51static, boolean type. And if I get my word in search, I will return it. True or not, I will
03:57return it. False. I will name it search. In search, I have a string type of key that I have
04:03to search in my try. Now the process is the one that I have written in insert. In fact, I
04:08am going to copy paste it. What did I do first? First of all, I have my current
04:13I was initialized with root and then it went to level wise. This time I have to travel to level
04:180 from key to length. I have to travel every time. I will remove my index. Index will be key
04:26.character at level minus a. Now I will check that if the children have the index of the value of
04:33the children, it means that this current character doesn't exist. So I will return false that the key doesn't exist
04:40in my try.
04:42in that case, I will do current and return current.endofword="true". I mean, my current node inside the letters are
04:52my word, my key is my last letter. Then I will return true. If not, it doesn't exist. It exists
04:58a small part. In that case, I have to return false in this case. Let's do this. In the main
05:03function, I had inserted all the words in the main function. Now, I will search for THEE and THOR. First,
05:11I will search first.
05:12call for this THEE and after that, I will call the search for my key THOR. Let's save them and
05:21now run. First of all, I have a print for THEE is true. For this, I will also call the
05:27search for THEE. So THEE is true and THOR is false. In fact, I can check on any other string.
05:38I have checked for ANY.
05:42which should be true, because a and y already exists here. So, I will check for an and. Let's save
05:50and run. For any, it has true returned and for an and it has false returned. If I talk about
05:58time complexity, I have put the same loop that goes to my level 0, which goes to my level 0.
06:04The length of my string will be more time. So, there will be time complexity here.