Trie Data Structure Implementation (LeetCode)

แชร์
ฝัง
  • เผยแพร่เมื่อ 2 ธ.ค. 2024

ความคิดเห็น • 127

  • @chiranjeevipippalla
    @chiranjeevipippalla 3 ปีที่แล้ว +35

    This is gold. 👏👏👏 Couldn’t find a better explanation of Trie. Thanks brother

  • @msadityakumar1
    @msadityakumar1 2 ปีที่แล้ว

    Thanks Michel, the video was so good that I could write a Trie DS myself within 20 mins.

  • @sachinmagdum
    @sachinmagdum 3 ปีที่แล้ว +4

    Good one! Small optimization/correction - char c at each Node can be removed (not needed).

    • @sachinmagdum
      @sachinmagdum 3 ปีที่แล้ว +1

      @John Gelson you don't. Isn't the location (index) in (Node[]) array represent the letter (ASCII value) itself?

  • @lakshmireddy1488
    @lakshmireddy1488 3 ปีที่แล้ว +1

    checking for Trie implementation all over the internet finally found and he also explained very well thanks, bro....

  • @shubhammittalSHM
    @shubhammittalSHM 3 ปีที่แล้ว +6

    What an amazing explanation! By far the best. He makes difficult concepts look so easy and very intuitive.
    Thank you so much, Michael.

  • @gregoryrobertson
    @gregoryrobertson 6 หลายเดือนก่อน

    Man, whenever I see your face after a google search on a data structure I feel a sense of relief. Thanks a lot for making these amazing videos.

    • @AlgosWithMichael
      @AlgosWithMichael  6 หลายเดือนก่อน

      That is such a huge compliment, thank you so much!

  • @disantha4447
    @disantha4447 4 ปีที่แล้ว +4

    I've always find your videos are useful...Great Explanation....This channel deserves more subscribers..

    • @AlgosWithMichael
      @AlgosWithMichael  4 ปีที่แล้ว +3

      I appreciate that, I try to put a lot of effort in my videos!

  • @rembautimes8808
    @rembautimes8808 3 ปีที่แล้ว +9

    Wow, I'm watching this on Sat morning as a warmup exercise to my coding routines. Amazing in its simplicity, coherence and how the author went straight to the point.

  • @hellorsanjeev11
    @hellorsanjeev11 3 ปีที่แล้ว

    Best video on Tries on TH-cam - Thank you so much. I just subscribed myself.

  • @bd3723
    @bd3723 4 ปีที่แล้ว

    Best description on tries ever! I've watched other ones but they were more complicated for some reason

  • @shidajieqing
    @shidajieqing 3 ปีที่แล้ว

    How I didn’t discover you sooner, your explanation is the best!

  • @arvindmega
    @arvindmega 4 ปีที่แล้ว +1

    Great explanation. No more time than it needs to take to explain the DS. Awesome work dude. Keep it up.

  • @mukundnarayanamurthy
    @mukundnarayanamurthy 3 ปีที่แล้ว +1

    Good job brother, you are really crisp and articulate with your thought process and approach towards explaining things. Wish you success , cheers 🥂

  • @huseyinbarin1653
    @huseyinbarin1653 2 ปีที่แล้ว

    Thanks, very clear explanation. "isWord" flag is fucking awesome, you can hold two words in the same path like cat and cats. With this flag when we search "cat", it will return it as true.

  • @Anubis10110
    @Anubis10110 3 ปีที่แล้ว

    Very calm, easy and straight forward explanation.. thanks

  • @AerosDaDinoHoodie
    @AerosDaDinoHoodie 2 ปีที่แล้ว

    Very good explanation, thank you!

  • @__--_--_-----
    @__--_--_----- 3 ปีที่แล้ว

    this is incredible, fantastic work Michael

  • @sumodbadchhape9823
    @sumodbadchhape9823 3 ปีที่แล้ว

    Really great explanation. Now I feel confident that if I get a question on trie I can easily implement it.

  • @chelseaip759
    @chelseaip759 4 ปีที่แล้ว +1

    Thanks for being such an awesome source

  • @pramodsharma4508
    @pramodsharma4508 4 ปีที่แล้ว +1

    This guy never disappoints...

  • @harungumus6512
    @harungumus6512 3 ปีที่แล้ว

    Great explanation. Simple and plain.

  • @JLRide
    @JLRide 3 ปีที่แล้ว

    Thank you Michael. You made this easy to understand. Thank you.

  • @harpercfc_
    @harpercfc_ 2 ปีที่แล้ว

    Awesomeeeee! Thank you so much Michael

  • @ruthraramanan3407
    @ruthraramanan3407 3 ปีที่แล้ว

    Congrats bro!! You've actually made me understand smthg!

  • @fatihucar5842
    @fatihucar5842 2 ปีที่แล้ว

    very useful approach

  • @jaybhatt6775
    @jaybhatt6775 3 ปีที่แล้ว

    dude ur a fucking great teacher, do you know that? love you brother!

  • @soniahandle
    @soniahandle ปีที่แล้ว

    Great video!
    If we wanted to add a delete method, would we just getNode and then set isWord to false?

  • @ooflajboo
    @ooflajboo 3 ปีที่แล้ว

    earned yourself a subscriber. legit video

  • @richequan5115
    @richequan5115 2 ปีที่แล้ว

    very helpful, thanks Michael!

  • @apurvasharma7660
    @apurvasharma7660 4 ปีที่แล้ว +1

    You are doing a great job. You are maintaining the quality of the contents. Could you add some detailed videos on tries?

    • @AlgosWithMichael
      @AlgosWithMichael  4 ปีที่แล้ว

      Yea, I can definitely do more Trie videos. Thanks for watching!

  • @shahinkohzadpour
    @shahinkohzadpour ปีที่แล้ว

    it was nice ; thanks Michael !

  • @gautamtyagi8846
    @gautamtyagi8846 3 ปีที่แล้ว

    many many thanks bro! amazing explanation and easy to comprehend.

  • @vbmendes
    @vbmendes 3 ปีที่แล้ว

    Very good content. Just for the sake of contribution, I found out that the property 'c' in your node is actually not needed. You can surpress it without losing information in your structure. This information is already there in the children array of nodes.

  • @antonfernando8409
    @antonfernando8409 3 ปีที่แล้ว

    Nice, just 2 questions, do you really need that array of [26], i mean it seems like a waste of resource, b'c the Node itself as has 'c', so a ten letter word would have 10 nodes, no need to utilize an array[26]. Lastly, why not utilizes recursion at some point. Thanks, I am bit out of touch with tree structures and recursion, not since college have to implemented them, so would appreciate a broad answer, thanks.

  • @intcoder
    @intcoder 2 ปีที่แล้ว

    👏Great explanation and great coding!! Thanks.

  • @SaikiranDasika
    @SaikiranDasika 2 ปีที่แล้ว

    Great explanation!

  • @szymon200000
    @szymon200000 3 ปีที่แล้ว +1

    For the most part I get the video, but I can't seem to figure out why in the return statement in 'search(String word)' has to include 'node != null.' I got rid of it and it passed the 'Run Code' test but not the submission itself
    E: nvm. it's because 'curr' can point to a null spot in the array in the case that a prefix is not found in the trie. 'null' doesn't have any methods, but it doesn't give you an error because of short-circuiting. it sees null IS null and just spits out 'false' without checking 'null.isWord()'

  • @rickyc46
    @rickyc46 ปีที่แล้ว

    it's a great video! Very clear explanation. I was just curious why you used an array for children Nodes instead of a map? Is it just your preference?

  • @EinPiannist
    @EinPiannist 3 ปีที่แล้ว

    Hi! Thanks so much for this video. Could you please tell us what kind of keyboard you use lol

  • @sajankumarsingh7536
    @sajankumarsingh7536 3 ปีที่แล้ว

    Thank you bro for this video.

  • @thivyamathias3947
    @thivyamathias3947 ปีที่แล้ว

    Is there any specific reason to have Node as inner class??

  • @debrajmaji9608
    @debrajmaji9608 3 ปีที่แล้ว

    Subscribed you!! thanks for the great explanation.

  • @airbus5717
    @airbus5717 3 ปีที่แล้ว

    thanks for this super useful video.

  • @emreimamoglu6367
    @emreimamoglu6367 2 ปีที่แล้ว

    Thanks for this great video :)

  • @altayhunoglu1709
    @altayhunoglu1709 3 ปีที่แล้ว +2

    You have a new subscriber :)

  • @TroyKuang
    @TroyKuang 2 ปีที่แล้ว

    This is amazing, thank you!

  • @mr.morshedloo408
    @mr.morshedloo408 3 ปีที่แล้ว

    Hi could you help me about Multi-index Hybrid Trie for IP Lookup and Updates?

  • @hawaijarjs7496
    @hawaijarjs7496 4 ปีที่แล้ว +1

    Thanks Bro! You made my day 🤘

  • @sininsterme797
    @sininsterme797 3 ปีที่แล้ว

    This was very helpful. Thank you.

  • @frankdaniel3506
    @frankdaniel3506 3 ปีที่แล้ว

    Which software are you using for explanation?

  • @sunnilabeouf
    @sunnilabeouf 4 ปีที่แล้ว +1

    Instead of having an array of length 26 defined for each node, you could just use a set for each node and add it’s children using a character -> node key value pair

  • @jcanbb
    @jcanbb 2 ปีที่แล้ว

    Anyone able to provide some input on what (if any) the performance difference would be between creating looping over char c : word.toCharArray() versus looping over the string via an index variable and extracting the char at each iteration with char c = word.charAt()?

  • @lean.drocalil
    @lean.drocalil 2 ปีที่แล้ว

    One issue I noticed there is in case someone passes an ASCII entry above 'z', program is going to attempt to access an invalid index into its array. Either checking or making it wrap around with remainder of division by 26 is required to prevent that.

    • @adamandom
      @adamandom ปีที่แล้ว +1

      true, but this specific problem states that we can assume that all chars will be between a-z

  • @ameynaik1755
    @ameynaik1755 3 ปีที่แล้ว

    This is neat! Can you please do a video on Design Search Autocomplete System

  • @maherworld1
    @maherworld1 2 ปีที่แล้ว

    Hey Michael, i really liked your explanation, but i have a question regarding using array and intializing its length, does this affect space complexity since we take 26 spaces in memory every time we insert one character ? thats why using a hashmap is more efficient, though i really prefer this solution because its cleaner and easier to understand, thanks again

  • @architshinde3977
    @architshinde3977 3 ปีที่แล้ว

    Thanks a lot! This was really helpful!

  • @xxsanyxx1
    @xxsanyxx1 3 ปีที่แล้ว

    I have a question! I'm getting a index out of bounds error at line 36, if(curr.children[c - 'a'] == null) return null;
    Can someone explain why it might've happened?

    • @AlgosWithMichael
      @AlgosWithMichael  3 ปีที่แล้ว

      Is it possible your character 'c' is not a lowercase letter?

    • @xxsanyxx1
      @xxsanyxx1 3 ปีที่แล้ว

      @@AlgosWithMichael I've double-checked and it is a lowercase letter. Not too sure what's wrong with it! Also, thank you for the great explanation in the video! :)

  • @Kidpunk98
    @Kidpunk98 3 ปีที่แล้ว +4

    Fantastic video! thank you so much! You really are an amazing teacher. my only question though is why did you need a char member variable in your Node class. I wrote mine and noticed the data structure operates perfectly fine without that "char" member variable in the Node class.

  • @MrAbhitv
    @MrAbhitv 3 ปีที่แล้ว

    Hey Michael, i got a question. At line 15-16 (In insert method) i don't understand what happens if curr.children[c-'a'] is not equal to null. If we are inserting Strings, for example ,st1= "Apple" and st2 = "Add" how can A point to P and D at the same time. When we are inserting "ÄDD" Since curr.children[a-'a'] != null. It moves to Node with value P. A new node for D is not created.
    Thanks.

    • @AlgosWithMichael
      @AlgosWithMichael  3 ปีที่แล้ว +1

      The way characters point to other characters is each node has a `children` array of nodes representing all possible characters in the alphabet. If the index at `c - 'a'` evaluates to null, that means a new node needs to be created at that index. Hope that helps!

  • @lewisbrowne8799
    @lewisbrowne8799 3 ปีที่แล้ว

    nice one mike good vid.

  • @UCS_RheaRaviSharma
    @UCS_RheaRaviSharma 3 ปีที่แล้ว

    Amazing video!!!

  • @micknamens8659
    @micknamens8659 2 ปีที่แล้ว +1

    Optimization of space requirements:
    It seems that we don't need the instance variable 'char c'. And the boolean variable 'isWord' could be avoided if we have 2 classes: 'Node' with method 'isWord()' returning false, and a subclass 'EndNode' which overrides 'isWord' now returning true. Instead of setting the variable value, we replace the object (once).

  • @sachinbisht7417
    @sachinbisht7417 4 ปีที่แล้ว

    I got a question in like if we want to print the vertices by numbering the edges in order of insert ....how can we do this...
    for example 1 : input -
    1
    ABC
    output -
    0 -> 1 A
    1 ->2 B
    2 ->3 C
    for example 2: input
    3
    AT
    AG
    AC
    output -
    0 -> 1 A
    1 -> 4 C
    1 -> 3 G
    1 -> 2 T
    THANKS FOR HELPING.....

  • @sunebarnard9043
    @sunebarnard9043 3 ปีที่แล้ว

    Could someone maybe explain again why 'a' is subtracted from the char? Just a little confused there...

    • @AlgosWithMichael
      @AlgosWithMichael  3 ปีที่แล้ว +1

      It is because 'a' in ascii is 97 in its decimal value. Any lowercase letter minus 'a' will give you the index of the letter. So as an example, say I want to get the index of 'b'. 'b' has a decimal value of 98 (1 more than 'a'). So we subtract 'b' - 'a' and that gives us 1 as the index. 0 is index 'a', 1 is index 'b', etc.
      Hope this makes sense!

    • @sunebarnard9043
      @sunebarnard9043 3 ปีที่แล้ว

      @@AlgosWithMichael thanks that helps a lot 😊

  • @shankarregmi
    @shankarregmi 3 ปีที่แล้ว

    Great Video

  • @omarsharim6253
    @omarsharim6253 3 ปีที่แล้ว

    Why your search is O(1) if you have a search function with an iteration through N letters,
    think you are doing a linear search, so i think your search it's O(N), any thing im missing?

    • @AlgosWithMichael
      @AlgosWithMichael  3 ปีที่แล้ว

      Space complexity is O(1), time complexity is O(N)

    • @omarsharim6253
      @omarsharim6253 3 ปีที่แล้ว

      @@AlgosWithMichael Got it, ty

  • @riadas7721
    @riadas7721 2 ปีที่แล้ว

    Awesome. thanks!

  • @kumarGobika-xm3ur
    @kumarGobika-xm3ur 9 หลายเดือนก่อน

    Which language is this?

  • @OllytheOzzy
    @OllytheOzzy 3 ปีที่แล้ว

    Thanks mate

  • @ashishjaswal1437
    @ashishjaswal1437 2 ปีที่แล้ว +1

    //Search Insert and Delete are discussing Below
    public class Implementation{
    static class Node{
    public char c;//We are dealing with only small values;
    public boolean isWord;
    public Node[] childern;//Store all the childerns in this Node.
    Node(char c){
    this.c = c;
    isWord = false;
    childern = new Node[26];
    }
    }
    static class Trie{
    private Node root;
    public Trie(){
    root = new Node('\0');
    }
    public void insert(String word){
    Node curr = root;
    for(int i=0;i

  • @andriys5772
    @andriys5772 3 ปีที่แล้ว

    Thank you so much!

  • @aryan7069_
    @aryan7069_ 2 ปีที่แล้ว

    Amazing

  • @sudipkumardey8791
    @sudipkumardey8791 ปีที่แล้ว

    LMAO, The kids yeeeaaah voice 🤣🤣🤣

  • @LyricVista
    @LyricVista 4 ปีที่แล้ว

    from where i get he full code?

  • @dmstrat
    @dmstrat 3 ปีที่แล้ว

    Just came across this ... good work and nice graphical instruction.
    I do have a concern over the "StartsWith(prefix)" method as it does not work as I would expect.
    In your implementation, If you have the word 'cat' in the trie but not any other word beyond that, like 'cats' or 'catastrophe' your StartsWith('cat') would return true even though you have no more words beyond that point. Therefore, a subsequent Search('cats') or any Search/StartsWith('cat' + any character(s)) would return false.
    I would expect the StartsWith to check to be sure there are nodes in the children array to indicate a longer word is available in your trie.
    Example: 'cat', 'cats', and 'catastrophe' were inserted to the Trie and performing a StartsWith('cat') would return true because there are more nodes below 't'
    Example 2: 'cat' was inserted to the Trie and performing a StartsWith('cat') would return FALSE because there are no children nodes below 't'
    What are your thoughts?

  • @ivaylopankov7369
    @ivaylopankov7369 4 ปีที่แล้ว

    Great video! For the purposes of this question though, the information about the exact character inside a node is not needed.

  • @christowndotcom
    @christowndotcom 3 ปีที่แล้ว

    Ah, since the children have an array of the alphabet anyways, the actual c element of the Node class is not needed. Just FYI. Thanks!

    • @AlgosWithMichael
      @AlgosWithMichael  3 ปีที่แล้ว

      Yea, that is a good point! Thanks for watching

  • @nishantparmar
    @nishantparmar 2 ปีที่แล้ว

    Great and very simplified...
    But small correction in insert method
    public void insert(String word) {
    Node current = root;
    for (int i = 0; i < word.length(); i++) {
    char c = word.charAt(i);
    if (current.children[c - 'a'] == null) {
    current.children[c - 'a'] = new TrieNode(c);
    current = current.children[ch - 'a'];
    }else if(current.children[ch - 'a'].c == c){
    current = current.children[c - 'a'];
    }
    }
    current.isWord = true;
    }

  • @jamesblack2719
    @jamesblack2719 4 ปีที่แล้ว +1

    Why is Node a class instead of a struct? You will have many nodes and a class requires more memory than a struct. You are also using "c - 'a' " in a loop. Put that into a variable and do the calculation once. Why pay for the calculation over and over. You didn't use the term ASCII, which is surprising, as it may help viewers to understand that this is the case. Why not make this recursive, rather than using so many for loops. At the end a trie is just a modification of a binary tree, but it can have 26 children. Nice video though.

    • @AlgosWithMichael
      @AlgosWithMichael  4 ปีที่แล้ว +1

      Yea, I could have put c - 'a' in a variable. I think using loops is easier to understand than recursive approaches, especially if I am trying to explain a concept that people don't know already. Thanks for the feedback!

    • @jamesblack2719
      @jamesblack2719 4 ปีที่แล้ว +2

      @@AlgosWithMichael Thanks for the reply. If you just mentioned that there are other options that would be enough. My concern is people will look at your video, then just copy it and not realize that it not the best choice, as this was written to teach, not for production. This happens when people put snippets of code on StackOverflow also.

  • @ooorayooonyoooo
    @ooorayooonyoooo 3 ปีที่แล้ว

    شايفكم ي عيال كفوبم وانتم تفشخون الحل هههه