Binary Search Tree - Recursive Search and Insert

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

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

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

    I cannot believe this does not have more views. By far the most helpful video that I have found on TH-cam.

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

    this is the most amazing elegant solution. for whatever reason, this explanation made clear sense in my head. I love how the insert function only takes in 1 argument. I saw too many different variations of peoples online with recursive insert calls taking in so many parameters. I was convinced it could only be done with 1 argument but just didnt know how until i saw this

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

      Thanks for the kind comment, William! Glad it helped :)

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

    You deserve million subscribers! Your explanation is crystal clear! The best on youtube

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

      Thank you for your kind comment, Sarah Star!

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

    Awesome video my guy! People like you and videos like this give me hope in myself, and in the world haha.

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

      I'm glad it helped, Ian! :)

  • @BlueTreeCode
    @BlueTreeCode  5 ปีที่แล้ว

    Hey Everyone! Thanks for checking out my video! Don't forget to Like, Subscribe, and MOST IMPORTANTLY click that Notification Bell for updates! :)

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

    thanks bro. love from VN

    • @BlueTreeCode
      @BlueTreeCode  9 หลายเดือนก่อน

      You're very welcome!! Thank you for watching. Please don't forget to share / recommend the channel on social media to help it grow.

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

    Brilliant explanation

    • @BlueTreeCode
      @BlueTreeCode  9 หลายเดือนก่อน +1

      Thank you! Please don't forget to share / recommend the channel to others / on social media to help it grow.

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

    Is that possible to write the Recursive Search and insert node without that many if-else statements? (Don't get me wrong. if-else is super human-friendly, but it will hard to fix a bug in reality program.)

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

      Hi Alex Yip,
      In the previous comment I went over some reasons why this would be an issue by just altering code inside the method.
      Going over the search method for example:
      If you really want to remove it, then you'd have to alter the parameters (NB: This will defeat the purpose of only using data as a parameter).
      Also, please note again, you'll end up doing ( root.search(root, 40) ) in the main method for example.
      By altering the parameters you can do something like this.
      public TreeNode search (TreeNode root, int data){
      if(root == null || data == root.data){
      return root;
      }
      else if (data < root.data){
      return search(root.left, data);
      }
      else{
      return search(root.right, data);
      }
      }
      NB: You can also wrap the method in another method. Again you'll be altering the method arguments inside the class, but you avoid passing in root as an argument in the Main method, which is nicer.
      example:
      public TreeNode search(int data) {
      TreeNode root = this;
      return searchWithRoot(root, data); //searchWithRoot is simply the above code renamed for simplicity.
      }
      Hope this helps :)

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

    very helpful thnx

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

    When you initially called this method, you would write search(50). Shortly into the recursion, the recursion will call left.search(40). How does the method remember 50? It seems when 40 is passed into the search parameter the method wouldn’t remember 50. Basically I’m confused by line 2

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

      Hi Gabriella, 40 isn't passed in as the argument. Every time we make a recursive call, 50 is passed in. So it'll be left.search(50), right.search(50), left.search(50). This 50 comes from the original 50 that was passed in when the method was first called. Hope this helps. :)

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

    Amazing

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

    What if root would be equal to null..?

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

      Hi Heena, a really cool feature about this particular Binary Tree class is that it's defined in a way that when an instance is created, that instance will be the root. Now, since these methods are instance methods, you would need that instance to use them.
      For example, I can't say: search(5).
      I would have to say TreeNode root = new TreeNode(5); and then use root.search(5) or root.insert(6).
      Hope this helps :)

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

    Why are you checking if children are null? The recursive call will take care of that. The one will check if the root, or all children are null or not. Recursively

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

      Hi, [Seryoga],
      Suppose there was only one node (80) in the tree and we decided to search for (40).
      By calling left.search(40) without that null check, we'll be calling null.search(40), since 80's left is null.
      Similarly, by calling right.search(100) without the null check, we would be calling null.search(100) since 80's right is null.
      Now, I'm assuming you're saying that there should only be one null check to handle everything.
      So, something like this:
      if(this == null){
      return null;
      }else if(data == this.data){
      return this;
      }
      ...* rest of code without the left and right null checks.*
      Here's why this wouldn't work. Suppose again we had only one node with value 80, then the base cases (Code above) will first check if 80 is null, and well 80 is not null so we're good, so we then check if 80 == 40, well 80 is != 40, so that's still good, because we can just check left.
      Now, here's where things get interesting.
      When we say left.search(40), because we excluded the (left != null) check, we'll we end up calling null.search(). So we're back to square one.
      Hope this helps :)
      P.S. (I'm assuming you meant to change code inside the method, not the signature).
      Now, if that's not what you meant, then check my response to Alex Yip. (NB: this involves altering the method arguments) Hope this helps.