Java Binary Search Tree 2

Get the Code Here:

Welcome to my 2nd video on Binary Trees in Java. If you haven’t seen part 1, definitely watch it first or this will be confusing binary tree in Java.

In this part of the tutorial, I will take you step-by-step through the process of deleting nodes in a binary tree. This topic seems to be confusing to many people. I personally prefer to build trees with the builder design pattern like I showed here Encapsulate Composite with Builder, but it is also important to understand the basics of the binary tree.






26 responses to “Java Binary Search Tree 2”

  1. yoseph gebrehiwet Avatar

    That was intense. but i loved it.

  2. Ed S Avatar

    Why would academics not like refactoring?

  3. Polarpwnage Avatar

    i noticed in the beginning you didnt address the issue of when deleting a node that has 2 children which are both subtrees

  4. Sabrina Markon Avatar

    I've often needed to program trees for affiliate programs, and found deletions more complicated, because if you are talking about human referrals, it is not fair that a right child of a deleted node has potential to be elevated and thus receive the deleted node's referrals, while the left side does not. I'm not sure if I might be missing something though because I"m currently studying this in my Data Structures class. Another question I had was say 75 ONLY has a left child. If 75 is deleted, we would simply replace it with the left child, correct? Sorry for so many questions I'm very interested in this sort of subject hahah

    Oh and major thanks Derek Banas!!!!..throughout my studies your videos have been like a safety net – if I don't understand something confusing you have a brilliant way of explaining even the most complicated subjects so they sound obvious. Thank you so much. I think a lot of students at my school watch and we recommend you often in our forums.

  5. Andrew Martin Avatar

    Hey, Derek, thanks for the great vids! One comment: I think your code would be more compact if you slightly modified the findNode method to keep track of isItALeftChild and reuse that method within the remove method

  6. Yin YuPeng Avatar

    I have a question: At 9:38, What does it mean for:

    (L252)Replacement.leftChild = focusNode.leftChild;
    What is this for, could someone explain?Thanks! Why is it leftChild?

  7. Heath Baron-Morgan Avatar

    When deleting the root, I loose 3 nodes..

  8. Ivan Shlomin Avatar

    Change your code or send how to fix it. It doesn't work and i don't know where is mistake.Thx

  9. Ivan Shlomin Avatar

    Can you change it? Thx

  10. Ivan Shlomin Avatar

    if you try to delete a parent with one or two kids you will loose parent and kids…there is a mistake in the code

  11. Ivan Shlomin Avatar

    Sorry for Russian comments in the code.

  12. Ivan Shlomin Avatar

    Please check. Maybe I did mistake. Thx

  13. 沈子语 Avatar

    BU CAS CS112…..

  14. Darkbdlz Avatar

    why can i just use a loop instead of recursion it is so much easier

  15. Sudiptaa Avatar

    Great style of teaching . Thank you 🙂

  16. Abhishek B Avatar

    Love your videos Derek.
    One error:

    Line 235 should be:

    parent.rightChild = focusNode.rightChild;

  17. Seventy-Three David Avatar

    Derek, do you have any videos regarding Breadth First search? Always love watching your tutorials.

  18. XliXliDiS Avatar

    Hello Derek. I am an IT student and i would like to thank you for your tutorials. Really helpful!. I have a question. What if the node with the value 30 has only a left child and we would like to delete 30. What will happen then? Thank you!

  19. Jeremiah Tolentino Avatar

    Derek you just got yourself a new subscriber this will get me through cs. You're a lifesaver!! keep up the videos 🙂

  20. Sam Tux Avatar

    Hi, Derek, please update the case when you delete the root and 75 has a left child. It seems like the leftChild of 50 should slide all the way down to the leftmost child of right child of root, right?

  21. alesquish Avatar

    if (replacement != replacedNode.rightChild) {
    replacementParent.leftChild = replacement.rightChild;
    replacement.rightChild = replacedNode.rightChild;
    even do anything?

  22. Janardhan Reddy Avatar

    at 8:15 minutes, under if(focusNode.leftChild == null) in else condition (last check), parent.rightChild = focusNode.leftChild.

    focusNode.leftChild is already NULL, setting to null would remove link to right sub tree. it should be parent.rightChild = focusNode.rightChild.

  23. Edward Smit Avatar

    Hi Derek, on top of making a video for AVL trees, are you planning on making a video for splay trees as well?

  24. Robert Norton Avatar

    Derek, your videos are absolutely superb! They help so much with understanding the fundamentals of different java tools and techniques. These videos and sample code have aided me greatly, thank you.

  25. Ajesh Mahto Avatar

    Hi Derek, Can you also make a tutorial on how to find the common ancestor between two binary tree? This question was asked to me in my interview and I wasn't able to solve. Appreciate if you could do one video on this.

  26. Jonathan Blik Avatar

    Derek. first and foremost thank you for your videos.
    would you please explain little deeper the last part with

    replacementParent.leftChild = replacement.rightChild;
    replacement.rightChild = replacedNode.rightChild; ?

Leave a Reply

Your email address will not be published. Required fields are marked *