Тёмный

Inorder Successor in a binary search tree 

mycodeschool
Подписаться 762 тыс.
Просмотров 360 тыс.
50% 1

See complete series on data structures here:
• Data structures
In this lesson, we have discussed efficient algorithm to find Inorder successor of a Node in binary search tree.
See source code here:
gist.github.co...
See video on Inorder Traversal here:
• Binary tree traversal:...
For practice problems and more, visit: www.mycodeschoo...
Like us on Facebook: / mycodeschool
Follow us on twitter: / mycodeschool

Опубликовано:

 

8 сен 2024

Поделиться:

Ссылка:

Скачать:

Готовим ссылку...

Добавить в:

Мой плейлист
Посмотреть позже
Комментарии : 169   
@DhirendraSingh03
@DhirendraSingh03 5 лет назад
This entire playlist is a treasure. Downloaded all the videos and created multiple copies in my PC, my external hard drive and google drive. Cannot risk it losing in case of apocalypse. And whoever you are, a big thank you. You are a GOAT.
@robinsoni4778
@robinsoni4778 5 лет назад
No, he is not a GOAT, He is a human
@Slim7073
@Slim7073 4 года назад
@@robinsoni4778 He capitalized GOAT. It stand for Greatest Of All Time.
@ApoorvaRajBhadani
@ApoorvaRajBhadani 4 года назад
kuch zyada ho gya...............tareef karne ka tarika tera mujhe kuch khas acha nhi laga 😂
@konstantinrebrov675
@konstantinrebrov675 4 года назад
@@Slim7073 That is ambiguous abbreviation. There could be 100 things it stand for. And "goat" sounds like a bad word.
@danilo2735
@danilo2735 4 года назад
@@konstantinrebrov675 it's a word used in many places, for example messi is the football goat, jordan the basket goat etc...
@llarn.i5103
@llarn.i5103 3 месяца назад
It’s been 10 YEARS since this playlist and people are still watching it, I don’t usually comment but Really Thank you for this masterpiece
@deeepbagadiya
@deeepbagadiya 8 лет назад
After 2 Years...THis videos Helped the same.....i am sure till many years this videos will keep helping Students
@random-0
@random-0 5 лет назад
yes it's still rocking he left us a treasure
@manishpandey3803
@manishpandey3803 4 года назад
yes today's date is 30/05/2020
@sudhanshu9831
@sudhanshu9831 3 года назад
2021 and still the easiest and understandable explanation of ds on yt
@AmanKUMAR-vq1ls
@AmanKUMAR-vq1ls 3 года назад
I am here on 20 july 2021
@jatinvashisht4293
@jatinvashisht4293 2 года назад
😂 This aged very well
@user-zd6rj9vt6c
@user-zd6rj9vt6c 4 месяца назад
finally i find someone who explain all things with details thank you so much
@darshanbhat9457
@darshanbhat9457 9 лет назад
I am a beginner in DS. These videos helped me a lot to understand quickly. Thanks a lot. Please don't stop uploading. Keep going.
@abhilashnair6660
@abhilashnair6660 6 лет назад
He stopped lol
@monikajha3500
@monikajha3500 6 лет назад
He is no more in this cruel world :(
@saeem01670
@saeem01670 5 лет назад
@@monikajha3500 what do you mean by that? :|
@noobcoder5988
@noobcoder5988 5 лет назад
@@monikajha3500 No, he is alive.
@random-0
@random-0 5 лет назад
@@noobcoder5988 sorry but sadly he is dead search for it
@narayankaadda772
@narayankaadda772 7 лет назад
Hi Sir, I have followed all your BST tutorials. It was very great and clear explanation. Initially I was afraid of learning BST. Now I can say I have learned BST confidently from your explanation. Thank you very much your contribution to helping programmers.
@shelbygt5004
@shelbygt5004 10 лет назад
mycodeschool Your tutorials are BRILLIANT! Please do continue the series as soon as possible. Would like to see more on Trees, Graphs, Maps, Hash-tables etc.
@Leon-hg1ph
@Leon-hg1ph 4 года назад
Yooo, this whole concept of BST is freaking amazing, almost magical. Thanks a lot for sharing this mate!
@1gouravgg
@1gouravgg 10 лет назад
finally completed the whole DS series :)
@kausshin5404
@kausshin5404 4 года назад
And got placed at google
@prajwalchoudhary4824
@prajwalchoudhary4824 4 года назад
@@kausshin5404 lol
@HARIHaran-ks7wp
@HARIHaran-ks7wp 3 года назад
@@kausshin5404 wait, really??
@manishkumargupta8677
@manishkumargupta8677 2 года назад
Congratulations
@prudvi01
@prudvi01 3 года назад
I'm watching this series after my engineering, these data structures make much more sense now thank you so much
@yuiooiuyyuiooiuyyuio
@yuiooiuyyuiooiuyyuio 8 лет назад
Hi I'm Kapil, These codes for successor and predecessor seem to work fine. They keep on storing the pointer to nodes whose value is greater than (or less than) the given integer in the path of search for the integer and the latest updated value is our answer. And also when it finds the integer, it doesn't stop, it goes on to find the next greater value (or smaller) by traversing further down the tree. It's a tiny code :) TreeNode* getSuccessor(TreeNode* A, int B) { TreeNode *P=NULL; while(A!=NULL) { if(A->valright; } else if(A->val>B) { P=A; A=A->left; } } return P; TreeNode* getPredecessor(TreeNode* A, int B) { TreeNode *P=NULL; while(A!=NULL) { if(A->valright; } else if(A->val>=B) { A=A->left; } } return P;
@panavsethi3014
@panavsethi3014 3 года назад
It actually works fine, the time complexity is also O(h). I wonder why he used such a complex algorithm.
@scotfsh5014
@scotfsh5014 2 года назад
I'm in tear watching your video sir, the way you spreading knowledge is on god tier, I coundn't been more satisfied with my life without learning from you.Much Love!
@__hannibaal__
@__hannibaal__ Год назад
Yeah i stop C++ 25 years ago, and i busy with mathematic, until last year, and per chance i step on these videos on C++ and data structure, and he gave me another breath and i start learn what i lost before, very effective __my back ground mathematic help me__ and now i just i have 80% of C++, and data structure 60%, and this second year is planed to make all data structure __special a complex tree with modern C++__ from scratch and both .
@Drogon-Gaming
@Drogon-Gaming 3 года назад
this play list is absolute bonus for every programmer who wants to get good at data structure , Many thanks to CS DOJO for suggesting this !!!!!
@darshpreetsingh2010
@darshpreetsingh2010 5 лет назад
All videos are great,most important thing is that they are all very interactive and you have worked very hard in making them and teaching us. Thankyou
@sarveshsant1337
@sarveshsant1337 6 лет назад
In Getsuccessor function instead of: "if(current->data < ancestor->data)" we must use :if(current->data data)" To ensure the data less or equal will be found in left subtree. Thanks for sharing your knowledge.
@hughlee5648
@hughlee5648 2 года назад
i think you won't have to check equal condition since in BST, duplicate is not allowed anyways.
@iamparitosh
@iamparitosh 3 года назад
Completed all the 13 Videos on BST I feel like a BST Master and able to complete most Leetcode problems on BST:)
@AnonymousCoder7
@AnonymousCoder7 Год назад
I'm gonna hit Like 👍🏻to every video of this channel coz It Deserves !!
@sarveshp1727
@sarveshp1727 2 года назад
This is THE PERFECT explanation of this concept.
@chayakumarsedutainment4799
@chayakumarsedutainment4799 4 года назад
You haven't handled the case when current node is leaf node. Yet, still it is an excellent tutorial for DS and I will recommend this to other. I have learnt a lot from you. Thank you so much..
@ganapatibiswas5858
@ganapatibiswas5858 4 года назад
It will fall into the current -> right==null case
@shubhammohapatra7766
@shubhammohapatra7766 3 года назад
Really, great stuff man. You have helped a lot of people. You should be really proud of yourself.
@parthkhanna8542
@parthkhanna8542 4 года назад
Man...U are an inspiration for me.. You are just great.. No words can express your work.. Thanx for helping
@sumitsaini1601
@sumitsaini1601 4 года назад
getSucessor in case II is really insightful and elegant. That sums up this video.
@prashantbacchewar8194
@prashantbacchewar8194 9 лет назад
Your videos are "the" best Any plan to cover self balancing trees like Red Black or AVL trees
@codingandmathvideos
@codingandmathvideos 10 лет назад
Impressive. Thanks a million times. You are simply the best.
@harinijeyaraman8789
@harinijeyaraman8789 4 года назад
Kudos for your efforts in making concepts seem this easy ! Love your channel. Really helpful !!
@nitinjaiman
@nitinjaiman 10 лет назад
can you make videos on red black trees, greedy algo, kruskal algo and other topics that are in cormen. i went through mit videos i must say you are better than those fancy professors
@findoc9282
@findoc9282 3 года назад
Most enjoyable experience i ever had of online learning, so much love and respect.
@sajalagrawal1430
@sajalagrawal1430 5 лет назад
Bro u are the best . cleared all doubts
@dr_920
@dr_920 4 года назад
If it is the max element in the BST, it should return null. The video doesn't take this corner case into consideration. Anyway, another really fantastic video. I learned a lot from the whole series. Thank you.
@Roover_sa
@Roover_sa 3 года назад
brother make an updated playlist of ds-algo with same explanation . i have not words to express who you are
@shubhamgoyal3100
@shubhamgoyal3100 6 лет назад
if the right subtree is present, the successor will be the node with the minimum value in right subtree else successor will be the node where we took the last left turn while searching for the current node.
@stage666
@stage666 9 лет назад
your videos are the best i can find. I love u
@monicaslv323
@monicaslv323 8 лет назад
+yoshinosakura Noooo. I love them more. :p
@prabaljainn
@prabaljainn 3 года назад
love me too
@saiudaybhaskarmudivarty7502
@saiudaybhaskarmudivarty7502 7 лет назад
Thanks for the video series. They've been really helpful for me these days. Can you please do a series on Balanced Binary trees and Tries as well. Thanks in advance.
@williamwambua7710
@williamwambua7710 3 года назад
Thank you very much...step by step I like that!! Bravoooo!!! wish you could make more videos on computer science
@Marko-vb2mi
@Marko-vb2mi 4 года назад
Dude, you just I locked my brain on this. Thank you so much!
@ScrappyVids
@ScrappyVids 7 лет назад
@mycodeschool, thanks for the video!, but I believe you should create the video for the predeccessor, in the end people use this videos to learn, but also as reference... dont treat us bad :(
@rahil_rehan
@rahil_rehan 2 года назад
theNewBoston of Indian software youtubers!
@RahulSharma-hp6hv
@RahulSharma-hp6hv 9 лет назад
Keep it up dude! you deserves appericiation
@shradheytripathi7564
@shradheytripathi7564 8 лет назад
Sir,I am a big fan of this channel.Please upload videos of some more topics of Data Structures.
@sumitsaini1601
@sumitsaini1601 7 лет назад
Woh, you did a problem on iterative inorder traversal without stack!
@jonsnow9246
@jonsnow9246 4 года назад
This isn't inorder traversal
@butlikereally
@butlikereally 6 лет назад
this is really really helpful. THANK YOU
@SumitKumar-fn3gj
@SumitKumar-fn3gj 5 лет назад
Thank You Sir
@kautukraj
@kautukraj 4 года назад
Very helpful!
@techswissknife2858
@techswissknife2858 2 года назад
Hey, great video thanks a lot. I was wondering a few things about the code you wrote: Is it better in C++ to use nullptr instead of the C macro NULL ? Is temp a good variable name? Coud we call the variable "successor" to make clear we're returning the successor? Is this: while(temp->left != NULL_ temp=temp->left; equivalent to: while (temp = temp->left) { ; //do nothing } Which one is a better c++ idiom?
@worldfromhome4033
@worldfromhome4033 2 года назад
Thank you is all I can say!
@zaneran5933
@zaneran5933 8 лет назад
Good analysis!
@arunkumargupta1947
@arunkumargupta1947 9 лет назад
i really enjoyed while watching each and every videos. can you add some videos of threaded binary tree if possible.............
@VC-kj9yx
@VC-kj9yx 5 лет назад
Thank u so much i finally understood this successor thing
@manfredoweber3562
@manfredoweber3562 2 года назад
i dont know if its good practice, but it a different faster implementation in trade off for higher memory requierements Node* successor(Node * root, int data, Node * ancestor=NULL){ //if found, return either right sub or ancestor if(root->data==data){ if(root->right!=NULL)return root->right; else return ancestor; } // search for Node, if call for left sub, give self as arg if (datadata)return successor(root->left, data, root); else return successor(root->right, data, ancestor); }
@namangupta2587
@namangupta2587 5 лет назад
Plss make more videos on graphs
@bharaniakella7734
@bharaniakella7734 7 лет назад
Java code for inorder predecessor-> public Node inOrderPredecessor(Node root,int key){ Node current=search(root,key); if(current==null)return null; if(current.left!=null){ return max(current.left); }else{ Node ancestor=root; Node predecessor=null; while(ancestor!=current){ if(key>ancestor.data){ predecessor=ancestor; ancestor=ancestor.right; }else if(key
@rhythmpatel5665
@rhythmpatel5665 6 лет назад
Needed this. Thanks :)
@olly779
@olly779 6 лет назад
you have a very good English!
@rohankademani6406
@rohankademani6406 3 года назад
Finally completed !!!
@heenagirdher6443
@heenagirdher6443 5 лет назад
Thanks sir.....
@niyas09
@niyas09 6 лет назад
very good explanation
@shaiksoofi3741
@shaiksoofi3741 4 года назад
Hats off.Its a treasure
@HeWhoShalNotBeNamed_
@HeWhoShalNotBeNamed_ 3 года назад
dude, a single optimization to propose, why not maintain the successor while you find the node? if right subtree doesnt exists,we simply return that value.. this way we sorta save the second traversal in else conditon. ideally it would be o(h) + o(h) but when we maintain it, it would turn out to be only o(h) single go. Find could be modified as :- > Node* Find(Node* root,Node* succ, int data){ Node* ancestor = root; while(ancestor->data != data){ if(data < ancestor->data){ succ = ancestor; ancestor = ancestor->left; } else{ ancestor=ancestor->right; } } return ancestor; } so , as soon as else is triggered, we directly return the succ node that we maitained during FInd().
@amateurbeginner7538
@amateurbeginner7538 7 лет назад
Inorder presecessor code in c : enjoy guys :) struct node* Getpresecessor( struct Node* root , int data ) { // Search the Node - O(h) struct node* current = Find(root,data); if(current == NULL) return NULL; if(current->left != NULL) { //Case 1: Node has right subtree return maxValueNode(current->left); // O(h) } else { //Case 2: No right subtree - O(h) struct node* presecessor = NULL; struct node* ancestor = root; while( ancestor != current ) { if( current -> key > ancestor -> key ) { presecessor = ancestor; // so far this is the deepest node for which current node is in left ancestor = ancestor->right; } else ancestor = ancestor->left; } return presecessor; } } struct node * maxValueNode(struct node* node) { struct node* current = node; /* loop down to find the leftmost leaf */ while (current->right != NULL) current = current->right; return current; }
@AbhishekKumar-yv6ih
@AbhishekKumar-yv6ih 7 лет назад
// so far this is the deepest node for which current node is in left. correction: left should be right.
@siddalingammakasbag5360
@siddalingammakasbag5360 5 лет назад
thank you sir it very much cleared way
@AbhishekKumar-ff9vg
@AbhishekKumar-ff9vg 3 года назад
FOR PREDECESSOR : - CASE 1 : we will have to find max in left subtree of the given node CASE 2: and when left subtree is not available curr = node_given ; ancs = root ; pred = NULL while ( ancs != NULL ) { if( ancs->data < curr->data ) { pred = ans; ancs = ancs->right; } else ancs = ancs->left; } return ancs;
@usama57926
@usama57926 6 лет назад
thank u bro
@vasantprabhu
@vasantprabhu 2 года назад
in the else part inside while loop ,we need a "sucessor=ancestor" there too i think (right subtree part)
@vasantprabhu
@vasantprabhu 2 года назад
update : - i am wrong . the code is correct. explanation is at 16:55 . not all nodes will have a successor. there is no successor the right most node.
@bpunsky
@bpunsky 10 лет назад
I noticed it's been a while since you uploaded any data structure videos. Will there be more, or is this the end of the line? Also, if you do continue the series, it would be cool to see a hashmap/hashtable implementation.
@mycodeschool
@mycodeschool 10 лет назад
***** No this is not an end. My co-founder who was an amazing friend passed away in an accident and I have not been able to focus much after that. Its been a hit. I am trying to reset things and keep this project going. Expect some videos in this data structures series in August. Video creation is slow and creative process. But 4-5 videos in a month is still something that I can do. -Animesh Nayan Co-founder, MyCodeSchool
@bpunsky
@bpunsky 10 лет назад
mycodeschool Wow, sorry to hear that. Not much I can really say other than that I feel for you and hope you come out the other side alright. It really makes the lack of videos seem insignificant by comparison, but I'm glad to hear that you are going to continue with your channel - it's very informative and I imagine helpful to a lot of people. Good luck and again, I'm very sorry to hear about your friend.
@1gouravgg
@1gouravgg 10 лет назад
***** in addition, he was the No.1 coder of India!!
@wingzero1912
@wingzero1912 7 лет назад
You've been very helpful Animesh for a lot of people out there.
@kritikasinghal8015
@kritikasinghal8015 6 лет назад
sir,please made video on combinations of sum
@PROcrastiDRIVESVofficial
@PROcrastiDRIVESVofficial 7 лет назад
Excellent! Thank you.
@roshantopno1253
@roshantopno1253 7 лет назад
great work
@wangchi562
@wangchi562 Год назад
bro, please come back!
@gobibs1
@gobibs1 6 лет назад
pls talk about n-ary tree
@sheardz6380
@sheardz6380 9 лет назад
Thanks
@cherrynaresh3111
@cherrynaresh3111 7 лет назад
while(ancestor != current) will not work to find inorder successor of a element in right subtree. So if we change it to while(ancestor != null) i think it will work.
@RaviRanjan_ssj4
@RaviRanjan_ssj4 9 лет назад
***** you said that you will be making more lessons on problems on Binary Trees and Binary trees in this video. Why did you stop ?
@desihaxor5690
@desihaxor5690 5 лет назад
his best friend died in a car accident
@gabrielpereiramendes3463
@gabrielpereiramendes3463 5 лет назад
Very Good!
@ankitsingh8087
@ankitsingh8087 7 лет назад
Can you add one more lecture on avl tree before graph.. That is the thing which is missing in your lectures
@namans9803
@namans9803 7 лет назад
sir , in this we don't have condition to check if the element is largest ,then there will be no successor.Can we use a find max function to check if it is the largest element ? will it affect the time complexity ??
@frozentaco143
@frozentaco143 5 лет назад
Great video. But it would have been much clearer if in case2 code, ancestor was named as temp and successor was named as ancestor.
@RP-th8in
@RP-th8in 9 лет назад
what about 11, what case does it fall into
@---xk4iw
@---xk4iw 9 лет назад
why there are ont avl tree?
@amateurbeginner7538
@amateurbeginner7538 7 лет назад
u are bossssssss
@listlist8982
@listlist8982 3 года назад
Hi! Your code had an error when I compiled it. The error is in the last line return successor; and says that successor undeclared (first use in this function). I delcared struct node * successor = NULL; at the top but then it doesn't return anything cause its Value is NULL and the value never changes inside the else statement! How can I fix it?
@santwanadutta4488
@santwanadutta4488 4 года назад
I guess that successor=ancestor, you have to define outside the if block. Am I correct please reply someone.
@effy1219
@effy1219 7 лет назад
excellent
@albertpatterson8164
@albertpatterson8164 7 лет назад
I like this.
@arjunpassi9945
@arjunpassi9945 9 лет назад
Can't you traverse (right root and then left) and check if the next node you are going to traverse is the one you are looking the successor for?
@rishigoley3521
@rishigoley3521 7 лет назад
Complexity would be O(n) for your approach.
@DevGamerShripadAgashe
@DevGamerShripadAgashe 6 лет назад
AVL tree please
@rimpinag6346
@rimpinag6346 4 года назад
Plz tell me sir whats ur name?? Really amazing video
@TonyDaExpert
@TonyDaExpert 2 года назад
did he cover AVL trees?
@gauravchauhan873
@gauravchauhan873 4 года назад
what about AVL and B+ trees
@wisepalaz
@wisepalaz 5 лет назад
what is the successor of 27 or 25 in this case?
@manojpatial2404
@manojpatial2404 10 лет назад
share one tut on threaded binary tree if possible ;)
@sandeepkumawat4982
@sandeepkumawat4982 4 года назад
11:072 Excuse me!!!😂😂
@ArjunSoota
@ArjunSoota 5 лет назад
GOAT
@ShivamKhantwal94
@ShivamKhantwal94 3 года назад
Deserving ❤️❤️
@looploop6612
@looploop6612 5 лет назад
how does it come back from 6 to 8?
@rudavathvinod5916
@rudavathvinod5916 2 года назад
How to find post order by given data in- order d b e a f c g and pre order a b d e c f g
@alexrcrew1975
@alexrcrew1975 2 года назад
simply first make the tree after that find post order as simple as that
@kashishmiglani6576
@kashishmiglani6576 8 лет назад
great
@scriptkiddie6151
@scriptkiddie6151 3 года назад
This good initiative had a bad end.
@saipratapdannana8048
@saipratapdannana8048 6 лет назад
your all videos are awesome. why did you stop uploading videos bro ?
@shamikbhattacharjee7321
@shamikbhattacharjee7321 5 лет назад
because one of the contributors humblefool unfortunately died.
@musicalive1782
@musicalive1782 6 лет назад
Good tutorial, but in your else statement you missed succesor = ancestor;
@harshitadevichopra8097
@harshitadevichopra8097 5 лет назад
nope bro, if we are going right, we dont have to move the succesor... see from 9:22 again... hope u'll get it
@meghasyam427
@meghasyam427 6 лет назад
things r getting real tough...
@antoniogargiulo9491
@antoniogargiulo9491 2 года назад
love u
Далее
Binary tree traversal: Preorder, Inorder, Postorder
14:29
10.1 AVL Tree - Insertion and Rotations
43:08
Просмотров 1,2 млн
I gave 127 interviews. Top 5 Algorithms they asked me.
8:36
Data structures: Binary Search Tree
19:28
Просмотров 1,3 млн
Data structures: Binary Tree
16:17
Просмотров 1,4 млн
Top 7 Algorithms for Coding Interviews Explained SIMPLY
21:22
Binary Search Animated
7:00
Просмотров 29 тыс.
Balanced binary search tree rotations
8:51
Просмотров 167 тыс.