Binary Search Tree in Java - 2: Delete a node of binary search tree

Поделиться
HTML-код
  • Опубликовано: 6 ноя 2024

Комментарии • 87

  • @CodingSimplified
    @CodingSimplified  5 лет назад +22

    At [12.35]: It'll be 'node.right = delete(node.right, successor.data)'; rather than 'node.right = delete(node.right, 4)';

    • @shaktikc606
      @shaktikc606 4 года назад

      thanks for video. And in above there should not be node.right = (....) but only delete(node.right , successor.data);

    • @HC-qe8ii
      @HC-qe8ii 3 года назад

      exactly man, I was confusing, didn't think of checking comment first xd

  • @saurabhc4278
    @saurabhc4278 3 года назад +1

    You just simplified the concept of bst deletion !!! Others just glorify the deletion as a complex 3 case problem and still keep you scared and confused . Yours give a life time intuition that stay for long time . I am wondering like why this problem didn't felt so simple before this video . Thank you !!

    • @CodingSimplified
      @CodingSimplified  3 года назад

      Thanks for your nice feedback. Glad you liked it. Keep watching.

  • @raitsu11
    @raitsu11 5 лет назад +13

    getSuccessor-methods while loop should check if temp.left is null instead of checking temp, that is: while(temp.left != null) instead of while(temp != null), otherwise there will be a nullPointerException when trying to delete a node that has two children.
    Otherwise, very helpful. Thankyou!

    • @CodingSimplified
      @CodingSimplified  4 года назад

      That's right. Thanks for mentioning. Updated this at thecodingsimplified.com/delete-a-node-of-binary-search-tree/

  • @sanketpatil2154
    @sanketpatil2154 Год назад

    Your teaching style is amazing. I've learned a lot about tree data structures from your videos. Thank you for your time and effort. 🙂👌

  • @flexerfadrigalan
    @flexerfadrigalan 3 года назад

    You're the man brother, after a few videos and an overwhelmed evening of relearning BSTs, makes SO much more sense.

  • @munazzainamulhaque8863
    @munazzainamulhaque8863 4 года назад +2

    Most precise level of teaching ...really amazing...because of your tutorial got to learn properly about Binary Search Tree..thanks!

  • @shivpatel8288
    @shivpatel8288 3 года назад +1

    I am really grateful to you. You saved me from a terrible exam. Virtual hugs!

  • @amazinglife5400
    @amazinglife5400 4 года назад +1

    Super great video!!! Finally able to learn deletion in bst after so much stress for not able to find reliable source 😇.
    Keep it up Sir!!!!

  • @CodingSimplified
    @CodingSimplified  5 лет назад

    Please find the source code at: thecodingsimplified.com/delete-a-node-of-binary-search-tree/

  • @asmhanalnazly2369
    @asmhanalnazly2369 2 года назад

    thank you so much you are A great teacher this lesson helps me a lot

  • @rishabhsingh3193
    @rishabhsingh3193 4 года назад +4

    in the else part where you called delete function howcome your are sending 4 as a key value shouldnt be there .data ?

  • @ShreyanGoswami
    @ShreyanGoswami 2 года назад +1

    Works for most cases but fails when the key to be deleted is in the root node.

  • @tekken1935
    @tekken1935 4 года назад +2

    at 7:20 why is it return node instead of return temp

  • @Techjai000
    @Techjai000 2 года назад

    in the lines 47 to 51 whats thew pont of using if else as you r returning temp in either way

  • @chaitanya3992
    @chaitanya3992 3 года назад

    instead of 4 use successor .data it works

    • @CodingSimplified
      @CodingSimplified  3 года назад

      That's right. We've pinned this in comment as well. Thanks.

  • @meditationandrelaxationmus741
    @meditationandrelaxationmus741 5 лет назад +2

    The video was very clear and nice I learnt a lot from your videos..Thanks

  • @vidyakamath8298
    @vidyakamath8298 6 лет назад +3

    Nice explanation,Debugging helps more to understand .thank you.

    • @CodingSimplified
      @CodingSimplified  6 лет назад +1

      +vidya kamath Thanks. Keep Watching more tutorials.

  • @yichen887
    @yichen887 2 года назад +1

    This video is super helpful! Thank you!

  • @bavarvaneel5
    @bavarvaneel5 3 года назад +1

    Good and clear explanation.

  • @surajgrandhi6742
    @surajgrandhi6742 4 года назад

    Amazing explanation
    please tell how to check if a tree is bst

    • @CodingSimplified
      @CodingSimplified  4 года назад

      Please check following. We've already covered this : ruclips.net/video/L2b3KQWIq_0/видео.html

  • @sathiyakala5946
    @sathiyakala5946 3 года назад

    Can we able to delete the root node using this code sir.

  • @ikaros9727
    @ikaros9727 4 года назад +1

    Hey. Nice Video. Could you please explain to me why you write node.right = delete(node.right,4) ?
    I dont really understand why you save the result in node.right. Also isnt it kind of bad to write bare number in code like the 4? Shouldnt that be successor.data? Or is it possible that the value of successor changes when i call the delete function again?
    Best regards

    • @CodingSimplified
      @CodingSimplified  4 года назад +1

      Hi, I agree with you. This was mistake done at my end. I've also pinned down it in my comment about it. You're right. It should be successor.data. when I realized this, video was already uploaded so could not change it.

  • @mohammedanees8561
    @mohammedanees8561 Год назад

    great work sir thank you for this😍😍

  • @AjaySingh-xy9zq
    @AjaySingh-xy9zq 3 года назад +1

    thanks sir you are amazing

  • @anuragsoni2256
    @anuragsoni2256 3 года назад +1

    Thankyou so much for guidance!

  • @shouvikdutta2825
    @shouvikdutta2825 5 лет назад +2

    why you debug instead of run the code.

    • @CodingSimplified
      @CodingSimplified  5 лет назад

      Debugging helps you in understanding each line of code & data which is being processed. That's main reason for explaining this way. It helps even if you're beginners. But if you're good in it, please skip debugging & run the code :)

  • @abdulrehmaan153
    @abdulrehmaan153 2 года назад

    The debugging helps,....
    Verbal explanation needs repair

  • @naman2080
    @naman2080 4 года назад

    I am implementing delete method in void and I am having a problem to delete node can you please check that.

    • @CodingSimplified
      @CodingSimplified  4 года назад

      Hi, could you please paste your code & mention at which step, you're not getting correct results or facing issue.
      - Without checking code, it's difficult to check where you're facing the issue. Thanks.

    • @naman2080
      @naman2080 4 года назад

      @@CodingSimplified 1 import java.util.*;
      class Node
      { int data;
      Node left;
      Node right;
      public Node()
      { data=0;
      left=null;
      right=null;
      }
      public int getData()
      {
      return(data);
      }
      public Node getRight()
      { return(right); }
      public Node getLeft()
      { return(left);
      }
      public void setData(int d)
      { data=d; }
      public void setRight(Node n)
      { right=n; }
      public void setLeft(Node n)
      {
      left=n;
      }
      }
      class BinarySearchTree
      {
      Node root;
      public BinarySearchTree()
      {
      root=null;
      }
      public boolean isempty()
      {
      if(root==null)
      return true;
      else
      return false;
      }
      public void insert(int data)
      {
      if(root==null)
      {
      Node n=new Node();
      n.setData(data);
      root=n;
      }
      else{
      insert(root,data);
      }
      }
      public void insert(Node s,int value )
      {
      Node n=new Node();
      if(values.getData())
      {
      search(s.getRight(),value);
      }
      else
      {
      search(s.getLeft(),value);
      }
      }
      public void deleteNode(int data)
      {
      deleteNode(root,data);
      }
      public void deleteNode(Node root,int value)
      {
      if(root==null)
      {
      System.out.println("Data not present to delete");
      }
      else if(value>root.getData())
      {
      //root=root.getRight();
      deleteNode(root.getRight(),value);
      }
      else if(value

    • @naman2080
      @naman2080 4 года назад

      In this deleteNode function not able to delete any value

  • @surajankita1
    @surajankita1 5 лет назад +1

    what is the need of if(node.left==null || node.right==null)?.can i write if(node.left==null && node.right==null){ return null; }

    • @CodingSimplified
      @CodingSimplified  5 лет назад +1

      Sorry for responding late. This is needed as we're solving two cases in this If
      - One when node is leaf node (node.left == null && node.right)
      - Second, when node has single child (either left or right)
      (node.left==null || node.right==null) will serve both purpose.

  • @vikaskumarjha1010
    @vikaskumarjha1010 4 года назад

    Hindi explanation of same problem - ruclips.net/video/XPGU8oFpJVA/видео.html

  • @amitnain8489
    @amitnain8489 4 года назад +1

    Thanks bro ..

  • @charisbaafi8450
    @charisbaafi8450 6 лет назад

    I wonder why you put (4). What if you don't know the leftmost node of the right node. Kindly explain

    • @shariqshoaib3207
      @shariqshoaib3207 5 лет назад +2

      I would send successor.data

    • @hikikomori9387
      @hikikomori9387 5 лет назад

      @@shariqshoaib3207 dude...thanks...tons of respect

    • @CodingSimplified
      @CodingSimplified  5 лет назад +1

      That's right. I was checking this video again & found out this. Silly Mistake. It should be successor.data. Not sure how to edit on live video.

  • @vinaykr1991
    @vinaykr1991 6 лет назад

    Have you done any videos on balancing a tree?

    • @CodingSimplified
      @CodingSimplified  6 лет назад +1

      Not yet, but we'll create soon on AVL balancing & Red-Black Tree. Thanks

    • @abdelrahman2348
      @abdelrahman2348 6 лет назад

      I hope you make it soon please we need it

  • @umaparhar3178
    @umaparhar3178 5 лет назад +1

    king!!!!!!!!!!!!!!!

  • @jtmaniac1
    @jtmaniac1 6 лет назад +1

    great video, make more.

  • @lichaytiram9246
    @lichaytiram9246 5 лет назад

    why too hard?
    public Node deleteNode(Node node, int value) {
    if (node == null)
    return null;
    if (node.getValue() == value)
    return null;
    if (value < node.getValue())
    node.setLeft(deleteNode(node.getLeft(), value));
    else
    node.setRight(deleteNode(node.getRight(), value));
    return node;
    }

    • @Muradlek
      @Muradlek 5 лет назад

      cause if you delete top of the tree, you will lose the top ,and it'll be 2 trees. lol...

  • @akshun9517
    @akshun9517 4 года назад +1

    too complicated

    • @CodingSimplified
      @CodingSimplified  4 года назад

      Hi, the problem is definitely tough. BTW at what point you're stuck. We can try to help you.

  • @coderbuddy3875
    @coderbuddy3875 4 года назад +1

    I didn't understand ☹️

    • @CodingSimplified
      @CodingSimplified  4 года назад +1

      Hi Adarsh, could you please explain what you're not understanding so that we can help you.

    • @coderbuddy3875
      @coderbuddy3875 4 года назад +1

      @@CodingSimplified i guess i need to watch it again then i will get it .... actually I'm beginner and yesterday i wasn't able to understand anything but today i am ...

    • @CodingSimplified
      @CodingSimplified  4 года назад +1

      @@coderbuddy3875 That's nice.

  • @WaqasAli-po1wh
    @WaqasAli-po1wh 5 лет назад

    Great

  • @aryanbisht7752
    @aryanbisht7752 4 года назад +1

    I think this is wtong

    • @CodingSimplified
      @CodingSimplified  4 года назад

      Hi Aryan, could you please elaborate what's wrong so that we can update accordingly. Thanks.

    • @aryanbisht7752
      @aryanbisht7752 4 года назад +1

      Node.right=delete(node.right,4);
      It should be :
      delete(node.right,successor.data);

    • @CodingSimplified
      @CodingSimplified  4 года назад

      @@aryanbisht7752 : I agree with you. This was mistake done at my end. I've also pinned down it in my comment about it, which is the first comment. You're right. It should be successor.data. when I realized this, video was already uploaded so could not change it.

  • @robertohernando2605
    @robertohernando2605 4 года назад

    NO ME LA CONTEEEEEEEEESPHITE

  • @qurbanivideos7509
    @qurbanivideos7509 6 лет назад

    Can u provide me the source code?

    • @CodingSimplified
      @CodingSimplified  4 года назад

      Hi, I know it's too late, but if you want, we've launched site & for this problem, source code is present at : thecodingsimplified.com/delete-a-node-of-binary-search-tree/

  • @dineshkumarmehra3133
    @dineshkumarmehra3133 4 года назад

    the tree example that you have taken in the video is not correct,
    the values in left subtrees should be less than the root and values in right subtrees should be bigger than root value

    • @CodingSimplified
      @CodingSimplified  4 года назад

      Hi, where do you see issue. Looks like example is correct, where all value in left-Subtree are less than root & all values in right subtree are greater than root.
      - Could you please elaborate the point, where it's not correct so that I can check it further.