Binary Search Tree - Recursive Search and Insert

  Рет қаралды 12,191

Blue Tree Code

Blue Tree Code

Күн бұрын

Пікірлер: 24
@wengeance8962
@wengeance8962 4 жыл бұрын
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 :)
@ryanciminski4695
@ryanciminski4695 3 жыл бұрын
I cannot believe this does not have more views. By far the most helpful video that I have found on KZbin.
@BlueTreeCode
@BlueTreeCode 3 жыл бұрын
Thanks a lot, Ryan!
@cutestyleplay
@cutestyleplay 4 жыл бұрын
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! :)
@namnguyenvan9601
@namnguyenvan9601 2 жыл бұрын
thanks bro. love from VN
@BlueTreeCode
@BlueTreeCode 8 ай бұрын
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 8 ай бұрын
Thank you! Please don't forget to share / recommend the channel to others / on social media to help it grow.
@themonster4759
@themonster4759 Жыл бұрын
very helpful thnx
@emenikeanigbogu9368
@emenikeanigbogu9368 2 жыл бұрын
Amazing
@BlueTreeCode
@BlueTreeCode 2 жыл бұрын
Thank you, Emenike!
@alex0917lfo
@alex0917lfo 4 жыл бұрын
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 жыл бұрын
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 :)
@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! :)
@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 жыл бұрын
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. :)
@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 жыл бұрын
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.
Binary Tree Traversal - Preorder, Inorder, Postorder
19:38
Blue Tree Code
Рет қаралды 2,5 М.
風船をキャッチしろ!🎈 Balloon catch Challenges
00:57
はじめしゃちょー(hajime)
Рет қаралды 92 МЛН
Turn Off the Vacum And Sit Back and Laugh 🤣
00:34
SKITSFUL
Рет қаралды 2,8 МЛН
Binary Search Tree - Recursive Min and Max
4:51
Blue Tree Code
Рет қаралды 4,7 М.
Binary Search Tree - Recursive Inorder Predecessor
10:46
Blue Tree Code
Рет қаралды 2,8 М.
AVL Tree - Insertion (Height Augmented)
21:59
Blue Tree Code
Рет қаралды 1,7 М.
Trees   6 recursive add
13:36
RobEdwards
Рет қаралды 10 М.
Learn Binary search trees in 20 minutes 🔍
20:25
Bro Code
Рет қаралды 182 М.
5 Simple Steps for Solving Any Recursive Problem
21:03
Reducible
Рет қаралды 1,2 МЛН
Finding the Maximum Depth of a Binary Tree (Recursion)
7:26
Stephen O'Neill
Рет қаралды 43 М.
Lecture 5: Binary Search Trees, BST Sort
52:40
MIT OpenCourseWare
Рет қаралды 615 М.