Red-Black Trees

  Рет қаралды 22,474

Algorithms Lab

Algorithms Lab

Күн бұрын

Пікірлер: 28
@asarephilip
@asarephilip Жыл бұрын
This is the only tutorial that explained when to do recoloring or rotation in a clear and simple way. I wish I could give 10x likes. Thank You!
@mrleenudler
@mrleenudler 2 ай бұрын
Nice and enlightening video, thanks! If I'd dare some criticism, It could be more clear the reason for the rotation in case2. It seems almost like the criterium is whether it's the right or left child of the parent, but that depends again on which child the parent is of the grandparent. Anyway, it's the most informative video I found on the topic, so good job!
@algorithmslab
@algorithmslab 2 ай бұрын
Thanks, good point. From the perspective of the algorithm it makes sense to order the cases as they are (i.e. discuss the corresponding slide from left to right). But for explaining why we have the cases, it might be better to go from right to left (i.e. what do we want to achieve, how we get if from case 3, and then how we get case 3, in case we have case 2). Anyway, you figured it out, so I achieved my goal!
@AdamKuczynski322
@AdamKuczynski322 3 жыл бұрын
Many thanks for all the very informative videos. Your explanations are great, and have helped me learn a great deal. Thanks for your hard work!
@rean223
@rean223 3 жыл бұрын
Very concise and clear, thank you very much Professor!
@j-p-d-e-v
@j-p-d-e-v 7 ай бұрын
Even though I dont understand some of the terms you use (its me on since Im not that good in DSA) but as a whole I like the way you explain things. Im currently studying DSA.
@algorithmslab
@algorithmslab 6 ай бұрын
Thanks! This video is part of a series, and it is probably a good idea to first watch the video that introduces binary search trees (kzbin.info/www/bejne/p3qtdKKXidd7jKssi=N-9x-yNDKw8VcHmi) Success with studying Data Structures and Algorithms!
@kabooby0
@kabooby0 3 жыл бұрын
Excellent explanation. And thank you for assuming knowledge of BSTs and such.
@sthitaprajnapriyadarshan1740
@sthitaprajnapriyadarshan1740 3 жыл бұрын
you deserve more subscribers
@algorithmslab
@algorithmslab 3 жыл бұрын
Thanks! I am already amazed by how many views some of my videos got in the last couple of months.
@sthitaprajnapriyadarshan1740
@sthitaprajnapriyadarshan1740 3 жыл бұрын
@@algorithmslab You shall get more. I am a teacher myself and I can see the potential that your quality of explanation has.
@kavas1970
@kavas1970 3 жыл бұрын
Agreed. Subscribed.
@manafsaeed3547
@manafsaeed3547 2 ай бұрын
Agree
@muhammedsinanck7234
@muhammedsinanck7234 3 жыл бұрын
nice video and simple explanation sir
@algorithmslab
@algorithmslab 3 жыл бұрын
thanks!
@kevinxiong9712
@kevinxiong9712 3 жыл бұрын
I wonder whether a all-black linear list could be seen as RBT.I understand that the corresponding operations guarantee that such trees can not be built.But, it's the definition that comes first. According to my search, as long as a BST that satisfies those 5 requirements should be called RBT. Like 1->2->3->4->NULL with all black color, the case at least to me does not violate any of those properties or I just missed something.
@algorithmslab
@algorithmslab 3 жыл бұрын
Great question. The trick here is that we always include the NIL-Nodes and give them a black color. Note that this is a bit different from how we normally look at the nodes of a binary tree: If a child is NIL (or None in Python), then we would normally just see it as not being there. But to make red-black trees here, we need these NIL nodes to be black. So this already prevents the example of a linear list. In your example the nodes 1,2,3, and 4 would all have an extra NULL (or NIL) attached to them. Therefore starting at 1 there are many paths to leaves 1 -> NULL or 1-> 2 -> NULL etc. They have different lengths, which contradicts property 5. We prove that a red-black tree with n nodes has a height bounded by 2 log⁡(n+1). This follows from the properties, so if a black list would satisfy the properties (which has height n), then something with the properties would have been wrong. I hope this clarifies the role of the NIL-nodes, and thanks for watching!
@kevinxiong9712
@kevinxiong9712 3 жыл бұрын
@@algorithmslab ​ Yes, the concept is now much clear to me. Many proofs for the upper bound of the height are using induction, which seems less intuitive to me. Your proof is concise and easy to follow for me. Thanks for the detailed and patient reply.
@ascyrax8507
@ascyrax8507 8 ай бұрын
awesome vid :)
@mpho_moses
@mpho_moses 2 жыл бұрын
But the resulting tree is not balanced, or is there something I might have missed?
@algorithmslab
@algorithmslab 2 жыл бұрын
Red-black trees are balanced. Maybe there is a small misunderstanding what balanced means? "Balanced" means that the height is in O(log n) (see 00:20-00:35). Note that this is O(log n) and not log n. The smallest height that a binary tree can have is log n, but updates (insert/delete) would take long, if we would always want to have height log n. Asking for height = O(log n) instead, gives us enough flexibility to get efficient updates (i.e. O(log n)), while still getting operations like search (which are in O(height) in O(log n) time. Specifically, the height of a red-black tree is guaranteed to be at most 2*log (n+1) =O(log n), see 04:53. To get a feeling for how unbalanced a red-black tree can get, it can be helpful to use a visualizer like www.cs.usfca.edu/~galles/visualization/RedBlack.html . If you for instance insert 1,2,3,4,5,6,... you will see how the rotations remove the imbalance. In particular, the height is never more than 2 times what the most balanced tree would have. Does this answer your question?
@mpho_moses
@mpho_moses 2 жыл бұрын
@@algorithmslab Thanks so much. This was very helpful. Thanks for the visualizer link as well.
@dmytrodieiev9338
@dmytrodieiev9338 6 ай бұрын
But wait, how did you fix the RB properties at 17:56, if #5 is violated? "For each node, all paths from the node to descendant leaves contain the same number of black nodes".
@algorithmslab
@algorithmslab 6 ай бұрын
The important observation here, is that #5 is actually never violated (i.e. assuming it holds at the beginning, all operations that we do don't break it). When inserting a new node, we make it red. So assuming that I had a correct tree before, the only violation that I now might have is one red child-parent pair. This is fixed in "Step 3". For all operations in Step 3 it is quite clear that they don't introduce a violation of #5. Only for the last rotation (the right rotation on the slide) we have to be careful. There we recolor z and b. But I explain, starting at 17:18, why property #5 still holds, assuming it was true before. So in short, the recoloring of z and b is what we do to make sure that #5 is not violated. I hope this answers the question.
@tsunamio7750
@tsunamio7750 2 жыл бұрын
Could you put this video on Odysee, please? It is excellent! Thanks you!
@algorithmslab
@algorithmslab 2 жыл бұрын
Thanks for the compliment and thanks for the suggestion. Since publishing the videos and replying to questions/discussions here is something I have to fit in on the side, for now, I prefer to focus my attention on the youtube channel.
@tsunamio7750
@tsunamio7750 2 жыл бұрын
@@algorithmslab You did make a nicely complete video about red/black. Gets the abstraction out of the window. Thanks again!
Graphs and Graph Representation (Elementary Graph Algorithms, part 1)
19:26
Red-black trees: Samuel's tutorial
24:20
Samuel Albanie
Рет қаралды 7 М.
🕊️Valera🕊️
00:34
DO$HIK
Рет қаралды 19 МЛН
Red Black Trees 2 Example of building a tree
17:45
RobEdwards
Рет қаралды 105 М.
Understanding B-Trees: The Data Structure Behind Modern Databases
12:39
Lec 10 | MIT 6.046J / 18.410J Introduction to Algorithms (SMA 5503), Fall 2005
1:23:52
Skip Lists
15:36
Algorithms Lab
Рет қаралды 41 М.
5.16 Red Black tree | Introduction to Red Black trees | DSA Tutorials
33:00
Jenny's Lectures CS IT
Рет қаралды 761 М.
Red Black Tree 1 The  Rules
8:10
RobEdwards
Рет қаралды 83 М.
10.1 AVL Tree - Insertion and Rotations
43:08
Abdul Bari
Рет қаралды 1,2 МЛН
🕊️Valera🕊️
00:34
DO$HIK
Рет қаралды 19 МЛН