Binary Search Trees (BSTs) - Insert and Remove Explained

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

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

  • @fuckkg
    @fuckkg 9 лет назад +59

    this is probably the best tutorial on RUclips

  • @Karim-nq1be
    @Karim-nq1be Год назад +4

    First I thought this explanation was going bad because the quality of the picture you put is low. But that's actually one of best explanations I've seen, thank you.

  • @williamz8330
    @williamz8330 4 года назад +3

    This is the best explanation of BST methods I've seen

  • @Simoky99
    @Simoky99 5 лет назад +8

    video from 2013 helping me 6 years later.... thanks!

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

      Glad to hear it! :-) - Colleen

    • @Serrchh
      @Serrchh Месяц назад

      And here for me 11 years later. I hope you achieved your goal from back then!

  • @JohnSmith-zg2id
    @JohnSmith-zg2id 8 лет назад

    What my professor couldn't make clear lecture after lecture after lecture, nor could my TA, you did in 6 minutes. Thank you so much for sharing this.

  • @VipinKumar-uy2sw
    @VipinKumar-uy2sw 8 лет назад +5

    Damn ! Tree operations are so easy ! This 6 min video taught me what I couldn't learn in 3 hour class. U r amazing ...thx ...muaahhh :)

  • @nafishy
    @nafishy 2 года назад +5

    Awesome! clear and concise explaination! İ came here from the Odin project

  • @Daniellagnaux
    @Daniellagnaux 5 месяцев назад +1

    Of all videos on RUclips, you posted the best tutorial on BST

    • @ColleenMLewis
      @ColleenMLewis  5 месяцев назад

      Thank you! I'm glad it was helpful to you!

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

    concise and clear, such a beautiful presentation ma'am

  • @vishnuvalleru
    @vishnuvalleru 10 лет назад +1

    This is the best explanation I ever came across. short and up to the point. Thank you.

  • @aryangoel4320
    @aryangoel4320 11 месяцев назад +1

    best explaination , simple language and covering all the cases . THANKS A LOT

    • @ColleenMLewis
      @ColleenMLewis  10 месяцев назад

      Glad it was helpful! Thanks for the note!

  • @jayjoshi3853
    @jayjoshi3853 9 лет назад

    I have checked many insertion and deletion videos, but yours is best one. Sweet, short ,simple and effective though. Thanks.

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

    This was one of the best explanations I have seen yet, so simple, so concise. Thank you, Colleen

  • @dulanjanaliyanagama3823
    @dulanjanaliyanagama3823 8 лет назад

    The way you explained is brilliant!!! It's marvellous that you manage the whole thing within 6 minutes. Job well done!!!

  • @ank2871
    @ank2871 2 года назад +2

    thank you so much for that simple explanation!. i was so confused about this all the time

  • @geeksclub3455
    @geeksclub3455 9 лет назад

    The best video ever.. Made the deletion look so much easier.. I wish you were my teacher

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

    Small and concise. To the point. Loved it. Thanks alot.

  • @MrNish27
    @MrNish27 9 лет назад

    amazing explanation. I used to always get stuck in the removal of a node, but now everything is clear.

  • @TheXHypex
    @TheXHypex 9 лет назад +8

    Learning for my exams at the moment so thank you very much for this video!

    • @ColleenMLewis
      @ColleenMLewis  9 лет назад +1

      TheXHypex Thanks! I hope the exams went well!

  • @AmbiguousAbhi
    @AmbiguousAbhi 8 лет назад

    i have my exam in 1 hour, and was getting confused in delete and insert. U made it look very easy. thanks a lot . #awesome

    • @ColleenMLewis
      @ColleenMLewis  8 лет назад

      +Abhijeet Joshi Good luck! :) Thanks for the comment!

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

    It took you 6minutes to explain something our teacher failed to do in 2 hours, thx

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

    Binary Search Tree Delete: 1:45

  • @shahzamanniazai3832
    @shahzamanniazai3832 9 лет назад +4

    Excellent video ... Best way to learn is to sit patiently, watch and practice the mind along with the video (pausing video at various points to do self analysis).....
    Reply ·

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

    Algo and Data structures exams in 2 hours. Thanks for saving my life.

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

      Thanks for the note - I'm happy it was helpful! - Colleen

  • @rinormehmeti8515
    @rinormehmeti8515 7 лет назад

    This video might be old but it was so useful and effective!
    Thanks a lot! Wish you all the best!

  • @alexitosrv
    @alexitosrv 8 лет назад

    Very succinct and to the point. Good video! I loved also the style for representing the different cases so I can rebuild the algorithm my own head without need to memorize anything else.

  • @xdae
    @xdae 7 лет назад +6

    Great! This was a really clear review. Except for case 3 you only talked about finding the min of the right subtree. One can also replace the target node with the max of the left subtree. Either method works in keeping the tree in order.

    • @ColleenMLewis
      @ColleenMLewis  7 лет назад +3

      That's correct. And we only need to replace it with one of those - and we should be consistent about which one we choose.

    • @joshuaronisjr
      @joshuaronisjr 7 лет назад

      👍🏿

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

      nice

  • @Mmnc-bv3rk
    @Mmnc-bv3rk Год назад

    best explination i could find on the subject, thank you a lot

  • @khumkhatri5810
    @khumkhatri5810 8 лет назад +3

    Thank you so much your explanation is very good and easy to understand....hope you will be uploading such types of explanation in next topics

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

    This clears up everything I only 6 minutes!

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

      I'm glad the video is helpful to you! - Colleen

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

    Using your video to prepare for an interview. Thanks for your help.

  • @Anooshaism
    @Anooshaism 7 лет назад +2

    But what happens to nodes labeled 34 and 36 when we remove their father( when we replaced 30 by 32) Now 34 and 36 should be placed on the left subtree of 40 and not on its right, how to deal with it algorithmically?

    • @ColleenMLewis
      @ColleenMLewis  7 лет назад +1

      When we replaced 30 with 32 we had to have deleted 32 so it is only in the tree once! To see what happens to 34 and 36 in that case, you can watch the video at 2 minutes and 50 seconds (ruclips.net/video/wcIRPqTR3Kc/видео.htmlm50s) where I had shown 32 being removed. Node 34 would be the new left child of node 40 (and node 40 would be the parent of node 34). Does that make sense?

    • @Anooshaism
      @Anooshaism 7 лет назад

      Yeah! Now I see it clearly! Thanks

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

    your video is best than other countries RUclipsr video

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

    professor dude from my uni took 2 hrs to explain this 6 mins and this female wibba explains a whole lot better, thanks a lot madam.

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

    This video is really helpful and effective to learn about trees.I am from Bangladesh.Thanks Colleen Lewis ♥

  • @FA-ff4dz
    @FA-ff4dz 5 лет назад

    Thank you so much for making such an awesome video that explains the whole idea clearly. It helps me a lot

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

      I'm glad it was helpful! Thanks for the comment!

  • @towhidskynet
    @towhidskynet 9 лет назад +14

    Finally. English. thank you so much!!!

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

    I can't believe I understand this now better than after a 2 hours class at the uni

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

    if I implement the remove method recursively, how can I remove a leaf ? Since by the time the recursion gets to the leaf, we lose access to the parent node

  • @mdilligaf
    @mdilligaf 7 лет назад

    Useful and clear, thanks. BSTs seem like a very useful invention.

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

    Best explaination

  • @Ginzo111
    @Ginzo111 10 месяцев назад +1

    Amazing explanation, thank you

  • @badis23
    @badis23 9 лет назад

    Very clear tutoriial and nice voice too thanks sweety!

  • @jayrmadrio8142
    @jayrmadrio8142 3 года назад +2

    THANKYOU SO MUCH😭💓

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

      Thanks for the comment! I'm glad it was helpful! :-)

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

    Thank you so much for this video!

  • @PRIYANKA1998
    @PRIYANKA1998 9 лет назад +2

    i never thought it was this easy

  • @Mahdi-hq4te
    @Mahdi-hq4te 8 лет назад

    Very helpful, thank you! I also liked the way you explained it.

  • @deveren
    @deveren 9 месяцев назад +2

    Greetings from odin!

  • @squirrelbrains2197
    @squirrelbrains2197 8 лет назад

    thanks, that was a simple, but very clear explanation.

  • @sonic2kk
    @sonic2kk 7 лет назад +1

    Extremely useful! Thanks for uploading.

  • @sreekeshv12
    @sreekeshv12 7 лет назад

    Awesome thanks understood it within a time span of 6 mins :D

  • @mmsky6316
    @mmsky6316 7 лет назад +1

    this helps me. I understand BSTs really well, but removing nodes continues to stump me. thank you for the clarification.

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

    So when removing 32. 34 and 36 just points to 40 on its left side, correct?

  • @bombambum7955
    @bombambum7955 9 лет назад +19

    short and effective !!

    • @ColleenMLewis
      @ColleenMLewis  9 лет назад +7

      BomBamBum Thanks! And thanks for the ascii art below! :-)

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

    thank you so freaking much!

  • @TheThunderSpirit
    @TheThunderSpirit 8 лет назад +6

    what happens after deleting 32?
    what happens to 34 and 36?

    • @ColleenMLewis
      @ColleenMLewis  8 лет назад +5

      If we start with the tree at the very beginning of the video and delete 32, then we're deleting a node that has only one child. That's a simpler case (than if it had two children) and we can just make 40's left child be 34, which effectively removes 32. We just keep the same connection between 34 and 36, because in general we try to avoid restructuring binary search trees. Does that help?

    • @TheThunderSpirit
      @TheThunderSpirit 8 лет назад

      colleen lewis yes. so while deleting 30 why do u choose to swap 32 in place of 30?
      we can choose 20. that can work too?
      while deleting 70 we can replace by 65. which is next smaller that too maintain tree property.? does it always have to be next bigger element?

    • @ColleenMLewis
      @ColleenMLewis  8 лет назад +2

      That's just a convention for what you do when you delete a node with two children. I have chosen to use the next biggest rather than the next smallest, but either would work just fine. Does that make sense?

    • @TheThunderSpirit
      @TheThunderSpirit 8 лет назад

      colleen lewis yes. now the main question is how to decide which one choose to swap so that least cost is inccured.
      does it also work for equal keys?

    • @ColleenMLewis
      @ColleenMLewis  8 лет назад

      Two answers:
      Usually we'd just implement either the next biggest or the next smallest. Not both.
      We never have duplicate keys in our BST - so that never comes up. If we try to add a key that is already in the BST, we would just replace the old value associated with that key.

  • @erickvazquez2070
    @erickvazquez2070 9 лет назад

    Thank you so much! This is very clear and easy to follow!

  • @markbargo1677
    @markbargo1677 8 лет назад

    If i built a tree in order to get to any element in log(n) operations, is it possible to insert an element the way not to break this feature. (in case, n!=2^k)

    • @ColleenMLewis
      @ColleenMLewis  8 лет назад

      Q: If i built a tree in order to get to any element in log(n) operations, is it possible to insert an element the way not to break this feature.
      A: Yes :-) If you can access any element in O(log(n)) operations, you can also add one element in O(log(n)) operations.
      What do you mean by: (in case, n!=2^k)
      - Colleen

    • @markbargo1677
      @markbargo1677 8 лет назад

      Oh, you missunderstand me. I mean we can get any element in log(n). And what I want to do, I want to insert an element in any amount of operations (better minimal) and still be able to get any element in log(n). If we have a tree of the only node 1 and insert 2,3,4..., n, we are going to get a line with kinda n elements. But I want it to be a binary search tree after every insert.
      And, ofc, if we have 2^k nodes, we can get any element in k operations, but inserting one node, there will be 2^k+1, so there will probably be at least 1 node, we get in k+1.

  • @ThiagoBenazziMaia
    @ThiagoBenazziMaia 8 лет назад

    Fastest and best video -)

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

    What an awesome explanation! Thank you a lot :)

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

    Given a list of number that you are going to insert, how do you decide which one is the root? The first one ?

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

      Since we never restructure things in a binary search tree - if we insert a list of numbers, the first one will always end up as the root!

  • @shashankk197
    @shashankk197 8 лет назад

    Is there a similar explanation on red black trees insertion and deletion.....help much appreciated :)

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

    can somebody tell me if the binary ratio 2:2 and what will be the structure under this system

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

      I'm sorry - I don't understand your question. Can you say a bit more?

  • @halluciongen3000
    @halluciongen3000 10 лет назад +1

    This was really good, thank you!

  • @As-iy2ki
    @As-iy2ki Месяц назад

    For the left subtree, wouldn't it be that we choose the largest node in the left subtree and smallest node in the right subtree?

    • @ColleenMLewis
      @ColleenMLewis  Месяц назад

      I think you're talking about delete. I think you're on the right track. We can decide to replace the node with the thing that is "just smaller than it" (biggest in the left subtree) OR "just bigger than it" (smallest in the right subtree)- What part of the video are you asking about?

    • @As-iy2ki
      @As-iy2ki Месяц назад

      @@ColleenMLewis I was asking about the what nodes we can choose when we are deleting a node that has 2 children e.g. the root node if that is what i am deleting.
      I wanted to know does it matter if I choose the to replace the root node with either the smallest node on the right subtree or the biggest node on the left subtree?
      (I was revising for my test at the time so please excuse my the wording of my initial comment. Your video did help me out a lot tho so thank you so much).

    • @ColleenMLewis
      @ColleenMLewis  Месяц назад

      @@As-iy2ki Thanks for clarifying! Yep - it doesn't matter which of those: either the smallest node on the right subtree or the biggest node on the left subtree!

    • @As-iy2ki
      @As-iy2ki Месяц назад

      @@ColleenMLewis Just wanted to say thank you.
      I got 80% in my test. Thank you! You are very good teacher. You saved me from getting the big mark questions wrong lol.

    • @ColleenMLewis
      @ColleenMLewis  Месяц назад

      @@As-iy2ki Congrats! :-)

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

    When you removed 32, is it the node itself or the pointer?

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

      Great question! The tree stores pointers to the content that they're storing. Here our keys (the things we search for) were numbers, but you could imagine that they could be something where we'd naturally store a pointer to the thing (like a String). When we remove 32 (which has one child) we remove the whole node! When we remove a node with two children, we'd typically just update the pointers in that node and remove the node that has the key we'll use to replace the node with two children. Does that help? - Colleen

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

      colleen lewis yes it’s does, thank you!

  • @AlivHasan
    @AlivHasan 9 лет назад

    awesome..it helped a lot...lucid explanation

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

    So what happens if you try to the number 33? does that get added as a leaf too?

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

      Yes! We only ever add things as leaves. And 33 would end up as the left subtree of 34.

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

      colleen lewis Awesome! I thought so, but I wasn’t quite sure. Thank you for replying!

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

    Thank you soooooo muuuuuuch.

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

      I'm glad this was helpful for you!

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

      colleen lewis you have no idea. I passed my course because of your video. I cant even thank you enough.

  • @durgeshbg
    @durgeshbg 6 месяцев назад

    its 2024 and this's still a great explanation

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

    what a badass teacher

  • @SuperJAIMISH
    @SuperJAIMISH 8 лет назад

    Pretty concise and clear. Thanks!

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

    so when I replace 30 with 32. do 34 and 36 stay as children of 40? or they follow 32 and 40 becomes a child of 36?

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

      I think it's easier if they follow 32. You can add an extra method to transplant the whole subtree, and 40 becomes the child of 36.

  • @gouki1001
    @gouki1001 7 лет назад +1

    Awesome simple video!

  • @racmo1000
    @racmo1000 9 лет назад

    Very helpful tutorial :) but I have a question:
    Can we find the biggest element in the left subtree rather than the smallest in the right subtree in different version of this algorythm?

    • @ColleenMLewis
      @ColleenMLewis  9 лет назад

      racmo1000 Yes! That is equivalent to pick the biggest element in the left subtree :-)

    • @racmo1000
      @racmo1000 9 лет назад

      colleen lewis Thanks for such quick response!

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

    Tried to plot these numbers into a visualizer, and on deletion it selected 65 instead of 75. OP is this a self-balancing tree? WTF?

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

      To all future students:
      There are two ways to do this. You either select the maximum value from left side, or the minimum value from right side.
      Colleen selects 75 here, but most visualizers will select 65 since it's the maximum from left side.
      Hence my confusion above

  • @kedarnadkarny4718
    @kedarnadkarny4718 7 лет назад

    Clearly explained. Thanks!

  • @cKEntertainment
    @cKEntertainment 3 месяца назад +1

    Herrroooo from The Odin Project

  • @danielgrindrod9526
    @danielgrindrod9526 8 лет назад

    Brilliant video, thanks!

  • @TheVirtualLab
    @TheVirtualLab 7 лет назад

    Amazing explanation thank you!

  • @joshsmit779
    @joshsmit779 7 лет назад

    Super good explanation

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

    what approach is this? right-left?
    thankss..

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

      Sorry - I don't understand your question. Can you rephrase it?

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

    Amazing video! Thank you

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

    awesome explanation

  • @chuaselo
    @chuaselo 8 лет назад

    what would be the T(N) analysis? I am curious to know

    • @ColleenMLewis
      @ColleenMLewis  8 лет назад

      Here's a hint. We can traverse between nodes with just 1 step, and we can remove a leaf with one step, and we can remove a node with only a single child with one step. Can you come up with a case that would require only 1 step to remove the node you're hoping to delete? Can you come up with a case where it would require N steps to remove the node you're hoping to delete? (Assume that N is the number of nodes in the tree).
      I bet you can figure these questions out :-)

    • @chuaselo
      @chuaselo 8 лет назад

      colleen lewis O(log(N)) ?

    • @ColleenMLewis
      @ColleenMLewis  8 лет назад

      What would need to be true of the node you remove if it is O(log(N)). It could take only one step - what would be true of the node you remove in that case. It could be N steps, what would be true of the node you remove in that case?
      The best way to think about this is as different cases. Can you draw a really "good" tree. Or a really "good" node to try to remove that would make it easy? Or a really "bad" tree/node to remove?

    • @chuaselo
      @chuaselo 8 лет назад

      colleen lewis I really appreciate your quick responses . I think I just need more time to study on this material . Thank you for your time

    • @ColleenMLewis
      @ColleenMLewis  8 лет назад

      :-) Consider a tree with only 1 node - and then you try to remove that node! :)
      What about if I have the nodes 1, 2, 3, 4, 5 .... 99,999, 100,000 and I added them in that order such that I got something that really didn't look like a tree - it looked like a list. where each node had only a right child! How many steps would it take to remove the 1? How many steps would it take to remove the 100,000? :-)

  • @saddamhossain2171
    @saddamhossain2171 8 лет назад

    Nicely explained!!!.... subscribed... :)

  • @roykamau7001
    @roykamau7001 7 лет назад

    Thanks Colleen..
    So so much

  • @AgentSubZero4
    @AgentSubZero4 8 лет назад

    thanks.. the best.. helped me alot..

  • @abdallhghanem708
    @abdallhghanem708 9 лет назад

    Very Very Very effective thank you so much

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

    Thank you!! You're amazing:)

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

    This helped a lot thank you. I did something wholly different than this.

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

    Can u make a video of avl trees? Thanks

  • @shadownirvana5044
    @shadownirvana5044 9 лет назад

    Good explaination. Thanks!

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

    awesome video !

  • @ericknicolasnunesdahora4860
    @ericknicolasnunesdahora4860 7 лет назад

    Thank you professor! You helped me a lot! \o/

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

    Ok but suppos we have to insert one more 32 element in binary search tree then where would we insert it plssass rply me

  • @mohammadusman3828
    @mohammadusman3828 8 лет назад

    ma'am you are just perfect :*

  • @tanmoyt9391
    @tanmoyt9391 9 лет назад

    Thanks...this was very helpful

  • @mikek.2703
    @mikek.2703 8 лет назад

    Thanks a lot for your help.

  • @LTM4620
    @LTM4620 9 лет назад

    Nice explanation :) like it ..