Red-black trees in 5 minutes — Insertions (strategy)

Red-black trees in 5 minutes — Insertions (strategy)

Michael Sambol

7 лет назад

469,429 Просмотров

Ссылки и html тэги не поддерживаются


Комментарии:

vgreddy saragada
vgreddy saragada - 09.10.2023 07:57

Great explanation ..Thank you so much ..

Ответить
Vinicius
Vinicius - 19.07.2023 02:28

😉🙏

Ответить
Vinicius
Vinicius - 15.07.2023 04:23

Great explanation! Help me much.

Ответить
Evan Teboul
Evan Teboul - 28.06.2023 23:48

why do you perform any of these operations when the tree remains balanced.

Ответить
Guilherme Matos
Guilherme Matos - 24.06.2023 15:20

Holy shit i'm so dumb

Ответить
Harshana Semasinghe
Harshana Semasinghe - 08.04.2023 11:57

Best video series to understand red black tree

Ответить
Peanutology Institute
Peanutology Institute - 07.02.2023 07:58

I don't like how you left out the subtrees, which you could have included a small triangle in the illustration so that the audience knows that something's there; that way we know that a red node is not a leaf node.

Ответить
nhatlam
nhatlam - 28.01.2023 14:46

In case 1 the structure is a triangle, does that characteristic matter? Because in case 2 it mattered.

Ответить
Matthieu
Matthieu - 03.09.2022 12:14

But why at the end of case 2 are there two red nodes aside ?

Ответить
Omar Abu Snineh
Omar Abu Snineh - 30.07.2022 11:25

The doctor at the university explained it in an hour and I did not understand it. you are here. you explained it in a few minutes and I understood everything from you. Thank you very much.

Ответить
Lotus Tree
Lotus Tree - 22.07.2022 19:03

Thank you for the video! Could you consider posting AVL tree when you have time? thank you!

Ответить
塩酸田代
塩酸田代 - 10.06.2022 08:58

I feel how you present case 2 and 3 is confusing. It sounds like they are independent, i.e. if (case 2), else (case 3). However, after handling case 2, case 3 definitely will occur. So it's more like you should handle case 2 if it applies, but case 3 is required anyway if uncle is red.

Ответить
Howard Yin
Howard Yin - 22.02.2022 08:37

thank you Michael~

Ответить
santosh kumar
santosh kumar - 02.01.2022 21:08

If this would have explained with numbers it would be more clear.

Ответить
Vansh Rana
Vansh Rana - 29.12.2021 12:32

Another banger, awesome job man😊

Ответить
LOVEडासूर
LOVEडासूर - 24.12.2021 00:29

Thank you for the video, you were really clear and pointed out solid points when rotating a node!
Sad to see you stopped uploading videos!

For those who are struggling with the pseudocode:--
0.The RBFixup algorithm only starts when z's parent is red, then case 1,2,3 follows. KEEP TRACKING THE VALUE OF Z.
1. The value of z changes whenever case 1 and case 2 occurs.
2. Recoloring process takes place only when case 1 and case 3 occurs.

Ответить
Apna Code
Apna Code - 18.12.2021 07:39

Can make video on rb tree deletion in 4 minutes please

Ответить
Apna Code
Apna Code - 18.12.2021 07:28

Thanks a lot bro for charming explanation 😊😊😊

Ответить
Stephen Marshall
Stephen Marshall - 18.12.2021 06:32

Uncle 😂

Ответить
Angayarkanni A
Angayarkanni A - 21.11.2021 18:44

crisp and clear explanation. super . Great effort. thanks

Ответить
utube channel
utube channel - 18.11.2021 09:59

Plz make vedio on deletion sir

Ответить
Soccer HD
Soccer HD - 26.05.2021 01:41

Awesomee Explaination! Thankssss Alottt brother

Ответить
jose padilla
jose padilla - 01.03.2021 13:48

"As you can see C is Z's uncle" great video but you might have chosen better names for the nodes

Ответить
Oli
Oli - 22.10.2020 18:55

For python students, what property would "T.nil" be in the pseudo?

Ответить
mike
mike - 31.07.2020 18:20

I thought red node cannot have a red node as a parent or a child?

Ответить
Stephen Howe
Stephen Howe - 02.07.2020 13:30

You should Red-Black Tree Delete
That is more complex. Inserts always occur at leaf nodes but Deletes can occur at any part of the tree.
Also Inserts have a maximum of 2 rotations, Deletes have a maximum of 3 rotations but both may recolour to the root in repairing.
Having found the node in the tree => count the children: 0, 1 or 2
Deal with the simple cases first.
If the number of children is 0 and the node is red => delete the node, black path count still constant, you are done.
If the number of children is 1 => then node must be black and child red, no other combinations are valid => delete the node and replace it with the child, the child becomes black. Black path count still constant, you are done.
If the number of children is 2 => you need to find the immediate successor or immediate predecessor node.
Immediate successor => look at right child and now follow its children leftwards as far down as you can go.
Immediate predecessor => look at left child and now follow its children rightwards as far down as you can go.
I would recommend finding the Immediate successor and then seeing if it is cheap to delete (above two cases)
If not find the Immediate predecessor and then seeing if it is cheap to delete (above two cases)
And if not pick one of them.
Swap the node you really want to delete with immediate predecessor or immediate successor, BUT DO NOT swap the colours. Now you have reduce the problem to deleting a node with 0 or 1 children and when deleted, the tree will still be ordered correctly. If it is an easy case => delete it.
So what is left to do?
The hard case: Deleting a leaf node, no children and it is black.
You delete it but now the black path tree is in violation.
And basically you work your way towards to root, 1-level at a time.
Intuitively speaking, restoring the delete situation is a 2-prong stategy:
(i) You look at a nodes siblings and your parent. You are looking for red-nodes that that can be rotated or recoloured in such a way that the black path count is restored on your side of the tree while maintaining the black path count on the siblings side of the tree. If you can do that => it is over.
(ii) at the same time, you might find that parent, sibling and siblings children are all black => so what do you do?
You recolour the sibling as red and retry repairing the tree at parent level, the problem has moved up 1-level
So what happens if you encounter the root, no red nodes encountered (which is possible, it is a Red-Black tree)?
Then it is a red black tree. You have introduced a red node in every path by (ii). At the same time, you have reduced the black path count by 1 in every path.

Ответить
Stephen Howe
Stephen Howe - 02.07.2020 11:54

I think you are missing 2 things
1. Any insertion will always occur at leaf nodes. Apart from the first node where it will become the root, this new node will have a parent whose left or right child where inserting is a nil pointer.
2. If the node you just inserted (which is red) has a black parent => no further repair is necessary, it is still a red-black tree.
Otherwise we move onto the cases you discuss.

Ответить
Nam Tran
Nam Tran - 24.03.2020 00:24

for case 2, I don't quite understand the solution. After the rotation, Z and A are still red. Does that not violate the red-black-tree property?

Ответить
Daniel James Dolor
Daniel James Dolor - 25.02.2020 08:12

that relatable moment when your parent is white but your uncle is black

Ответить
Harry Wang
Harry Wang - 25.02.2020 01:13

I feel like all of the trees violated the black property

Ответить
bala prasanna
bala prasanna - 26.01.2020 07:06

crazy explaination

Ответить
teng stone
teng stone - 11.01.2020 10:52

hope thae same vedio to understand deletions in red Black tree.

Ответить
Debalina
Debalina - 22.12.2019 23:37

Please make more videos

Ответить
AK TSR
AK TSR - 19.11.2019 05:58

why A can be in the right subtree when the root is B?

Ответить
FATEH OURGHI
FATEH OURGHI - 18.11.2019 23:03

the best explanation i ever seen, thanx

Ответить
Mudit Malpani
Mudit Malpani - 20.09.2019 02:44

I am black and i do not like the tone in which he is addressing my uncle

Ответить
Vivek Kaul
Vivek Kaul - 02.07.2019 18:58

In Case 2 you already violate the child relationship as you still have a red child as a child for a red parent after rotation

Ответить
Nani Fatimana
Nani Fatimana - 26.06.2019 04:40

I need the Red-Black trees deletion video

Ответить
Nani Fatimana
Nani Fatimana - 26.06.2019 02:57

thank you

Ответить
Marco Aurélio Nascimento
Marco Aurélio Nascimento - 22.06.2019 22:25

It would be nice to emphasize which rules each case violates...

Ответить
Ahmad Khalil
Ahmad Khalil - 20.05.2019 13:03

wow, great video!

Ответить