Insertion for Red-Black Trees ( incl. Examples ) - Data Structures

Поділитися
Вставка
  • Опубліковано 18 гру 2024

КОМЕНТАРІ • 34

  • @moisesacero4995
    @moisesacero4995 4 роки тому +26

    Question, why do we rename the z, y, x nodes to abc? Why relabel when we can just work with z, y, x, or if you really like your abc then why not label them abc in the first place?

  • @charlesstrickland8839
    @charlesstrickland8839 5 років тому +22

    Just watched the first red-black tree video, after 3 years the second part comes out

  • @thando6109
    @thando6109 Рік тому +3

    this was so helpful!! i might actually pass my exam now...

  • @DrCaesarsPalace_MD
    @DrCaesarsPalace_MD Рік тому +1

    Thanks for the explanation, it filled in some gaps and helped me understand it 👍

  • @muyuan5935
    @muyuan5935 5 років тому +9

    Nice video! But I think there is one case missed:
    when x is the right child of y and y is the left child of z, we need to first rotate the x-y link then follow the operations of CASE1.

  • @Momo-bb2fn
    @Momo-bb2fn 4 роки тому +8

    Pseudo Code
    Insert(x)
    add(x)
    rebalanceInsert(x)
    if x is the root:
    make x black # depth decreases by 1
    else:
    make x red
    y = parent of x, z = grandparent of x
    if y is red:
    s = sibling of y # recoloring is needed
    if s is black: # Case 1
    a, b, c = restructure(x) # i think this means that zyx now = abc
    make b black
    make a red
    ake c red
    else: # Case 2
    make y black
    make s black
    make z red
    rebalanceInsert(z)

  • @olenaqwerty7895
    @olenaqwerty7895 3 роки тому +1

    good explanation, thanks!

  • @jihadshaito9116
    @jihadshaito9116 3 роки тому +1

    Thanks for the video, but in all the video you mentioned S to be black as an external node, however it wasn't an external node and in this case we will be breaking the black depth property, so then 'a' needs to be colored black. Hope you reply soon, I have an exam on Wednesday xD.

  • @tyapka
    @tyapka 4 роки тому +2

    So the point is that it just rebalances itself according to some rules, and colors there are just to make understanding of rules a bit simpler.

    • @MisterCode
      @MisterCode  4 роки тому +1

      Yes, that is the essence. The colors are encoded as bits during implementation. However, explaining all of this with 0s and 1s is a bit cumbersome. Good remark! :)

  • @second_second_
    @second_second_ 4 роки тому

    At 5:40, (s is black), then we're not introducing double red. But wouldn't it violates the black depth? How can we remedy that?

    • @MisterCode
      @MisterCode  4 роки тому +1

      By design, fortunately, executing the steps of case 2 neither violates the depth property.
      Initially, the left and right subtree of x, the right subtree of y, and the right subtree of z do not violate any properties. After the execution of the steps, notice that the left and right subtree of x become the left and right subtree of a, respectively. Moreover, the right subtree of y becomes the left subtree of c. Finally, the right subtree of z becomes the right subtree of c. We have not increased the black depth for any of these subtrees (or nodes a, b and c) during these steps. Therefore, we are not violating the depth property.
      Good question! :)

  • @jordanbecker7006
    @jordanbecker7006 Рік тому +1

    thank you bro

  • @second_second_
    @second_second_ 4 роки тому

    6:26 if z is root, what does it mean by repeating the algorithm? simply color z to black again?

    • @MisterCode
      @MisterCode  4 роки тому +1

      Yes, if z is the root in case 2, we eventually color it black.
      "Repeat recoloring for z" means that we indeed repeat the algorithm. Looking at the pseudocode, this corresponds to the recusive call "rebalanceInsert(z)" at 11:42. Assuming that z is the root, it is colored black by the call "makeBlack(x)" at 11:02.
      Good question! :)

  • @stephenhowe4107
    @stephenhowe4107 9 місяців тому

    Sorry it is insufficient.
    In the case of a new node inserted that is red, if the parent is red and grandfather of course black and uncle is black (case 1 at 4:58):
    It further depends if x is on the outside of the tree or the inside,
    (*) If on the inside, an additional rotation in the direction of the outside takes place on node y. Now x is parent node.
    Then the final rotation takes place about z away from newly inserted node.
    But step (*) is critical and you have not talked about it,
    Insert always has a possible maximum of 2 rotations to fixup (sometimes 1 rotation, sometimes up to log (N) /2 * 3 recolorations.
    Delete always has a possible maximum of 3 rotations to fixup.

  • @alaafathy5361
    @alaafathy5361 4 роки тому +1

    when we inserted 23 how 29 become black and 31 red ?

  • @parasite6731
    @parasite6731 5 років тому +1

    Really a good video

  • @ShivamYadav-rx3hy
    @ShivamYadav-rx3hy 5 років тому +1

    nice video really helped

  • @Momo-bb2fn
    @Momo-bb2fn 4 роки тому +3

    Doesn't he add a non existant child in each insert such as at 8:11 where the inserted 23 should have one single child? 29 has two children. One stays with 29 as 23's sibling, the other become's 23's child so why does 23 end up having _two_ children? Where'd that third node come from? Assuming this doesn't happen, would this imbalance the tree?

    • @MisterCode
      @MisterCode  4 роки тому +1

      The additional external node is added to prevent the external property from being violated. The external nodes can be regarded as dummy nodes to prevent any external property violations. It is common practice to maintain these dummy nodes during Red-Black Tree operations. In order to prevent any confusion, I should have mentioned that too in the video. Regarding your second question, Red-Black Tree property violations eventually always result in an imbalanced tree.
      Thank you for your question! :)

  • @ioanadobos2840
    @ioanadobos2840 4 роки тому +1

    thank you!

  • @soufianebaeloul3382
    @soufianebaeloul3382 3 роки тому +1

    Bigup bro tnx

  • @varadthokal1406
    @varadthokal1406 4 роки тому +2

    when we inserted 5, why does it have two children? it replaced one child of 7, so it should have only one child??

    • @tyapka
      @tyapka 4 роки тому

      That's the rule - leafs must be empty nodes, so you add to nils to 5

    • @rasathuraisivaram8301
      @rasathuraisivaram8301 4 роки тому

      When we insert an element, we normally insert like BST. then we arrange based on reb black tree properties

    • @rasathuraisivaram8301
      @rasathuraisivaram8301 4 роки тому

      @@tyapka this one will be disliked by me since it may be not a good answer for this ques

  • @herrschultz7413
    @herrschultz7413 4 роки тому

    why could you not insert the "23" at the right of 7?

    • @rx_lang
      @rx_lang 4 роки тому +1

      Because 23 is bigger than 10 (the root), so 23 has to go to the right of 10.

  • @Jaykumar-po4wq
    @Jaykumar-po4wq 2 роки тому

    Case 1: s is black right rotate around z and interchange colour of z and y

  • @acesoberano447
    @acesoberano447 14 днів тому

    Why 7 is black?

  • @jordyfra
    @jordyfra 4 роки тому +1

    thank you!